broadcasting of BOOT_COMPLETED intent action does not work properly

11,752

Solution 1

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

Solution 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>
Share:
11,752
laplasz
Author by

laplasz

Updated on June 04, 2022

Comments

  • laplasz
    laplasz almost 2 years

    I have a receiver class listening on several actions but it can not catch the android.intent.action.BOOT_COMPLETED action. What I am doing wrong? here is my manifest file:

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

    as it can be seen I added the permission again inside the receiver and the name of the receiver gets the full name of the class as this answer suggests.
    here is the broadcast receiver class:

    @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");
        }
    }
    

    When I run the app the receiver catches the android.intent.action.PACKAGE_REPLACED but when I restart the phone the receiver does not catch the BOOT_COMPLETED.
    However when I comment in the .OtherReceiver in the Mainfest file it can catch it!
    here is the code of this class:

    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