2009-10-20 105 views
5

我想注册一个广播接收器接收包事件的广播事件。以下是清单文件中的代码和接收器。日志统计不会发生,但我可以清楚地看到“HomeLoaders”(Launcher)调试语句的相同广播触发。我错过了什么?无法接收广播包的意图

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> 
+1

错误可以在IntentListener而不是com.android.samples.app.IntentListener中? – Zorb 2011-11-24 18:59:47

回答

4

这是可能的,这些Intent s不能在清单注册的组件被接收,但只有通过registerReceiver()在Java中注册的接收器。

+0

是啊,我很确定这是这种情况,出于好奇,为什么是这种情况?我没有看到这个安全问题? – 2009-10-20 18:37:31

+3

正如我在大约一天前评论过另一个SO问题时,Android并不一定希望始终创建一个新组件。我知道的一个例子是电池事件(例如,ACTION_BATTERY_LOW)。它看起来像SCREEN_OFF(也可能是SCREEN_ON)是其他的。如果你想到它,并且使它与registerReceiver()一起工作,请回顾这个问题。我认为我需要在博客文章和/或书籍部分讨论这个话题,不幸的是,没有任何清单的接收者意图列表没有记录。 – CommonsWare 2009-10-20 21:10:28

+1

我想看看这是记录在哪里。我已经检查过来源,并且针对这些受保护广播的唯一检查是非系统进程无法启动广播。我还通过清单仅在源注册中看到了此意图的其他包。 – James 2009-10-21 13:40:42

0

这是我的表现,没有

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

这三个意图,即

Intent.ACTION_PACKAGE_ADDED 
Intent.ACTION_PACKAGE_REMOVED 
Intent.ACTION_PACKAGE_CHANGED 

时由系统广播,有添加

Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT 

标志,以便只有注册接收机将接收广播并没有广播接收机组件将启动。有关更多详细信息,请参阅Intent和PackageManagerService类的源代码。