【问题标题】:How to make my app receive broadcast when other applications are installed or removed如何在安装或删除其他应用程序时让我的应用程序接收广播
【发布时间】:2012-06-30 01:44:30
【问题描述】:

我想制作一个可以在设备上安装或删除其他应用程序时接收广播的应用程序。

我的代码

在清单中:

<receiver android:name=".apps.AppListener">
    <intent-filter android:priority="100">
         <action android:name="android.intent.action.PACKAGE_INSTALL"/>
         <action android:name="android.intent.action.PACKAGE_ADDED"/>  
         <action android:name="android.intent.action.PACKAGE_REMOVED"/>
    </intent-filter>
</receiver>

在 AppListener 中:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AppListener extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    // TODO Auto-generated method stub
    Log.v(TAG, "there is a broadcast");
    }
}

但我无法接收任何广播。我认为这个问题是由于应用权限造成的,有什么想法吗?

感谢您的帮助。

【问题讨论】:

    标签: android permissions broadcastreceiver


    【解决方案1】:

    在您的清单中:

    <receiver android:name=".apps.AppListener">
        <intent-filter android:priority="100">
             <action android:name="android.intent.action.PACKAGE_INSTALL"/>
             <action android:name="android.intent.action.PACKAGE_ADDED"/>  
             <action android:name="android.intent.action.PACKAGE_REMOVED"/>
        </intent-filter>
    </receiver>
    

    在intent-filter标签前添加一行

    <data android:scheme="package"/>
    

    所以你的清单应该是这样的:

    <receiver android:name=".apps.AppListener">
        <intent-filter android:priority="100">
             <action android:name="android.intent.action.PACKAGE_INSTALL"/>
             <action android:name="android.intent.action.PACKAGE_ADDED"/>  
             <action android:name="android.intent.action.PACKAGE_REMOVED"/>
             <data android:scheme="package"/> 
        </intent-filter>
    </receiver>
    

    我不确定 PACKAGE_REMOVED 意图是否真的可用。

    【讨论】:

    • @rup35h arg1.getDataString() 将像“package:com.google.joke”一样返回“package:TheInstalledPackageName”
    • PACKAGE_INSTALL 无用、已弃用且 Google 从未使用过:developer.android.com/reference/android/content/…
    • 它对我不起作用。我的意图过滤器中有多个操作。其中,PACKAGE_FULLY_REMOVED 正确触发了我的接收器的 onCreate,但是,PACKAGE_ADDED 根本没有。我的应用程序已经处于运行状态,我已经为接收器 export="true" 并且根据您的建议包含了 。我在这里错过了什么吗?
    • 如何触发 my_app onReceive() 如果安装或卸载了任何新应用程序,即使我的应用程序没有运行也请帮助我,如果应用程序正在运行,安装和卸载如果应用程序没有运行,则会触发 onreceive听。
    【解决方案2】:

    您必须删除 android.intent.action.PACKAGE_INSTALL,因为它已被弃用且不再推荐,因为它仅适用于系统。其他一切都是完美的,我建议不要使用 100,而是使用 999,文档没有给出使用的最大或最小数量,数字越大,接收器的优先级越高。对不起翻译。我用西班牙语说话和写作。 Information

    <receiver android:name=".apps.AppListener">
    <intent-filter android:priority="999">
         <action android:name="android.intent.action.PACKAGE_ADDED"/>  
         <action android:name="android.intent.action.PACKAGE_REMOVED"/>
         <data android:scheme="package"/> 
    </intent-filter>
    

    【讨论】:

      【解决方案3】:

      很好的答案,只剩下一件小事:

      在每次应用更新时,首先调用 ACTION_PACKAGE_REMOVED,然后调用 ACTION_PACKAGE_ADDED——如果您希望忽略这些事件,只需将其添加到您的 onReceive() 中即可:

      if(!(intent.getExtras() != null &&
          intent.getExtras().containsKey(Intent.EXTRA_REPLACING) &&
          intent.getExtras().getBoolean(Intent.EXTRA_REPLACING, false))) {
      
          //DO YOUR THING
      }
      

      这是来自文档:

      在 API 级别 3 中添加的 EXTRA_REPLACING 字符串 EXTRA_REPLACING 用作 ACTION_PACKAGE_REMOVED 意图中的布尔额外字段表示 这是一个替换包,所以这个广播将 紧随其后的是不同版本的添加广播 同一个包。 常量值:“android.intent.extra.REPLACING”

      【讨论】:

      • 我希望我的广播仅在安装或卸载应用程序时触发事件,而不是在更新应用程序时触发事件。如何处理?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多