Android:更新后重新启动应用程序-操作程序包被替换
我的应用程序不在Play Store上,请在web上验证是否有新版本,然后下载并启动它。安装后,我希望重新启动应用程序,并使用Android:更新后重新启动应用程序-操作程序包被替换,android,broadcast,Android,Broadcast,我的应用程序不在Play Store上,请在web上验证是否有新版本,然后下载并启动它。安装后,我希望重新启动应用程序,并使用BroadcastReceiver替换操作包。代码如下: 广播: public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){ ApplicationInfo app = new App
BroadcastReceiver
替换操作包。代码如下:
广播:
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);
}
}
}
<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>
清单:
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);
}
}
}
<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>
问题是,当我安装新的apk时,广播似乎没有启动,为什么?请参见以下内容:
正确的修复方法是您在清单中使用了错误的字符串:
它应该是“android.intent.action.PACKAGE_replacement”
好的,我发现我所写的还不足以尝试,所以我将破例发布一个完整的项目,以证明它是有效的:
应用程序代码位于名为“com.broadcast\u receiver\u test”的包中。
别忘了在测试之前运行它,否则它在一些android版本(我认为是API11+)上无法工作
舱单:
<?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>
请运行它,看看它是否工作正常
编辑:如果您的应用程序适用于API12及以上版本,并且只希望处理更新应用程序的情况,则您可以单独使用此意图:
我将以下接收器放入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>
因此,我的应用程序可以在更新和设备重新启动时启动。当然,正如每个人都提到的,我的软件包需要API 12+。经过两天的努力和实验,我找到了原因。
原来我需要仔细阅读官方文件
正如这里所说:
事实证明,关键短语是:
它不包含任何附加数据
那些。如果你改变了舱单,
更新后,应用程序将不会重新启动
欢迎您经过数小时的搜索,在更新应用程序后如何重新启动应用程序,我终于找到了它无法重新启动的原因
应用程序必须而不是由Android Studio或其他IDE启动。如果我手动安装带有apk的应用程序,并从当前的Android设备启动它,应用程序可以识别我的应用程序是否有更新,并正确重启
我的用户案例是一个自定义启动程序,它可以通过启动PackageInstaller.Session
自行更新,而无需进入谷歌Play商店
这是密码
舱单:
<receiver android:name="UpdateReceiver" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
对于尝试所有其他答案但未成功的用户,我建议您重新启动应用程序
我注意到该应用程序已成功更新,但即使我尝试以多种方式启动该活动,它仍无法启动
我在BroadcastReceiver
中使用了,因此基本上在您下载的更新中,添加ProcessPhoenix.triggerRebirth(上下文)
在您的BroadcastReceiver
中,或者以某种方式在其中重新启动应用程序。您是否尝试检查它是否可以在手动安装较新版本的情况下工作?另外,可能只有当构建数量大于当前数量时才有效?坦白地说,我从来没有使用过它,所以这只是我的猜测。当另一个应用程序正在更新时,你也尝试过听这个事件吗?顺便问一下,你是如何克服确认安装apk的需要的?每次需要更新时,用户是否仍能看到它?我启动APK此代码:Intent intenti=Intent new(Intent.ACTION\u视图);intent.setDataAndType(Uri.fromFile(新文件(Environment.getExternalStorageDirectory()+“/download/”+“apkname.apk”),“application/vnd.android.package-archive”);星触觉(意向)@ShakeBayaz当你的应用程序被清除时,你不会收到通知,因为这也意味着你的应用程序将被停止(至少我注意到了这一点)。无论如何,如果您希望处理应用程序的升级,我找到了另一个解决方案,但它需要API12: