Failed to receive screen lock broadcast in Android Development

Source: Internet
Author: User

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 final
StringTag=
"Lockscreen ";

Private
Boolean
Isres =False;

 

@ Override

Public
Void
Onreceive (context, intent ){

Log.I(Tag, "Screen lock broadcast ...");

 

}

// Register screen lock Broadcast

Public
Void
Registerreceiver (context ){

If(! Isres ){

Intentfilter filter =New
Intentfilter (intent.Action_screen_off);

Context. registerreceiver (lockscreen.This, Filter );

Isres =True;

}

}

// Anti-register Broadcast

Public
Void
Unregisterreceiver (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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.