Why won't this broadcast receiver work in Loll

2020-02-14 06:45发布

问题:

I have this broadcast receiver declared in the Manifest:

    <receiver android:name="classes.VoiceLaunchReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.USER_PRESENT" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

It does its thing whenever the user unblock and turns on the screen. It works perfectly in Jellybean 4.3 and lower. Why won't it work in Lollipop?

(I already know that the systems send that intent, what I want is to detect it with my receiver)

回答1:

Ok, I solved the issue by adding two things: a permission, and a category tag inside the receiver's intent Filter. Everything in the manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
        <receiver android:name="classes.VoiceLaunchReceiver" >        
             <intent-filter>
                <action android:name="android.intent.action.USER_PRESENT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

Sometime when I was trying to make my own project, I did not see that the original Android demo had the permission.