react-native - android怎么在一个非activity类里面重启app
阿神
阿神 2017-04-18 09:18:41
0
4
514

stackoverflow翻了个遍也没找到解决办法。就很简单的一个功能,我实现了一个热更新功能,想在更新后重启app以应用此更新,因为如果不重启的话,此热更新根本不会立即应用。

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

用了这个方法,调用后程序会弹出一个提示框,说抱歉,程序已经终止运行,然后就关闭了(有时候屏幕会闪一下,看起来好像重启了,但实际确没更新)。请教大家有什么办法没有?

阿神
阿神

闭关修行中......

reply all(4)
迷茫

Try this method to see if it works
ActivityManager am = (ActivityManager) mContext.getSystemService(mContext.ACTIVITY_SERVICE);
am.restartPackage("packageName");

巴扎黑

This “simple” feature is anything but simple! Just like "how to ensure that the application process is not killed" is not simple, so a third-party application itself does not have the ability to restart itself. But it is not impossible, jni forks the process to implement process daemon, but it is not simple. If you are interested, you can Google it.

阿神

I don’t quite understand whether to restart the App process or restart the Activity?
If you want to restart the Activity, just

Intent intent = new Intent(mContext, ...);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(i);

If you want to restart the process, there seems to be no good way. To restart the process, you need to kill the process first:

android.os.Process.killProcess(android.os.Process.myPid());

You need to restart next because the process has been killed and you need to rely on the system to achieve it. It seems that it can be achieved through broadcast or service. I haven’t done it specifically, so google it yourself

伊谢尔伦

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
改成
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
试试

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template