Android background scheduling task and provincial power detailed _android

Source: Internet
Author: User
Tags creative commons attribution joins

I. Handler:

Effective use during the course of survival, Google official recommended.

Simple and easy to use.

Stable and efficient.

II. Alarmmanager:

Use the system-level alarm clock service (holds wake Lock).
This is the best choice if you need an accurate timing task.

1. function

Run/Repeat the specified task at approximate intervals.

Specify the exact time interval to perform the task.

2. Characteristics

After registration, whether or not your application process exists/component exists, it will execute normally.

All registered alarm service will be reset after the system reboot, so if you need to ensure the task, you need to register receive_boot_complete, to ensure that after the restart, you can again register the task to the alarm clock service.

Alarmmanager deals with a pendingintent, so it's usually a service that starts a process transaction.

3. Notes

The official does not recommend network requests related to the use of Alarmmanager.

Considering the loss of electricity, it is recommended to use the approximate time in a special case, so Android will try to get several tasks packaged together to prevent frequent calls to the phone.

Iii. Job Scheduler:

    1. Jobscheduler Official documents
    2. It is recommended that network-related tasks be placed in job Scheduler.
    3. After the system restarts, the task remains in the job scheduler.
    4. Only in API21 or above system support

1. Advantages

    1. More energy savings
    2. More efficient
    3. More usable

2. Explicit designation of specific scenario execution (Jobinfo):

Because multiple tasks are packaged in one scenario, there is a slight delay in execution, and there are deadlines, and if certain conditions are not met within the deadline, the system joins the queues and executes them later.

    1. Device starts charging
    2. Free
    3. Connect to the Network
    4. Disconnect Network

3. Interface type

Boolean onstartjob (Jobparams params) {
  //Start execution
  //Note This method is performed on the main thread, and if it is time-consuming, throw it into the standalone thread
  //jobfinished ( Jobparameters params)//After completing the task and deciding whether more tasks need to be performed regularly
  //return is on independence now there are transactions to perform
}

void Onstopjob () {
  / Used to clean up the data and be recalled after the task is finished.
}

Iv. GCM

GCM Netwrok Manager actually uses the Job Scheduler in Api 21 or above, and the previous version uses the Google Play service in the real

The function of the current job scheduler.
There are a lot of rules in the Gcmnetworkmanager that are good for saving points.

1. Interface type

Create a task through Oneofftask.builder () and Periodictask.builder ().

Gcmtaskservice#onruntask (Taskparams params) is performed in a background thread.

The trigger scene is the same as in Jobinfo.

V. Sync Adapter

Transferring Data Using Sync adapters

It is usually used to synchronize more data.

Maybe this is a better alternative to job Scheduler API 21 ago.

Synchronizes the data on the server side with the local device.

1. Characteristics

    1. facilitate large data synchronization.
    2. No need to rely on Google Play Service.
    3. Power-saving stability.
    4. Users can proactively view the timing of synchronization through settings, trigger synchronization, or turn off synchronization.
    5. API 7 or more.

2. Notes

You can bind an account.

A database that provides contentprovider and synchronizes with the server.

Synchronization is triggered only when there is a network.

2. Trigger synchronization in a certain scenario

As much as possible, pack all the tasks that need to be synchronized in a single cycle to save as much battery power as possible.

Service-side/device-side data has changed.

When the phone is idle.

One day.

If the synchronization fails, it is placed in the queue where synchronization failed, synchronizing as much as possible.

VI. Doze Mode

Deep Doze Mode

In API 23, it is called doze Mode directly.

Regardless of the target SDK, this mode is enabled as long as the device is Android API 23 or above.

1. Characteristics

AIM: To reduce the consumption of mobile phone as much as possible after the user leaves the device.

Developers do not need to make special adaptations, but they affect all of the schedule methods mentioned above (Job Scheduler, Alarmmanager, syncs Adapter).

The task request is packaged by moving the window, and the time interval becomes longer.

2. Entry Conditions

will take effect for a period of time (approximately 30 minutes) after the following conditions are met:

The phone's not charging.

Screen is closed

Cell phones remain stable on all sides

The exit condition is that any conditional state in the entry condition changes.

