【问题标题】:MY_PACKAGE_REPLACED stops working - How to find app upgrade androidMY_PACKAGE_REPLACED 停止工作 - 如何找到应用升级 android
【发布时间】:2021-12-28 10:05:40
【问题描述】:

我想找到我的应用程序的升级。所以我一直在使用这个代码来找到它 PACKAGE_REPLACED,但突然我无法接收到我的应用程序包替换的事件。

我改成了 MY_PACKAGE_REPLACED。还是同样的问题。

分析了一些堆栈溢出问题。没有运气。尝试了所有答案。

我的目标sdk版本是30:

清单代码:

<receiver android:name=".Receiver" android:enabled="true" android:debuggable="true" android:exported="true"
    tools:ignore="HardcodedDebugMode">
  <intent-filter>
    <action android:name="android.intent.action.PACKAGE_REPLACED" />

  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
  </intent-filter>
</receiver>

收货人代码

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Log.d(TAG, "action = " + action);

    if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
        Log.d(TAG, "BOOT COMPLETED, Ping start...");
    } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
        Log.d(TAG, "PACKAGE REPLACED, upgrade ping...");
    } else {
        //default action is network changed
        Log.d(TAG, "network status changed...");
    }
}

【问题讨论】:

  • "突然我无法接收事件" - 你是在暗示你之前有工作代码吗?
  • @Pawel 对不起,以前我使用这个事件“android.intent.action.PACKAGE_REPLACED”现在它失败了。我更改为 MY_PACKAGE_REPLACED。但仍然未能获得事件。
  • 更新了我的问题。 @Pawel
  • 我不明白你的句子。这个接收器有没有工作过?
  • 是的。 Action_PACKAGE_REPLACED 正在工作,但现在不行。 @Pawel

标签: android android-intent android-sdk-tools target-sdk


【解决方案1】:

在你的接收器中,你应该改变这个:

Intent.ACTION_PACKAGE_REPLACED.equals(action)

为此:

Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)

【讨论】:

  • 对不起,这也行不通。
  • 你的意思是你的清单和接收器中有 MY_PACKAGE_REPLACED 但你仍然没有收到事件吗?
  • 我发现了我的错误并添加为上面的答案。感谢您的回复。
【解决方案2】:

在分析了更多 stackoverlfow 问题/答案和 cmets 后回答自己。

以前,我使用 PACKAGE_REPLACED 事件来接收更新,当我们收到所有包替换的此事件时,我添加了包检查。

但是突然或者我现在注意到了,应用程序停止接收 PACKAGE_REPLACED 事件。

stackoverflow 的回答说,我们应该使用 MY_PACKAGE_REPLACED 。 这不会提供任何额外的数据。

另外,我已将其替换为 MY_PACKAGE_REPLACED 并通过从 Android Studio 运行的普通 apk 进行了尝试。 这是我犯错的地方。

看来,我们应该使用adb命令运行,然后才触发包替换事件

./adb install /Users/vijay/desktop/android/myapp.apk

完整代码:

清单:

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

收货人代码

class MyPackageReplacedReceiver : BroadcastReceiver() {
     override fun onReceive(context: Context, intent: Intent) {
       Log.d(TAG, "package replaced event")
     }
}

【讨论】:

    猜你喜欢
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2021-03-20
    相关资源
    最近更新 更多