The secret of Android power saving: jobscheduler

Jobscheduler is a newly introduced API in Android l version. Jobscheduler, as its name suggests, is used to schedule work. The conditions under which work is scheduled include network change, charging, plugging, periodic execution, etc. Usage scenarios include data download and upload under WiFi conditions. Why did Google introduce this new API? It is a specification formulated to save power. Think about it. If every developer uses this API to upload data under WiFi network, the data upload operation will be unified to the same point in time for batch processing, which saves much more power than many applications wake up alone.

Here is a small example

Primary mainactivity

builder.setrequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED); It stands for free Internet, usually referring to WiFi

Jobs to be dispatched

When WiFi is available, the demoservice will execute onstartjob

detailed list

onStartJob.png

The onstartjob function often has two scenarios

1. No time-consuming operation. In this case, you should return false

2. For time-consuming operations, such as data download, you should start a new thread (because jobservice runs on the main thread) and return true

What if the scheduling task fails to execute?

There are many cases of task failure, such as download failure, for example, the WiFi is broken during the download process.

No problem, Google provides a convenient way to reschedule.

For example, if the WiFi is broken during the download process, the jobservice will call back the onstopjob function, which only needs to set the return value of the function to true. When WiFi reconnects, jobservice will call back onstartjob function again.

What if the download fails, such as the asynctask execution failure in the above example? We only need to execute jobfinished (mjobparameters, true) in onpostexecute of asynctask. Here, true means that the task should be rescheduled when the WiFi conditions are met again. The classic writing is as follows.

Start scheduling

Stop scheduling

What if the scheduled task always fails to execute?

In order to save power, the failed task must be delayed for a period of time when the execution conditions are met. And as the number of failures increases, the delay will be longer. For example, a task that fails because the WiFi is disconnected will not be executed immediately after the WiFi is connected. It can only be executed after a period of time.

Delay time = 30s * number of failures.

The following figure is the sequence diagram of jobscheduler in the framework layer. In the next chapter, you will use ADB instructions to visually view the situation of jobscheduler, which should not be missed. You can leave a message if you have any questions. Let's discuss it together!

Jobscheduler internal timing chart

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>