-3

A SERVICE I HAVE NOT ANY ACTIVITY is running in forebackground and when user click on app button i want do function of hardware back button onBackPressed is not working inside service!!
enter image description here

David Wasser
  • 93,459
  • 16
  • 209
  • 274
dornadev
  • 1
  • 1
  • 8

4 Answers4

4

Is it not possible to programmatically press the back button on android through a service

onBackPressed() Called when the activity has detected the user's press of the back key.

you need to just call onBackPressed() method don't forgot override onBackPressed() in your activity

@Override
    public void onBackPressed() {
        super.onBackPressed();   
        // perform your action here

    }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 1
    @dornadev **Is it not possible to programmatically press the back button on android through a service** – AskNilesh Sep 14 '17 at 09:17
  • I DEVELOPED HOME BUTTON AND RECENT BUTTON JUST NEED TO BACK BUTTON – dornadev Sep 14 '17 at 09:21
  • explain in brief @dornadev **I DEVELOPED HOME BUTTON AND RECENT BUTTON JUST NEED TO BACK BUTTON** – AskNilesh Sep 14 '17 at 09:30
  • OP has a `Service`. He wants to press the BACK button programatically. He doesn't have an `Activity` running, so can't call any methods of `Activity`. This answer is not relevant. – David Wasser Sep 15 '17 at 12:12
  • @DavidWasser i know that read first line of my ans **Is it not possible to programmatically press the back button on android through a service** – AskNilesh Sep 15 '17 at 12:14
2

Currently, it's not possible to press "BACK" from service. Since you don't have an activity in the foreground, you don't have the ability to programmatic to press a BACK press, due the fact the service not provide any user interface. What is the option - If you have a rooted device with an access to shell you can try to use the following command in the service Process process = Runtime.getRuntime().exec("adb shell input keyevent 4");

  • Good idea, I'm trying it but if used in an activity it works, while in a service (floating window) it doesn't do anything, do you have an idea why? – gidanmx2 Jun 14 '22 at 18:27
0

You can handle back button operation like this;

@Override
public void onBackPressed() {
// your code.
}

otherwise try below code;

@Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
    // your code
    return true;
}

return super.onKeyDown(keyCode, event);
}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
  • OP doesn't want to detect the BACK button being pressed. He wants to press it himself programatically. This answer is not relevant. – David Wasser Sep 15 '17 at 12:12
0

You can handle back button operation like this;

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        //Home Button!
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
  • OP doesn't want to detect the BACK button being pressed. He wants to press it himself programatically. This answer is not relevant. – David Wasser Sep 15 '17 at 12:10