安卓:更新后重新启动应用程序 - ACTION_PACKAGE_REPLACED [英] Android : restart application after update - ACTION_PACKAGE_REPLACED
问题描述
我的应用程序,是不是在播放存储在网络上验证是否有一个新的版本,下载并启动它。安装完毕后,我想重新启动应用程序,我会使用 BroadcastRecevier
与 ACTION_PACKAGE_REPLACED
。这是code:
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 :
广播:
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,广播似乎不启动,为什么?
The problem is that when I install new apk, the Broadcast appears not to start, why ?
推荐答案
看到这样的:
<一个href="http://stackoverflow.com/questions/2133986/how-to-know-my-android-application-has-been-upgraded-in-order-to-reset-an-alarm">How要知道我的Android应用程序已经升级,以重置报警?
正确的解决办法是,你使用了错误的字符串中的清单: 的http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED
correct fix is that you use the wrong string in the manifest: http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED
应该是android.intent.action.PACKAGE_REPLACED代替。
it should be "android.intent.action.PACKAGE_REPLACED" instead.
好了,我看到了什么我已经写尚不足以尝试一下,所以我会破例发布整个项目只是为了显示它的工作原理: 应用程序code是在一个名为com.broadcast_receiver_test包。 不要忘了测试前运行它,否则它不会在一些Android的版本工作(我认为API 11+)。
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+) .
清单:
<?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 .
编辑:如果你的程序是为API12及以上,且只希望处理您的应用程序的更新的情况下,可以单独使用此意图:
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:
的http://developer.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED
这篇关于安卓:更新后重新启动应用程序 - ACTION_PACKAGE_REPLACED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!