In the program, you want to receive the broadcast sent when the mobile phone is locked. This broadcast is a bit special. You cannot listen when registering the broadcast in androidmanifest. XML, so you need to register it with code in the program:
Public classLockscreen
ExtendsBroadcastreceiver {
Private
Static finalStringTag=
"Lockscreen ";
Private
BooleanIsres =False;
@ Override
Public
VoidOnreceive (context, intent ){
Log.I(Tag, "Screen lock broadcast ...");
}
// Register screen lock Broadcast
Public
VoidRegisterreceiver (context ){
If(! Isres ){
Intentfilter filter =New
Intentfilter (intent.Action_screen_off);
Context. registerreceiver (lockscreen.This, Filter );
Isres =True;
}
}
// Anti-register Broadcast
Public
VoidUnregisterreceiver (context ){
If(Isres ){
Context. unregisterreceiver (lockscreen.This);
Isres =False;
}
}
}
After writing this class, go to androidmanifest. declare the broadcast receiver in XML, and then call the registerreceiver (context) method in this class to register the broadcast, and call unregisterreceiver (contextcontext) then you can register the broadcast.