Extended battery life-sensor, WakeLock

Source: Internet
Author: User
Tags xml attribute

A. Sensor

Sensor usage is similar to location services: Apps register listeners with specific sensors for update notifications.

private void Registerwithaccelerometer () {Sensormanager sm= (sensormanager) getsystemservice (Context.sensor_service) ; List<sensor> sensors=sm.getsensorlist (Sensor.type_accelerometer); if (sensors!=null&&! Sensors.isempty ()) {Sensoreventlistener listener=new Sensoreventlistener () {@Overridepublic void onsensorchanged ( Sensorevent event) {//TODO auto-generated method stub} @Overridepublic void Onaccuracychanged (sensor sensor, int accuracy {//TODO auto-generated method Stub//dosomthing ();}}; Sensor sensor=sensors.get (0); Sm.registerlistener (Listener, Sensor,sensormanager.sensor_delay_normal);}}
As with positioning, Android can also allow the application to specify what frequency to get the sensor updates, location service is milliseconds, can only specify one of 4 values: Sensor_delay_normal,sensor_delay_ui,sensor_delay_game, Sensor_delay_fastest.

Disables the sensor's listener when no notification is required. Use the Sensormanager Unregisterlistener () method to achieve this goal.


Two. WakeLock

In some cases, some applications will prevent the device from going into hibernation, even if the user has no device interaction for a long time, to maintain a good user experience. For example, when a user watches a video or movie on a device, this situation requires the CPU to decode the video, while the screen remains open, allowing the user to view it. In addition, the screen cannot be dimmed while the video is playing.

Android has designed the Wakelock class for this scenario:

private void Runinwakelock (Runnable Runnable, int flags) {PowerManager PM = (powermanager) getsystemservice ( Context.power_service); Powermanager.wakelock WL = Pm.newwakelock (Flags, "My WakeLock"); Wl.acquire (); Runnable.run (); Wl.release ();}
Note: The app requires Wake_lock permissions to use.

The behavior of the system depends on the tag (flags) parameter of the Wakelock object being created. Defined as follows:

(1) Partial_wake_lock (CPU Open)

(2) Screen_dim_wake_lock (CPU open, dark color display)

(3) Screen_bright_wake_lock (CPU open, bright display)

(4) Full_wake_lock (CPU on, bright display, keyboard open)

These tags can be used in conjunction with

(1) Acquire_causes_wakeup (open screen and keyboard)

(2) On_after_release (Wakelock after release to keep screen and keyboard open for a moment)

Prevent problems from appearing:

Using the Wakelock.acquire () version with a timeout, it releases wakelock after the time limit is exceeded. For example, an app that plays a video can use the video length as the Wakelock time-out.

If you use a flat screen that is associated with the view in activity, you can also use the XML attribute Android:keepscreenon in the layout file. The advantage is that you don't have to risk forgetting to release wakelock and leave it to the system to handle it.



Extended battery life-sensor, WakeLock

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.