3

I would like to execute some code when user presses one of hardware buttons(it doesn't matter, which of these, but Power is preferrable).

I would like to catch events even application is minimized to background and device is sleeping. (Primary task is processing events, when device is inside pocket and the user presses button three times in a row, but not takes out the device).

Is that possible? If it isnt't possible for power button, may be it's possible for volume or home buttons? How can I do it?

I tried many ways, but the only worked is that:

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);

But I can't process triple press because of huge delay between actual button press and receiver's code execution.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Egor
  • 435
  • 5
  • 17

1 Answers1

1

I don't think Android will allow that. The app in the foreground should logically get preference in all respects. Doing what you suggest would be like intercepting the behavior of another app (they might want to use the hardware buttons too). This must be a violation of the T&C, I believe.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • Yes, I understand this, so I would like to process triple press of power button (which will not intercept other apps behaviour). Can I do this? – Egor Mar 18 '14 at 09:33
  • 1
    Still, how do you know another application would never want to do that? – Viral Patel Mar 18 '14 at 09:36
  • Hmmm, I've never seen apps, that process triple Power press when device is sleeping, before... – Egor Mar 18 '14 at 09:42