0

I'm creating an app with service that shows Toast on a long press of the power button after the app is killed.

How can I implement code that detects the long press of power button?

Here is My Service Class

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

   final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    longClick = new LongClick();
    registerReceiver(longClick, intentFilter);


    // Toast.makeText(getApplicationContext(),"Service Started",Toast.LENGTH_LONG).show();
    return START_STICKY;
}

MyRecevier Class

    public class LongClick extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
        {
            Toast.makeText(context,"Long Click",Toast.LENGTH_LONG).show();
        }
    }
}
Sagar Poshiya
  • 136
  • 3
  • 11
  • duplicate? https://stackoverflow.com/questions/25832380/android-detect-pressing-on-power-key – TooManyEduardos Feb 08 '19 at 05:04
  • You can not access power button click event . Also `Service` are no more runs in background continuously. – ADM Feb 08 '19 at 05:34
  • Possible duplicate of [Want to Access Power Button events in android](https://stackoverflow.com/questions/37157921/want-to-access-power-button-events-in-android) – ADM Feb 08 '19 at 05:35
  • i make service that run in background after app killed – Sagar Poshiya Feb 08 '19 at 06:11
  • And i also implemented power button normal press that work perfectly after app killed – Sagar Poshiya Feb 08 '19 at 06:13
  • @TooManyEduardos Not same as per your link but here i have tooo detect long press of power button after app killed or remove by the user from recent apps...... – Sagar Poshiya Feb 18 '19 at 10:59

0 Answers0