App power consumption optimization three use jobschedule to reasonable scheduling of tasks

Source: Internet
Author: User

JobSchedule is added after Android5.0, the previous version did not.
The JobSchedule principle is a programmatic mechanism that arranges tasks in an appropriate and practical way. The optional timing is provided, as follows:
1 is performed under Available networks. Before 7.0, applications could perform tasks by listening to network changes, but only if the application had to survive. After 7.0, such APIs have been invalidated, but the jobschedule mechanism provides monitoring of network changes. Further, you can perform certain operations in the event of a network change. For example, in the WiFi environment to perform download tasks.
2 The device performs some tasks when charging or idle.
3 Set the time for execution, which is used together with other conditions.
The specific implementation is as follows:
1 Task Scheduling

FinalJobscheduler Scheduler = Context.getsystemservice (Jobscheduler.class); //or    FinalJobscheduler Scheduler =(Jobscheduler) Getsystemservice (Context.job_scheduler_service); //If the current Jobid is already scheduled, then cancel the arrangement.    Final intJobId = (int) Info.mid;    Scheduler.cancel (JOBID); //build Jobinfo jobId for JobId. Downloadjobservice service that is accepted by the job, the service must inherit Jobservice    FinalJobinfo.builder Builder =NewJobinfo.builder (JobId,NewComponentName (context, Downloadjobservice.class)); //setting up a device restart is doing this task. The premise is that you need to have receive_boot_completed permissionsbuilder.setpriority (Jobinfo.priority_foreground_app); //This setting does not set up as a foreground process. Notifications also need to be applied to their own hair. Additionally, this setting ignores the network limit for the task. builder.setflags (Jobinfo.flag_will_be_foreground); //set task delay execution time, not to be used in conjunction with Setperiodic (long)builder.setminimumlatency (time); //set network conditions for device execution jobinfo.network_type_unmetered non-metered network (WiFi), jobinfo.network_type_not_roaming non-roaming network, Network_type_ Any network//Jobinfo.network_type_none Whether or not a network is executedBuilder.setrequirednetworktype (jobinfo.network_type_unmetered); //set to execute when the device is chargingBuilder.setrequirescharging (true); //settings are performed during device idle timeBuilder.setrequiresdeviceidle (true); //Loop does not execute once in 5 secondsBuilder.setperiodic (5000); //The condition in the agreed time is not triggered, it starts in 5 seconds.Builder.setoverridedeadline (5000); //Build JobJobinfo job =Builder.build (); //Schedule Job, the method has a return value jobscheduler.result_success indicates a scheduled success, jobscheduler.result_failure scheduling failedscheduler.schedule (Job)//to schedule a job, PackageName represents the job that the application is scheduled to use (which consumes the app). UserID indicates who scheduled the jobScheduler.scheduleaspackage (Builder.build (), PackageName, Userhandle.myuserid (), TAG);

2 processing tasks.
When the task is scheduled, it should be recorded in the Jobscheduler service. When the condition is met, the processing job corresponding Jobservice will be started, otherwise ...
Define Jobservice First

<service        android:name= ". Downloadjobservice "        android:exported=" true "  // This place must be true, otherwise external applications cannot start the Jobservice         // must define this permission, you know    />
 Public classDownloadjobserviceextendsJobservice {//begins execution of the job interface. (must be implemented) if return false indicates that the job has been executed. If true indicates that the job is being executed.          Public Booleanonstartjob (jobparameters params) {Final intID =params.getjobid (); .... Start Job Execution1 for a task that can be completed in a moment, return herefalse. 2if it is a time-consuming task, it needs to be executed in an asynchronous thread and returns True.            Furthermore myhandler.removemessages (ID);            Myhandler.sendemptymessage (ID); Do not forget to execute jobfinished} after execution//job execution is stopped and must be implemented when the task is canceled, if the task is not ended, the method is executed, otherwise it is not executed.          Public Booleanonstopjob (jobparameters params) {//of course, the canceled task can be rescheduled here. scheduler.schedule (Job)}//There is one more way. Jobfinished (Jobparameters params, Boolean needsrescheduled) This method notifies the system when the task execution is complete (does not mean that the task was executed successfully).        Needsrecheduled indicates whether the task has been repeatedly executed. //For example, the Onstartjob execution result is true. The task is actually still executing. At this time, if the task is finished. You must call this method or the subsequent task.             }

In summary, Scheduler is a set of tasks to provide developers with an optimized power consumption task, or quite good.

App power consumption optimization three use jobschedule to reasonable scheduling of tasks

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.