Andorid Chinese api:keep the CPU on

Source: Internet
Author: User

If you want to keep the CPU running so that you can do some work before the device sleeps, you can call wake locks using PowerManager. Wake locks allows your app to control power state.

      Creating and maintaining wake locks can have a significant impact on the battery life of the device. So you should use it where you desperately need it, and use it for as short a time as possible. For example, you never need to use wake lock  in your activity. If you want to keep the screen on, please use flag_keep_screen_on.

A reasonable use case, the use of wake-up lock may be background service. It needs to keep the CPU running to perform some functions when the screen is off. Again, the use of this method should minimize the image of the battery life.

The first step in using Wake Lock: Request permission

<uses-permissionandroid:name= "Android.permission.WAKE_LOCK"/>

If your app contains a broadcast receiver, this broadcast uses a service to implement some functionality. You can pass the Wakefulbroadcastreceiver. This is a preferred scenario. If your app does not allow this scheme, you can set up a wakelock directly here.

powermanager powermanager =
(PowerManager) Getsystemservice (power_service);
wakelock WakeLock =
Powermanager.newwakelock (Powermanager.partial_wake_lock,mywakelocktag");
wakelock.acquire ();

Release Lock: Wakelock.release ()


Using Wakefulbroadcastreceiver

Manage the lifecycle of a background task by using a broadcast combined with a service.


A wakefulbroadcastreceiver is a special type of broadcast receiver that can create and manage the parital_wake_lock you apply. When a wakebroadcastreceiver receives a broadcast, it passes the work to the service (a typical intentservice) until it ensures that the device is not hibernating. If you do not keep the wake-up lock when handing over work to the service and allow the device to hibernate before the work is completed, there will be some situations that you do not want to see.

Wakefulbroadcastreceiver is a broadcast that primarily creates and manages a partial_wake_lock for your app. A wakefulbroadcastreceiver passes off the work to a Service (Typicallyan intentservice) while ensuring that the device is not in hibernation. If you don't hold a wake lock and transitioning the work to a service, you're effectively allowing the device to go BAC K to sleep before the work completes. The end result is that the application may not be able to finish doing the work until some point in the future, which is not what you want.


First: Add something to the manifest file:

<receiver android:name= ". Mywakefulreceiver "></receiver>

Next: Use Startwakefulservice () to start myint He entservice service. This method and StartService () are the same, except Wakefulbroadcastreceiver  holds the wake lock lock. A temporary lock was held by intent through Startwakefulservice ().


public class Mywakefulreceiver
extendswakefulbroadcastreceiver{

@Override
Publicvoid onreceive (Context context,intent Intent) {
//Start the service,
Keeping the device awake while the service is
Launching.
The Intent to deliver to the service.
Intent Service =newintent (Context,myintentservice.class);
Startwakefulservice (context, service);
}
}

Last: When the service is finished, it calls Mywakefulreceiver.completewakefulintent () to release the Wake Lock.

public class Myintentserviceextendsintentservice{
Publicstaticfinalint notification_id = 1;
Privatenotificationmanager Mnotificationmanager;
Notificationcompat.builder Builder;
Publicmyintentservice () {
Super ("Myintentservice");
}
@Override
Protectedvoid onhandleintent (Intent Intent) {
Bundle extras = Intent.getextras ();
requires your app to keep the CPU running.
// ...
Release the wake Lock provided by the Wakefulbroadcastreceiver.
Mywakefulreceiver.completewakefulintent (Intent);
}
}
Recommended for everyone: the Gospel of Android developers, the choice of conscience

Andorid Chinese api:keep the CPU on

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.