Android startservice automatically unlocks and lights up the screen

Source: Internet
Author: User

See a post, make notes, post in: http://topic.csdn.net/u/20110304/15/ae5279b1-692e-48a7-a5b9-0e579a3a6974.html

What I want to record is:

A service that provides the reminder function to automatically unlock and enable the screen.

Write an activity to start the service. Use a thread to start the service. It will only be effective if the service is started on a black screen.

Import Android. App. keyguardmanager;
Import Android. App. keyguardmanager. keyguardlock;
Import Android. App. Service;
Import Android. content. context;
Import Android. content. intent;
Import Android. OS. ibinder;
Import Android. OS. powermanager;
Import Android. util. log;

Public class screenservice extends Service {

// Declare the keyboard Manager
Keyguardmanager mkeyguardmanager = NULL;
// Declare the keyboard lock
Private keyguardlock mkeyguardlock = NULL;
// Declare the Power Manager
Private powermanager PM;
Private powermanager. wakelock;

@ Override
Public ibinder onbind (intent arg0 ){
Return NULL;
}
@ Override
Public void oncreate (){
// Obtain the power of the service
PM = (powermanager) getsystemservice (context. power_service );
// Obtain system services
Mkeyguardmanager = (keyguardmanager) getsystemservice (context. keyguard_service );
Super. oncreate ();
}
@ Override
Public void onstart (intent, int startid ){
// Bright screen
Wakelock = PM. newwakelock (powermanager. acquire_causes_wakeup | powermanager. screen_dim_wake_lock, "my tag ");
Wakelock. Acquire ();
Log. I ("log:", "------> mkeyguardlock ");
// Initialize the key lock to lock or unlock the key lock
Mkeyguardlock = mkeyguardmanager. newkeyguardlock ("");
// Disable display keyboard lock
Mkeyguardlock. disablekeyguard ();
}
@ Override
Public void ondestroy (){
Wakelock. Release ();
Super. ondestroy ();
}
}

Remember to add it to the androidmanifest file.
<! -- Unlock permission -->
<Uses-Permission Android: Name = "android. Permission. disable_keyguard"> </uses-Permission>
<! -- Screen Display permission -->
<Uses-Permission Android: Name = "android. Permission. wake_lock"> </uses-Permission>

 

 

 

 

When started with startservice, the screen will not be completely hacked. In the stop method of the activity, stopservice,
Then, release mwakelock. Release () in the destory method of the service ();
In this way, it will not always be on.

 

Although the screen is lit up, it is not as bright as it is normal. This makes the screen light up only a little bit, or very dark.

Public void acquire () // This method only ensures that the screen is awakened.
Makes sure the device is on at the level you asked when you created the wake lock.

Public void acquire (long timeout) // The wake-up time. The lock is automatically released after the time.
Makes sure the device is on at the level you asked when you created the wake lock. The lock will be released after the given timeout.

 

The API has a parameter to light up.

 

 

 

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.