3

I want to capture Power button press event in my app and that too inside a Service that runs in background. For this I used following code with Broadcast Receiver.

private static final String ACTION="android.intent.action.SCREEN_ON";
private BroadcastReceiver yourReceiver;

onReceive() Method:

final IntentFilter theFilter = new IntentFilter();
    theFilter.addAction(ACTION);
    this.yourReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Do whatever you need it to do when it receives the broadcast
            // Example show a Toast message...
            Log.d("button","button pressed");
            Toast.makeText(context,"pressed",Toast.LENGTH_LONG).show();
        }
    };

With this code I am able to detect button's press event inside receiver.

Now I want to detect that whether power button was pressed for 3 or more seconds then I want to perform specific action inside my receiver. For this I found out this method

    @Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_POWER) {
        // Do something here...
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}

But I found out that this method will only run inside an activity, but I want to call it inside a Service.

Is there any way to detect whether power button was pressed for 3 seconds or can we use the above method inside Service.

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
  • Here is the fully working [answer](https://stackoverflow.com/questions/57051157/how-to-override-double-or-even-3-times-clicking-power-button-or-even-volume-up-d/57056408#57056408) to this question – Niamatullah Bakhshi Jul 16 '19 at 11:44

1 Answers1

0

There's no way to do that "without doing a complete mess" natively in android, and chances that you are actually doing something wrong if you need to do that are high because as specified in the Service Documentation "A Service is an application component representing either an application's desire to perform a longer-running operation while NOT INTERACTING WITH USER". The service component is meant to be just for long perform tasks that do not require any interaction nor depend at all from specific actions on activity other than start and stop. By default the service do not contain input methods like the one you need, but just to answer your question anyway. One way to do it would be by binding the service to an activity, getting the service reference from the activity, implement a method in the service and then execute that method from your activity when onKeyPress is called in it.

Devendra Singh
  • 2,343
  • 4
  • 26
  • 47
  • It is a kind of emergency app therefore I want my app always running in background, therefore I am using service – Vivek Mishra Apr 23 '16 at 07:52
  • You can not do this. :) – Devendra Singh Apr 23 '16 at 07:57
  • Here is the fully working [answer](https://stackoverflow.com/questions/57051157/how-to-override-double-or-even-3-times-clicking-power-button-or-even-volume-up-d/57056408#57056408) to this question – Niamatullah Bakhshi Jul 16 '19 at 11:44
  • @DevendraSingh please either delete your answer or update it, there is a fully working answer. – Niamatullah Bakhshi Jul 16 '19 at 11:45
  • @NiamatullahBakhshi This is not an accepted answer and old question as well,And you posted same question while you might post your answer here, not creating a duplicate question. – Devendra Singh Jul 17 '19 at 12:39
  • @Davendra The reason I dint post all the answer but a link to the answer is this question was asked very long time back and even if I put all the answer here, it may not be accepted because the one who has asked this question might ignore the answer since it is very old. – Niamatullah Bakhshi Jul 20 '19 at 00:31