【问题标题】:Why MY_PACKAGE_REPLACED Android action is ignored by the content scheme为什么内容方案会忽略 MY_PACKAGE_REPLACED Android 操作
【发布时间】:2016-09-02 14:41:09
【问题描述】:

我在广播的清单中有一个意图过滤器声明。

       <intent-filter>
            <action android:name="android.intent.action.TIME_SET"/>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
            <data android:scheme="content"/>
        </intent-filter>

问题是当我删除 &lt;data android:scheme="content"/&gt; 时,收到 MY_PACKAGE_REPLACED 操作,否则不会收到。

这种情况下的数据标签是什么?从文档中无法真正理解。

【问题讨论】:

    标签: android broadcast intentfilter


    【解决方案1】:

    &lt;data&gt; 元素表示“Intent 上必须有一个Uri,并且它必须与&lt;intent-filter&gt;&lt;data&gt; 元素中提供的规则相匹配”。在您的特定情况下,规则是“Uri 必须存在并且它必须具有content 方案”。

    由于这三个广播都没有使用content Uri,因此删除&lt;data&gt; 元素。

    【讨论】:

      【解决方案2】:

      对于MY_PACKAGE_REPLACED,你只需使用这个:

      <receiver
        android:name=".UpgradeReceiver" android:enabled="true" android:exported="true">
        <intent-filter>
          <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
        </intent-filter>
      </receiver>
      
      
      
      public class UpgradeReceiver extends BroadcastReceiver {
      
          @Override
          public void onReceive(final Context context, Intent intent) {
              if (!Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction()))
                  return;
          ...  
          }
      }
      

      此外,请确保在禁用即时运行时运行应用程序,如 here 所述。我注意到如果启用它并不总是会收到...

      【讨论】:

      • 它在调试模式下运行,即调用了UpgradeReceiver onReceive。但是在发布 apk 中它不起作用。有什么建议吗?
      • @GiruBhai 应该可以。如果不是,要么是因为即时运行,要么是因为用户选择了“强制停止”或类似的。
      • 我用两个 release apk 测试过
      • 测试更好。例如,使用 log.e("something") 写入日志。我现在已经测试过了。运行良好,除非您从未启动过该应用。
      • 发布被保护,在你的 proguard 文件中保留 boradcastReceiver 的名称,它应该是第一个检查你的“没有在 prod 中运行”问题..
      猜你喜欢
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 2012-09-22
      相关资源
      最近更新 更多