Android:更新后重新启动应用程序 - ACTION_PACKAGE_REPLACED
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10728016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android : restart application after update - ACTION_PACKAGE_REPLACED
提问by MisterX_Dev
My application that is not on Play Store verify on the web If there are a new version and download and start it. After the installation I would like to restart the application and I would use a BroadcastRecevier
with ACTION_PACKAGE_REPLACED
. This is the code :
我不在 Play 商店中的应用程序在网络上验证是否有新版本并下载并启动它。安装后,我想重新启动应用程序,并使用BroadcastRecevier
with ACTION_PACKAGE_REPLACED
. 这是代码:
Broadcast:
播送:
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
ApplicationInfo app = new ApplicationInfo();
if(app.packageName.equals("it.android.downloadapk")){
Intent LaunchIntent = context.getPackageManager().getLaunchIntentForPackage(app.packageName);
context.startActivity(LaunchIntent);
}
}
}
Manifest:
显现:
<receiver android:name="it.android.downloadapk.Broadcast">
<intent-filter>
<action android:name="android.intent.action.ACTION_PACKAGE_REPLACED"></action>
<data android:scheme="package" android:path="it.android.downloadapk" />
</intent-filter>
</receiver>
The problem is that when I install new apk, the Broadcast appears not to start, why ?
问题是当我安装新的 apk 时,广播似乎没有开始,为什么?
回答by android developer
see this:
看到这个:
How to know my Android application has been upgraded in order to reset an alarm?
correct fix is that you use the wrong string in the manifest: http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED
正确的解决方法是您在清单中使用了错误的字符串:http: //developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED
it should be "android.intent.action.PACKAGE_REPLACED" instead.
它应该是“android.intent.action.PACKAGE_REPLACED”。
ok , i see that what i've written is still not enough to try it out, so i will make an exception and publish a whole project just to show that it works: app code is in a package called "com.broadcast_receiver_test" . don't forget to run it before testing , or else it won't work on some android versions (i think API 11+) .
好的,我看到我写的内容仍然不足以尝试它,所以我将破例并发布整个项目只是为了表明它有效:应用程序代码位于名为“com.broadcast_receiver_test”的包中。不要忘记在测试前运行它,否则它在某些 android 版本上将无法运行(我认为 API 11+)。
manifest:
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcast_receiver_test" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:name=".BroadcastReceiverTestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
</manifest>
MyBroadcastReceiver.java:
MyBroadcastReceiver.java:
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(final Context context,final Intent intent)
{
final String msg="intent:"+intent+" action:"+intent.getAction();
Log.d("DEBUG",msg);
Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
}
}
please just run it and see that it works perfectly .
请运行它,看看它是否完美运行。
EDIT: if your app is for API12 and above, and only wish to handle the case of updating of your app, you can use this intent alone:
编辑:如果您的应用适用于 API12 及更高版本,并且只想处理应用更新的情况,您可以单独使用此意图:
http://developer.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED
http://developer.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED
回答by Sileria
I put the following receiver in the AndroidManifest.xml
我将以下接收器放在 AndroidManifest.xml 中
<receiver android:name=".StartupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
So my app can be launched on update as well as device reboot. Ofcourse as everyone has mentioned that you need API 12+ for MY_PACKAGE_REPLACED.
所以我的应用程序可以在更新和设备重启时启动。当然,正如每个人都提到的,MY_PACKAGE_REPLACED 需要 API 12+。
回答by Александр Остапюк
After two days of struggle and experimentation, I found out the reason. It turned out I need to carefully read the official documentation.
经过两天的挣扎和实验,我找到了原因。原来我需要仔细阅读官方文档。
As said here: https://developer.android.com/reference/android/content/Intent#ACTION_MY_PACKAGE_REPLACED
正如这里所说:https: //developer.android.com/reference/android/content/Intent#ACTION_MY_PACKAGE_REPLACED
It turned out the key phrase is: It does not contain any additional data
原来关键短语是: 它不包含任何额外的数据
those. if you changed the manifest, then the application will not restart after updating
那些。如果您更改了清单,则应用程序将不会在更新后重新启动
you are welcome
不客气