如何从应用程序内重新启动 Android 应用程序

发布于 2024-10-05 21:57:57 字数 237 浏览 12 评论 0原文

我需要在用户更改首选项时重新启动应用程序。清除堆栈对我没有帮助,因为这不会取消后端服务调用。我想终止应用程序进程本身。我正在使用它

Process.killProcess(Process.myPid());

,它可以帮助我杀死该应用程序。但我需要的是重新启动应用程序。意味着终止进程并触发一个新进程,以便应用程序再次重新启动。

有办法做到这一点吗?

提前致谢。

I have a requirement to restart the application when the user changes a preference. Clearing the stack doesnt help me since this doesnt cancel the backend service calls. I want to kill the application process itself. I am using

Process.killProcess(Process.myPid());

and it works for me to kill the application. But what i need is to restart the application. Means kill the process and trigger a new process so the application start fresh once again.

Is there a way to do this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

月下凄凉 2024-10-12 21:57:58

这不应该是人们应该在测试环境之外尝试做的事情。

也就是说,有两个想法:

1)在不久的将来的某个时间设置一个警报,然后终止你的进程

2)启动其他东西(可能是一个小的本机进程或shell脚本)来检测你的死亡并通过一个命令重新启动你。 您也可以尝试发出一个意图来

启动自己,然后快速死亡,但这听起来像是一个潜在的竞争条件,具体取决于实现。如果您从 /proc 中获取活页夹 fd 并在本机代码中做了邪恶的事情,您可能会以这样的方式触发意图,使您的应用程序在从 ioctl 返回时崩溃...

This is not something which one should probably attempt to do outside of a testing environment.

That said, two ideas:

1) Set an alarm for some time in the very near future and then kill your process

2) Start up something else (perhaps a small native process or shell script) that will detect your death and restart you via an intent

You could also try firing off an intent to start yourself and then dying quickly, but this sounds like a potential race condition depending on implementation. If you grabbed the binder fd out of /proc and did evil things in native code, you might be able to fire off the intent in such a way that your application crashes on the return from the ioctl...

绮烟 2024-10-12 21:57:58

Android 并不是为此而设计的,这也不是“要求”。这是一个实现。具体要求是什么?为什么您不能将应用程序设计为在不重新启动的情况下处理首选项更改?这似乎是一个非常糟糕的解决方案。

Android is not designed to do this, and this is not a "requirement". This is an implementation. What exactly is the requirement? Why can't you design your app to handle preference changes without a restart? That seems like a very poor solution.

朦胧时间 2024-10-12 21:57:58

设计操作系统以使应用程序可以自行重启似乎是一个非常糟糕的主意。 Android 操作系统需要能够自由地终止进程以释放内存——如果应用程序可以突然自行重启,则释放的内存将再次被用完。我同意 Falmarri 的观点,您需要调查为什么您的应用程序无法即时处理偏好更改。

Designing the OS so that an app could restart itself seems like a Very Bad Idea. The Android OS needs to be free to kill a process to free up memory -- if the app could restart itself suddenly the freed memory is used up again. I agree with Falmarri, you need to investigate why your app can't deal with a preference change on the fly.

十二 2024-10-12 21:57:58

由于您已完成终止进程,因此要重新启动应用程序,请使用以下代码:

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

Since you are done with killing the process, to restart the application use this code:

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
若无相欠,怎会相见 2024-10-12 21:57:58

如果您确实想重新启动活动(包括终止当前进程),请尝试以下代码。将其放在 HelperClass 中或您需要的地方。

