How to kill the app whenever the screen goes off?

I want to kill the app when ever the screen goes off, I mean if the screen timeout or when the lock button is pressed.

I've tried this method but it does not work for me, knowing that I call it at the beginning of the the onCreate method on my MainActivity:

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);
}

Unfortunately this does not work for me.

Upvotes: 3

Views: 158

Answers (1)

Enamul Haque
Enamul Haque

Reputation: 5063

You can try

  1. Create a ScreenOnOffReceiver class & paste bellow code

    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. Add bellow code in 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. Add Bellow code in AndroidManifest.xml

       <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. I think your problem will be solved. Advance Thanks...Enamul

Upvotes: 1

Related Questions