2016-07-25 103 views
0

当Android通过用户按下按钮或屏幕超时进入待机状态时,如何关闭我的应用程序? 我试过onResume(),onPause和onRestart()方法,但我已经在另一个上下文中使用了。Android关闭应用程序支持

问候

回答

1

在您的应用程序类的onCreate()方法,登记接收的屏幕关闭事件

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
intentFilter.addAction(Intent.ACTION_SCREEN_OFF); 
registerReceiver(new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
      Log.d(TAG, Intent.ACTION_SCREEN_OFF); 
     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
      Log.d(TAG, Intent.ACTION_SCREEN_ON); 
     } 
    } 
}, intentFilter); 

这样。