0

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

I know I can override the onBackPressed() method if I were in an Activity, but I am looking for a solution that works for a service running in the background.

vsx06
  • 87
  • 3
  • 8

2 Answers2

1

Its not a good idea to have the service change the UI directly. You can send a broadcast from the service. Any activity which is open at that point, can listen to the broadcast and then decide to take appropriate action (in this case to close the app). You can simulate a key press from the activity.

Aswin Rajendiran
  • 3,399
  • 1
  • 21
  • 18
  • The problem is, I need the service to simulate a back press, as the app tries to automate back presses in a particular situation. Is using an activity the only way to do this ? – vsx06 May 08 '13 at 00:45
  • you can try this solution by n0sferat0k in http://stackoverflow.com/questions/4967178/how-to-generate-key-presses-programmatically-android. I have not tried nor will I recommend this. – Aswin Rajendiran May 08 '13 at 00:57
1

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

No.

I know I can override the onBackPressed() method if I were in an Activity,

That has nothing to do with "programmatically press the back button".

I am looking for a solution that works for a service running in the background.

Your service is welcome to send a message to your activity which causes the activity to call finish(). You could use any of the following for such a message:

  • LocalBroadcastManager
  • an event bus like Otto
  • a regular broadcast Intent
  • a Messenger
  • etc.

However, if your expectation is that you will be able to attack the activities of other apps, forcing them to call finish(), that is not possible.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • An app called SideBar (on Play Store) reacts to back key presses. It is a foreground service that adds a view as system overlay and removes the view when the back key is pressed. I would like to know how this is done. – RufusInZen Aug 07 '13 at 05:13
  • @Phat7: That's nice, but it has nothing to do with this question. Feel free to open a new question. – CommonsWare Aug 07 '13 at 11:25
  • @CommonsWare: Could you explain why we cannot perform back button in a service? – Jame Nov 25 '16 at 03:33
  • @user8430: You cannot "perform back button" anywhere. – CommonsWare Nov 25 '16 at 12:18
  • Thanks but do you have any official document to prove this – Jame Nov 25 '16 at 12:23
  • @user8430: No more than I have an "official document" to prove that there is no API method to launch a nuclear missile, or to prove that there is no API method to allow for integration with a boulder, etc. – CommonsWare Nov 25 '16 at 12:35
  • Sorry, it is hard to me to prove with my customer that it is no possible. Because the customer wants to perform the back button which allows closing current foreground app. Because he does not know about programming, he wants to a document to prove that work can not do. – Jame Nov 25 '16 at 12:41
  • @user8430: You don't close the current foreground app. You move something else into the foreground, via `startActivity()`. The user may not like this, if you are interrupting something that the user is doing, which is why calling `startActivity()` is not recommended from a service. – CommonsWare Nov 25 '16 at 12:51
  • @CommonsWare: Thank you for your solution. Currently, I have to do as follows: making a service which can close (I am not sure the `close` word is correct or not) the launch app. It must result in as performing physical back button. For example, my current app is voice app, then I open my browser. If the service calls the close function, the result must be similar action when I press the back button. (the browser is removed, the voice app becomes the foreground app). I am not sure `startActiviy` can work. Sorry if my explanation makes complicate – Jame Nov 25 '16 at 13:32