-5

I am developing an android app, I need code to perform some tasks in my app when user press power button five times, either app is on or off.

1 Answers1

0

From Android - Count Power button clicks and Start Activity

public class MyReceiver extends BroadcastReceiver {
    static int countPowerOff=0;
    private Activity activity=null;
    public MyReceiver (Activity activity)
    {
    this.activity=activity;
    }
    @Override
    public void onReceive(Context context, Intent intent) {

      Log.v("onReceive", "Power button is pressed.");

      Toast.makeText(context, "power button clicked", Toast.LENGTH_LONG)
             .show();

     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
{
    countPowerOff++;    
} 
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
{
    countPowerOff++;   
      if(countPowerOff==5)
      {
          //do something
       }
    }

}
Community
  • 1
  • 1
lucas_fls
  • 1
  • 2
  • Thank you for the answer. It works when application is on, but I want it in both cases either application is on or off. Please suggest something. – Kajal Patidar Mar 25 '17 at 16:39