26

我不在 Play 商店中的应用程序在网络上验证是否有新版本并下载并启动它。安装后我想重新启动应用程序,我会使用BroadcastRecevierwith 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时,广播似乎没有开始,为什么?

4

5 回答 5

30

看到这个:

如何知道我的 Android 应用程序已升级以重置警报?

正确的解决方法是您在清单中使用了错误的字符串: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

于 2012-05-23T22:23:57.023 回答
8

我将以下接收器放在 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>

所以我的应用程序可以在更新和设备重启时启动。当然,正如每个人都提到的,MY_PACKAGE_REPLACED 需要 API 12+。

于 2016-03-01T00:31:31.267 回答
1

经过数小时搜索如何在更新后重新启动您的应用程序后,我终于找到了为什么它不会重新启动。

该应用不得Android Studio 或其他 IDE 启动。如果我使用其 apk 手动安装应用程序并从当前的 Android 设备启动它,该应用程序可以识别我的应用程序是否有更新并正确重新启动。

我的用户案例是一个自定义启动器,它可以自行更新PackageInstaller.Session而无需去 Google Play 商店

这是代码

显现:

<receiver android:name="UpdateReceiver" >
     <intent-filter>
         <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
     </intent-filter>
</receiver>

UpdateReceiver(Kotlin 代码):

class UpdateReceiver: BroadcastReceiver() {

    companion object {
        private val TAG = UpdateReceiver::class.java.simpleName
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        Log.d(TAG, "onReceive ${intent?.action ?: "unknown action"}")
        if (intent?.action == Intent.ACTION_MY_PACKAGE_REPLACED) {
          
            //MainActivity = Your first activity
            val yourIntent = Intent(context, MainActivity::class.java)
            yourIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            context?.startActivity(yourIntent)
        }

    }
}
于 2020-07-03T07:42:09.867 回答
0

经过两天的奋斗和实验,我找到了原因。原来我需要仔细阅读官方文档。

正如这里所说: https ://developer.android.com/reference/android/content/Intent#ACTION_MY_PACKAGE_REPLACED

原来,关键短语是: 它不包含任何额外的数据

那些。如果您更改了清单,那么应用程序将不会在更新后重新启动

不客气

于 2020-01-13T22:40:33.803 回答
0

对于尝试所有其他答案但没有成功的人,我建议您重新启动应用程序。

我注意到该应用程序已成功更新,但即使我尝试以多种方式启动该活动,它也没有启动。

我在 中使用了https://github.com/JakeWharton/ProcessPhoenixBroadcastReceiver,所以基本上在你下载的更新中,添加ProcessPhoenix.triggerRebirth(context);你的BroadcastReceiver,或者以某种方式重新启动应用程序。

于 2020-09-11T20:58:14.543 回答