English 中文(简体)
Android: 更新后重新启动应用程序 - Action_ Package_ REPLAED
原标题:Android : restart application after update - ACTION_PACKAGE_REPLACED

我的应用程序不在网上 Play Store 校验, 如果有新版本, 下载并启动它。 在安装后, 我想重新启动应用程序, 并使用 BroadcastRecevier 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时, 广播似乎没有开始, 为什么?

最佳回答

见此:

correct fix is that you use the wrong string in the 显示 : http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_REPLACED

应该是"和机器人. 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+) .

显示 :

<?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>

我的播报Repeiver. 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();
    }
  }

请运行它,看它是否完美。


EDIT:如果你的应用程序是用于 API12 和以上, 并且只希望处理更新应用程序的个案, 您可以单独使用这个意图 :

< a href=>http:// developmenter.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED" rel=“noreferr'>http://developinger.android.com/reference/android/content/Intent.html#ACTION_MY_PACKAGE_REPLACED

问题回答

我把下面的接收器放进Adroidmanifest.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+ 用于 My_PACKAGE_ REPLED 。

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

应用程序必须由 Android Studio 或其他 IDE 启动 。 如果我手动安装应用程序及其电源, 从当前的 Android 设备中启动, 应用程序可以识别是否更新了我的应用程序, 并正确重新启动 。

我的用户案例是一个自定义的发射装置, 它可以自动更新, 启动一个 < code> PackageInstaller 。 Session 无需前往 Google Play Store 。

这里就是代码

明显:

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

更新接收器( 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)
        }

    }
}

After two days of struggle and experimentation, I found out the reason. It turned out I need to carefully read the official documentation.

如这里所说,