【问题标题】:Can't receive broadcasts for PACKAGE intents无法接收 PACKAGE 意图的广播
【发布时间】:2009-10-20 07:41:50
【问题描述】:

我正在尝试注册一个广播接收器来接收包事件的广播事件。以下是清单文件中的代码和我的接收器。日志语句永远不会发生,但我可以清楚地看到“HomeLoaders”(启动器)调试语句的相同广播触发。我错过了什么?

public class IntentListener extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.i("INTENT LISTNER:", intent.getAction());
    }
}

<receiver android:name="IntentListener" android:enabled="true" android:exported="true">
    <intent-filter>
        <data android:scheme="package"></data>
        <action android:name="android.intent.action.PACKAGE_ADDED"></action>
        <action android:name="android.intent.action.PACKAGE_ADDED"></action>
        <action android:name="android.intent.action.PACKAGE_CHANGED"></action>
    </intent-filter>
</receiver>

【问题讨论】:

  • 错误可能在 IntentListener 而不是 com.android.samples.app.IntentListener ?

标签: android


【解决方案1】:

这些Intents 可能无法被清单中注册的组件接收,而只能被通过registerReceiver() 在Java 中注册的接收者接收。

【讨论】:

  • 是的,我很确定是这样,出于好奇,为什么会这样?我看不出这有任何安全隐患?
  • 正如我大约一天前在另一个 SO 问题上评论的那样,Android 不一定要一直启动一个新组件。我知道的一种情况是电池事件(例如 ACTION_BATTERY_LOW)。看起来 SCREEN_OFF(也许 SCREEN_ON)是其他的。如果你想到它,并且你让它与 registerReceiver() 一起工作,请回到这个问题上。我想我需要在博客文章和/或书籍部分中介绍这个主题,不幸的是,没有清单接收者意图的列表没有记录。
  • 我想看看这是在哪里记录的。我已经检查了源,对这些受保护广播的唯一检查是非系统进程无法启动广播。我还仅通过清单在源寄存器中看到了此意图的其他包。
【解决方案2】:

这三个意图即,

Intent.ACTION_PACKAGE_ADDED
Intent.ACTION_PACKAGE_REMOVED
Intent.ACTION_PACKAGE_CHANGED

当系统播出时,有

Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT

添加了标志,以便只有注册的接收器将接收广播,并且不会启动广播接收器组件。有关详细信息,请参阅 Intent 和 PackageManagerService 源类。

【讨论】:

    【解决方案3】:

    这是我的清单,没有

    <category android:name="android.intent.category.DEFAULT" />
    

    我的应用仅检测到 Android Market 应用安装,但不会删除。现在它还接收非 Android Market 应用广播。

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SomeActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
        <receiver android:name="com.som.pakage.PackageInstallReceiver" >
            <intent-filter >
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
    
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="package" />
            </intent-filter>
        </receiver>
    </application>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多