Reputation: 4918
Can the a backbutton press be detected from a service?
As the header says really? I have done a lot of googling but can't find a definitive answer. Nor a way to do it?
Upvotes: 3
Views: 2173
Reputation: 1326
If through your service your are adding view or something then you can use EditText box to capture back press. Just set its onKeyListerner. (Hide edittext box behind other view)
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d("TAG", "onKey() called with: " + "v = [" + v + "], keyCode = [" + keyCode + "], event = [" + event + "]");
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction()== KeyEvent.ACTION_UP){
onBackPress();
}
return false;
}
});
Upvotes: 0
Reputation: 1006554
Can the a backbutton press be detected from a service?
No, sorry. If you have an activity in the foreground, the activity can detect a BACK press. If you do not have an activity in the foreground, the BACK button has nothing to do with your code.
Upvotes: 4
Reputation: 1503
Don't think so. Back button is something managed at activity level, while a service is totally separated from this level.
The only way you have is to communicate a back pression from your activity to your service. But, you need that your activity is in foreground.
Upvotes: 2