3. Mobile status between two processing windows

    1. Deny network access to all applications.
    2. All Jobscheduler, Sync-adapter, Alarmmanager Tasks will be deferred to the window for execution.
    3. The system will reject all wake-lock from the application
    4. Stop all WiFi and GPS scans
    5. Reduce location events from devices to detect WiFi hotspots.

Light Doze Mode

API 24 or more will enable this mode

1. Characteristics

Compared to deep Doze Mode, the frequency of packing tasks is higher

2. Entry Conditions

A colleague will be in effect for a period of time (about a few minutes) after meeting the following:

The phone's not charging.

Screen is closed

In a stable state/unstable state

or in the following conditions:

In deep Doze Mode

Screen off

The phone's not charging.

Cell phone is no longer in stable condition

3. Exit conditions

Screen Open

Cell phone starts charging

Enter Deep Doze Mode

4. Mobile status between two processing windows

Deny network access to all applications.

All Jobscheduler and Sync-adapter tasks will be deferred to the window for execution.

The tasks in Alarmmanager are not affected, but there will be no network access (if your task requires network access, it is time to switch to Jobscheduler or sync-adapter to ensure that there is a network in the task window)

Interrupt/Avoid Doze

All of the following, Google officials suggest not to use special scenarios, because of the interruption of the rule of saving electricity.

1. Alarmmanager

Specifies the event that requires exact time: Setandallowwhileidle (), Setexactandallowwhileidle (). However, no network access restrictions are not lifted during non windows, and only 10s of time is available for processing.

The event that specifies the alarm event alarmmanager.setalarmclock () will cause the system to completely exit the Doze mode briefly before the end of the alarm clock, and handle the event normally, and the system will display the icon of the physical alarm clock on the status bar to highlight the alarm event.

2. FCM/GCM

(firebase Cloud messaging, called Google Cloud Messaging (GCM) in the old version.)
FCM/GCM in the High priority task configuration ("Priority": "HI") message, in the doze mode can be normal in time to arrive.

3. White List

White List Official documents

Official proposal to consider the inclusion of the white list

The active request joins the white list, the user joins the white list after the same;

Users can also take the initiative to remove or add apps from the whitelist;

Applications can be isignoringbatteryoptimizations () to see if they are in the whitelist;

Whitelist applications can access the network and hold valid Wakeloke, but other doze constraints still exist (such as deferred job Scheduler, Syncs-adapter, Alarmmanager);
White

How to request the list:

By action_ignore_battery_optimization_settings the Power Optimization page, users can use the search to turn off the application's power optimization to add the whitelist.

Hold the Request_ignore_battery_optimizations permission first, and then start the Intentaction_request_ignore_battery_ Optimizations Direct Eject dialog allows the user to turn off the application's power optimization to add a whitelist.

4. Special Circumstances

The front desk service (Foreground-service) will not be affected by the doze mode.

Doze mode test

Google officials have provided some ADB commands to test the doze pattern rather than waiting to enter the Doze mode.

1. Enter Doze mode

Prepare a system that is in the Android nougat devloper Preview4 or above version of the device.

Connect it to your computer.

The ADB shell dumpsys Battery Unplug command allows the device to enter a disconnected charging mode.

Forced into doze mode through adb shell Dumpsys deviceidle step [Light|deep].

Exit Doze mode, let the phone return to normal need to reset the charging mode: adb shell dumpsys battery reset.

2. Other Directives

Gets the device status adb Shell Dumpsys deviceidle get [light|deep|force|screen|charging|network].

In the Android nougat Developer Preview 4, the state cycle of the doze mode is:

Light:active-> IDLE-> idle_maintenance->
Deep:active-> idle_pending-> Sensing-> locating-> IDLE-> idle_maintenance

Choosing the right Background Scheduler in Android

Diving into Doze Mode for developers

©2012–2016, Jacksgong (blog.dreamtobe.cn). Licensed under the Creative Commons attribution-noncommercial 3.0 license (this license lets others, remix, and Tweak D upon a work non-commercially, and although their new works must also acknowledge the original author and is Non-commerci Al, they don ' t have to license their derivative works on the same terms). http://creativecommons.org/licenses/by-nc/3.0/

The above is on the Android back-Office scheduling tasks and power-saving data, there is the development of Android applications friends can refer to, thank you for the support of this site!

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.