4

我有一个接收器类监听几个动作,但它无法捕捉到android.intent.action.BOOT_COMPLETED动作。我做错了什么?这是我的清单文件:

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <!--<receiver android:name=".OtherReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
    </receiver>-->
    <receiver android:name="com.myApp.AppReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
             <action android:name="android.intent.action.PACKAGE_ADDED"/>
             <action android:name="com.myApp.wifitimer"/>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" android:path="com.myApp" />
    </intent-filter>
    </receiver>

可以看出,我在接收器中再次添加了权限,并且接收器的名称得到了这个答案所暗示的类的全名。
这是广播接收器类:

@Override
public void onReceive(Context arg0, Intent arg1) {

    String action1 = arg1.getAction();

    if(action1.equals(Intent.ACTION_BOOT_COMPLETED)) {
        Log.d("receiver","action is: boot");
    }
    if(action1.equals("android.intent.action.PACKAGE_REPLACED")) {
        Log.d("receiver","action is: package");
    }
}

当我运行应用程序时,接收器会捕捉到,android.intent.action.PACKAGE_REPLACED但是当我重新启动手机时,接收器不会捕捉到BOOT_COMPLETED.
但是,当我在 Mainfest 文件中发表评论时,.OtherReceiver可以捕捉到它!
这是这个类的代码:

public class OtherReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {

    String action = arg1.getAction();

    if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
        Log.d("new receiver","action is: boot");
    }

}   
}

just the same as the other one. So my question is why I need define a separate receiver for the BOOT_COMPLETED action?
Edit: I also tried to send the action via adb according to this, and without any permission I could catch it with the AppReceiver class:

am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n com.blubuk/.AppReciever
4

2 回答 2

9

First, remove android:permission="android.permission.RECEIVE_BOOT_COMPLETED" from your <receiver> element.

Second, your <data> portion of your <intent-filter> is applying to all <action> elements within that <intent-filter>, which you do not want. There is no Uri on ACTION_BOOT_COMPLETED.

However, rather than creating a separate <receiver> element, you could just create a separate <intent-filter> element on your original <receiver> element. Move your <action android:name="android.intent.action.BOOT_COMPLETED" /> to the new <intent-filter> (and perhaps that com.myApp.wifitimer one too), so that they are unaffected by the <data> of your first <intent-filter>.

于 2013-03-30T21:41:29.880 回答
2

Replace your Manifest with this and I am sure it will work. Placing android:permission in the receiver tag was incorrect.

    <receiver
        android:name="com.myApp.AppReceiver"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
于 2016-07-18T05:50:23.267 回答