屏幕熄灭时杀死应用程序

时间:2017-11-06 01:23:20

标签: android

大家好我是Android界的新手,我想在屏幕熄灭时杀死应用程序,我的意思是如果屏幕超时或按下锁定按钮。 我已经尝试过这种方法,但它对我不起作用,因为我知道我在 MainActivity

onCreate方法的开头调用它
private void registerBroadcastReceiver() {

    final IntentFilter theFilter = new IntentFilter();
    /** System Defined Broadcast */
    theFilter.addAction(Intent.ACTION_SCREEN_ON);
    theFilter.addAction(Intent.ACTION_SCREEN_OFF);
    theFilter.addAction(Intent.ACTION_USER_PRESENT);

    BroadcastReceiver screenOnOffReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String strAction = intent.getAction();

            KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
            assert strAction != null;
            if(strAction.equals(Intent.ACTION_USER_PRESENT) || strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_SCREEN_ON)  ) {
                assert myKM != null;
                if( myKM.inKeyguardRestrictedInputMode())
                {
                    int pid = android.os.Process.myPid();
                    android.os.Process.killProcess(pid);;
                } else
                {
                    int pid = android.os.Process.myPid();
                    android.os.Process.killProcess(pid);
                }
            }

        }
    };

    getApplicationContext().registerReceiver(screenOnOffReceiver, theFilter);
}

不幸的是,这对我不起作用。

1 个答案:

答案 0 :(得分:1)

你可以尝试

  1. 创建一个ScreenOnOffReceiver类&粘贴波纹管代码

    public class ScreenOnOffReceiver extends BroadcastReceiver{
    
     @Override
      public void onReceive(final Context context, Intent intent) {
    
       if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
        {
         //   Log.e("Sleep Time", getTimeForSleep(context)+"");
    
        }
    
       if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
       {
          // some code
          Log.e("screen-- ", "off");
    
          try {
            Intent intent1 = new Intent(context, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
            ((Activity)context).finish();
    
          //  sleepTime = getTimeForSleep(context);
    
            } catch (Exception e) {
             // TODO: handle exception
          }
    
    
        }
    
      }
    
     }
    
  2. 在MainActivity中添加以下代码...

      ScreenOnOffReceiver sonoff;
        try {
           //Log.e("Error Welcome :","Welcome Begin");
           sonoff = new ScreenOnOffReceiver();
           IntentFilter intentFilter = new IntentFilter();
           intentFilter.addAction(Intent.ACTION_SCREEN_ON);
           intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
            registerReceiver(sonoff, intentFilter);
           //Log.e("Screen On Off ", "Started" );
         } catch (Exception e) {
           //Log.e("Error Welcome :", e.toString());
       }
    
  3. 在AndroidManifest.xml中添加Bellow代码

       <receiver android:name=".ScreenOnOffReceiver">
            <intent-filter>
              <action android:name="android.intent.action.SCREEN_ON" />
              <action android:name="android.intent.action.SCREEN_OFF" />
            </intent-filter>
        </receiver>
    
  4. 我认为你的问题将会得到解决。提前谢谢...... Enamul