I am creating a class which uses broadcast receiver. I want to receive the broadcast on unlocking of the phone. But there is some issue. Please help me out.
My Manifest.xml is :-
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
and my Broadcast reciever class :-
public class MyReiever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
Though other application and services are receiving broadcast for "Screen_on" and "USer_Present" eg. WifiService.
Check your Class name, that is extending BroadcastReceiver. It should be "MyReciever" not "MyReiever"
I can only give you a quick tip as i have gone through that path you are following with much less success. Try to read logcat using java.util.logging so that you will not require permission to read logs. And in log view create listener for the one containing "system disable" as its header. it fires up both at lock and unlock. check for the one that gives access to system.android not the other screen. Hope it helps. Best of luck
Although the Java constants are
android.content.intent.ACTION_USER_PRESENT
,android.content.intent.ACTION_BOOT_COMPLETED
, andandroid.content.intent.ACTION_SCREEN_ON
, the values of those constants areandroid.intent.action.USER_PRESENT
,android.intent.action.BOOT_COMPLETED
, andandroid.intent.action.SCREEN_ON
. It is those values which need to appear in your manifest.Note, however, that a receiver for
ACTION_SCREEN_ON
can not be declared in a manifest but must be registered by Java code, see for example this question.Since implicit broadcast receivers are not working as of Android 8.0, you must register your receiver by code and also in the manifest.
Do these steps:
To make your broadcast work, you have to register it in service. And to keep your service alive you can use tools like
alarm managers
,jobs
, etc which is not related to this question.Beside Typo that mentioned in earlier answers and the fact that the receiver class package name should be completely mentioned in
receiver
tag, I think the problem is that the code in question uses two nested intent filters. I believe this code will work correctly: