Android-Close Other Apps

前端 未结 5 713
甜味超标
甜味超标 2020-12-03 19:38

I have some code that will launch a different application using intents but what can I do to close or kill the other app?

Here is the launch code (works great):

相关标签:
5条回答
  • 2020-12-03 19:49

    The only proper way to kill another "App" would be to send some kind of Intent to the app (ex. Pandora) or it's service that will ask it to exit.

    Task killers use facilities outside of the normal means of Android like Process.killProcess() that talks to the Linux Kernel and not Android. The problem with that approach is it does not tell Android to close the program, but instead the Linux Kernel directly. If a background service is killed, for example, without calling Context.stopService(), Android may restart it later automatically. Also, killProcess doesn't just close the Activity, it kills all the components and doesn't allow them to shutdown normally.

    If you started Pandora with Activity.startActivityForResult() you could try closing it with Activity.finishActivity(), but there's no guarantee that it will work the way you want it to.

    0 讨论(0)
  • 2020-12-03 19:50

    I ended up finding the real answer to this question. Please see Automate closing of applications in Android for the solution!

    0 讨论(0)
  • 2020-12-03 20:09

    Basic answer, you can't. As of Froyo, the only facility available is ActivityManager.killBackgroundProcess(), which:

    (a) Only allows you to kill processes that are, as it said, in the background and not visible to the user. That is, it does not allow you to disrupt the normal execution of the other app.

    (b) Is not needed anyway, because Android will kill the background processes for you if it needs their memory.

    You simply do not need to do what you at this point seem to think you do. "Because my users have asked for it" is not a reason to be going around killing other people's processes.

    0 讨论(0)
  • 2020-12-03 20:15

    you can use Process.killProcess()? you need to know the PID. to get the PID, you can exec "ps", but that's not going to be robust as the text output format could be different across devices or releases. you could provide your own native library to gather the process names and IDs into a standard format for your own purpose.

    0 讨论(0)
  • 2020-12-03 20:16

    but what can I do to close or kill the other app?

    You don't. Your user can exit that "other app" when they wish to.

    0 讨论(0)
提交回复
热议问题