public static void doRestart(Context c) {
        try {
            //check if the context is given
            if (c != null) {
                //fetch the packagemanager so we can get the default launch activity 
                // (you can replace this intent with any other activity if you want
                PackageManager pm = c.getPackageManager();
                //check if we got the PackageManager
                if (pm != null) {
                    //create the intent with the default start activity for your application
                    Intent mStartActivity = pm.getLaunchIntentForPackage(
                            c.getPackageName()
                    );
                    if (mStartActivity != null) {
                        mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        //create a pending intent so the application is restarted after System.exit(0) was called. 
                        // We use an AlarmManager to call this intent in 100ms
                        int mPendingIntentId = 223344;
                        PendingIntent mPendingIntent = PendingIntent
                                .getActivity(c, mPendingIntentId, mStartActivity,
                                        PendingIntent.FLAG_CANCEL_CURRENT);
                        AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
                        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
                        //kill the application
                        System.exit(0);
                    } else {
                        Log.e(TAG, "Was not able to restart application, mStartActivity null");
                    }
                } else {
                    Log.e(TAG, "Was not able to restart application, PM null");
                }
            } else {
                Log.e(TAG, "Was not able to restart application, Context null");
            }
        } catch (Exception ex) {
            Log.e(TAG, "Was not able to restart application");
        }
    }

这还将重新初始化 jni 类和所有静态实例。

If you really want to restart your activity including a kill of the current process, try following code. Place it in a HelperClass or where you need it.

public static void doRestart(Context c) {
        try {
            //check if the context is given
            if (c != null) {
                //fetch the packagemanager so we can get the default launch activity 
                // (you can replace this intent with any other activity if you want
                PackageManager pm = c.getPackageManager();
                //check if we got the PackageManager
                if (pm != null) {
                    //create the intent with the default start activity for your application
                    Intent mStartActivity = pm.getLaunchIntentForPackage(
                            c.getPackageName()
                    );
                    if (mStartActivity != null) {
                        mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        //create a pending intent so the application is restarted after System.exit(0) was called. 
                        // We use an AlarmManager to call this intent in 100ms
                        int mPendingIntentId = 223344;
                        PendingIntent mPendingIntent = PendingIntent
                                .getActivity(c, mPendingIntentId, mStartActivity,
                                        PendingIntent.FLAG_CANCEL_CURRENT);
                        AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
                        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
                        //kill the application
                        System.exit(0);
                    } else {
                        Log.e(TAG, "Was not able to restart application, mStartActivity null");
                    }
                } else {
                    Log.e(TAG, "Was not able to restart application, PM null");
                }
            } else {
                Log.e(TAG, "Was not able to restart application, Context null");
            }
        } catch (Exception ex) {
            Log.e(TAG, "Was not able to restart application");
        }
    }

This will also reinitialize jni classes and all static instances.

私野 2024-10-12 21:57:58

这里有一个想法:

拥有一个与其余组件(活动、服务等)具有不同流程的服务。

为了重新启动应用程序,请调用此服务,它将执行的操作是终止应用程序其他组件的进程,然后向您想要进入的活动发送意图。

这只是一个想法,因为我从未测试过。请告诉我它是否有效,并更新问题以包含答案(如果它确实有效)。

here's an idea:

have a service that has a different process than the one of your the rest of the components (activities , services,...) .

in order to restart the app , call this service and what it will do is to kill the processes of the other components of the app , and then send an intent to the activity that you wish to go to .

it's just an idea , since i've never tested it . please tell me if it works and update the question to include the answer if it really works.

以为你会在 2024-10-12 21:57:58

如果这是一个您可以控制设备的企业项目,您可以轻松安装第二个应用程序,充当第一个应用程序的看门狗。我的一位客户使用这样的看门狗应用程序来处理诸如夜间应用程序重新启动(出于奇怪的非技术原因)之类的事情,以及检查和安装自助(非 Play 商店)应用程序更新。

但如果您确实想在应用程序中执行此操作,则可以执行以下操作(包括上面的终止进程):

Process.killProcess(Process.myPid());
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 500 /* half a second*/,
       PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()),
                                 Intent.FLAG_ACTIVITY_NEW_TASK));

If this is an enterprise project where you have control of the devices, you could easily install a second application that functions as a watchdog to the first. One of my clients uses such a watchdog application to handle things such as a nightly application reboot (for weird, non-technical reasons) and to check for and install self-served (non Play Store) application updates.

But if you really wanted to it from within your app, you can do the following (including your kill process above):

Process.killProcess(Process.myPid());
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 500 /* half a second*/,
       PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()),
                                 Intent.FLAG_ACTIVITY_NEW_TASK));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文