Android:更新后重新启动应用程序 - ACTION_PACKAGE_REPLACED

2024-04-25

我的应用程序不在 Play 商店中,请在网络上验证是否有新版本并下载并启动它。安装后我想重新启动应用程序并使用BroadcastRecevier with ACTION_PACKAGE_REPLACED。这是代码:

播送:

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 应用程序已升级以便重置闹钟? https://stackoverflow.com/questions/2133986/how-to-know-my-android-application-has-been-upgraded-in-order-to-reset-an-alarm

正确的解决方法是您在清单中使用了错误的字符串:http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED

它应该是“android.intent.action.PACKAGE_REPLACED”。


好吧,我发现我写的内容仍然不足以尝试,所以我将破例并发布整个项目只是为了证明它有效: 应用程序代码位于名为“com.broadcast_receiver_test”的包中。 不要忘记在测试之前运行它,否则它将无法在某些 Android 版本上运行(我认为 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:

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();
    }
  }

请运行它并查看它是否完美运行。


编辑:如果您的应用程序适用于 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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:更新后重新启动应用程序 - ACTION_PACKAGE_REPLACED 的相关文章

随机推荐