如何强制退出的Andr​​oid应用程序,然后重新启动呢?重新启动、应用程序、Andr、oid

2023-09-05 06:29:58 作者:糟酒

我的Andr​​oid应用程序有一个的WebView 需要Flash插件。 web视图将提供一个市场链接的Adobe Flash,如果它没有安装。安装的Flash,初始化它在我的应用程序的唯一方法后就是强制退出并重新启动应用程序。我想present一个按钮来执行此对他们来说,因为它太复杂,普通用户开放其正在运行的进程和力手动退出该应用程序的用户。什么是Java code来实现这样的重新启动按钮?

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 Flash, 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:

杀死自己的过程。使用 Process.myPid(),将它传递到 Process.killProcess()。您可能需要一个权限添加到您的清单(可能是 android.permission.KILL_BACKGROUND_PROCESSES )来得到这个工作。 在你杀死你自己的过程中,有一个未决的意图重新启动活动在不久的将来注册报警。第二个参数警报。设置 triggerAtTime 。将其设置为0时报警立即开火。这个例子在这里设置一个时间来启动应用程序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. 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.

在code是这样的:

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));