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.
Asked
Active
Viewed 451 times
-5
-
2what did you try? – lelloman Mar 21 '17 at 14:15
-
I am trying to send msg after pressing power button 5 times. – Kajal Patidar Mar 21 '17 at 15:10
-
I got the solution here- http://stackoverflow.com/questions/30029978/how-to-detect-device-power-button-press-twice-in-android-programmatically – Kajal Patidar Mar 26 '17 at 09:13
1 Answers
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
}
}
}
-
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