开发者

How to force quit the Android app and restart it? [duplicate]

开发者 https://www.devze.com 2023-03-16 13:52 出处:网络
This question already has answers here: How to force stop my android application programmatically? (7 answers)
This question already has answers here: How to force stop my android application programmatically? (7 answers) Closed 5 years ago.

My Android app has a WebView that requires the Flash plugin. The webview will provide a Market link to Adobe Flash if it's not installed. After installing Fla开发者_StackOverflowsh, the only way to instantiate it in my app is to force quit and restart the app. I would like to present a button to the user that does this for them because it's too complicated for casual users to open up their running processes and force quit the app manually. What is the Java code to implement such a restart button?


You can restart your app in two steps:

  1. Kill your own process. Use Process.myPid(), pass it into Process.killProcess(). You might need to add a permission to your manifest (possibly android.permission.KILL_BACKGROUND_PROCESSES) to get this to work.
  2. Before you kill your own process, register an Alarm with a pending intent to restart your Activity in the very near future. The second parameter to alarm.set is the triggerAtTime. Setting it to 0 causes the alarm to fire immediately. The example here sets a time to launch the app one second into the future.

The Code goes like this:

AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alm.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()), 0));


I suppose you could call finish to make your activity stop but you will not be able to make your app start back up again. That would require (if it is even possible) root and it would be a terrible idea. It would mean that any app could start itself whenever it wanted to: which is just a bad idea and would actually be a bug if it was possible.


you can exit of app with System.exit(0) : this doesn't require permission for your app

0

精彩评论

暂无评论...
验证码 换一张
取 消