Why won't this broadcast receiver work in Lollipop?

倖福魔咒の 提交于 2019-11-28 09:16:13

问题


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.



来源:https://stackoverflow.com/questions/29081414/why-wont-this-broadcast-receiver-work-in-lollipop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!