I am developing an application in which call on an number on power button click (4 times) but now issue is when user press home button 4 times it will trigged the call and I want only side power button click.
Asked
Active
Viewed 1,015 times
-7
-
Share your code...what have you tried until now – Akshay Aug 02 '17 at 06:34
-
have you try this https://stackoverflow.com/questions/37157921/want-to-access-power-button-events-in-android?answertab=votes#tab-top ? – Mohit Suthar Aug 02 '17 at 06:35
-
Yes I have tried both Let me share My code too.. – Er.Nancy Thakkar Aug 02 '17 at 06:37
-
@JRG I want only side power button click this code is triggered center button click also please read my point. – Er.Nancy Thakkar Aug 02 '17 at 08:25
2 Answers
1
My receiver and service in manifest
<receiver
android:name=".services.SOSBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
</intent-filter>
</receiver>
<service
android:name=".services.SOSService"
android:enabled="true">
</service>
and my BroadcastReceiver class
public class SOSBroadcastReceiver extends BroadcastReceiver
{
private static long lastTriggerTime = 0;
private static final int ONE_MILLI = 1000;
protected static final long ONE_SEC = 1 * ONE_MILLI;
protected static final long TWO_SEC = 2 * ONE_MILLI;
protected static final long THREE_SEC = 3 * ONE_MILLI;
protected static final int TRIGGER_THRESHOLD = 3;
protected static boolean triggerInProgress = false;
protected static int triggerCounter = 0;
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().contains(Intent.ACTION_SCREEN_ON))
{
if (!triggerInProgress)
{
checkAndCreateAlert(context);
}
}
else if (intent.getAction().contains(Intent.ACTION_SCREEN_OFF))
{
if (!triggerInProgress)
{
checkAndCreateAlert(context);
}
}
}
private void checkAndCreateAlert(Context context)
{
/*---- If the gap between power button press is less than 2 seconds ----*/
if ((System.currentTimeMillis() - lastTriggerTime) <= TWO_SEC
|| (triggerCounter == 0))
{
triggerCounter++;
lastTriggerTime = System.currentTimeMillis();
}
else
{
triggerCounter = 0;
}
if (triggerCounter > TRIGGER_THRESHOLD)
{
((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(1000);
triggerInProgress = true;
Intent intent = new Intent(context, SOSActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("timer", true);
context.startActivity(intent);
triggerInProgress = false;
triggerCounter = 0;
}
}
}
My code will keep the count of power button click in terms of screen_on and screen_off event and execute the other method if power button is pressed more than 3 time in 2secs.
This is my Service class
public class SOSService extends Service
{
BroadcastReceiver mReceiver;
IntentFilter pqrs_intentFilter;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public void onDestroy()
{
unregisterReceiver(mReceiver);
}
@Override
public void onStart(Intent intent, int startid)
{
pqrs_intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
pqrs_intentFilter.addAction(Intent.ACTION_SCREEN_ON);
mReceiver = new SOSBroadcastReceiver();
registerReceiver(mReceiver, pqrs_intentFilter);
}
public void onStop(Intent intent)
{
unregisterReceiver(mReceiver);
}
}

Mohammed Farhan
- 1,120
- 8
- 14
-
-
Because In my code when user press center button home it will triggered an event – Er.Nancy Thakkar Aug 02 '17 at 06:39
-
-
have a try for me it works. When user click power button then am keeping its count based on screen_on/off. And if screen_on/off exceeds more than 3 times in 2 secs then am starting another activity. – Mohammed Farhan Aug 02 '17 at 06:40
-
-
-
-
Yes I am starting service in splash. Intent intent_service = new Intent(getApplicationContext(), SOSService.class); startService(intent_service); – Er.Nancy Thakkar Aug 02 '17 at 07:25
-
-
-
Perhaps its working code in my current project. Did you include service and receiver in manifest? And also am using this code to have a count of screen on and screen off which is possible only if user clicks on power button only. What do you mean by center button? If its mobile hardware home button you are talking about then just tell me clicking/pressing home button will it lead to on and off the screen? In my mobile screen goes on and off only by pressing power button and usually in all mobiles. Which mobile have you got which does this functionality on home button pressed? – Mohammed Farhan Aug 02 '17 at 07:44
-
you need not to press power button soo fast. When you press it for first time then screen goes off, when it goes off then again you need to press power button then screen goes on. Like wise you need to do on off more than 3 s times in 2 secs. Then only broadcast receiver will trigger the code. Hope its clear. – Mohammed Farhan Aug 02 '17 at 07:47
-
Yes this code is working but it triggered on home button click also My question is I want only side power button click (shut down) – Er.Nancy Thakkar Aug 02 '17 at 08:24
0
public class ExampleActivity extends Activity {
@Override
protected void onCreate() {
// INITIALIZE RECEIVER
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
// YOUR CODE
}
@Override
protected void onPause() {
// WHEN THE SCREEN IS ABOUT TO TURN OFF
if (ScreenReceiver.wasScreenOn) {
// THIS IS THE CASE WHEN ONPAUSE() IS CALLED BY THE SYSTEM DUE TO A SCREEN STATE CHANGE
System.out.println("SCREEN TURNED OFF");
} else {
// THIS IS WHEN ONPAUSE() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
}
super.onPause();
}
@Override
protected void onResume() {
// ONLY WHEN SCREEN TURNS ON
if (!ScreenReceiver.wasScreenOn) {
// THIS IS WHEN ONRESUME() IS CALLED DUE TO A SCREEN STATE CHANGE
System.out.println("SCREEN TURNED ON");
} else {
// THIS IS WHEN ONRESUME() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
}
super.onResume();
}
}
Receiver
public class ScreenReceiver extends BroadcastReceiver {
private boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
Service
public static class UpdateService extends Service {
@Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
// YOUR CODE
} else {
// YOUR CODE
}
}
}
Hopefully this was useful. Let me know if you have questions.

Akshay Chopde
- 670
- 4
- 10
-
I an using the same code, I want only side power button click this code is triggered on home button click also. – Er.Nancy Thakkar Aug 02 '17 at 07:34