【Android】JobService的使用

在Android开发中,会存在这么些场景 : 你需要在稍后的某个时间点或者当满足某个特定的条件时执行一个任务
JobService介绍:This service executes each incoming job on a Handler running on your application's main thread. This means that you must offload your execution logic to another thread/handler/AsyncTask of your choosing. Not doing so will result in blocking any future callbacks from the JobManager - specifically onStopJob(android.app.job.JobParameters), which is meant to inform you that the scheduling requirements are no longer being met.

官方文档:https://developer.android.com/reference/android/app/job/JobService.html

示例:在接收到开机广播后延时5秒执行任务
1.在mainfest中声明:



    

    
        
            
                

                
            
        

        
            
                
                
            
        

        
    


2.声明广播

package com.example.sun.jobservicedemo;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.Toast;

import java.util.concurrent.TimeUnit;

/**
 * Created by sun on 10/03/2018.
 */

public class BootReceiver extends BroadcastReceiver {
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.e("aaa","boot complete");

        Toast.makeText(context,"boot complete", Toast.LENGTH_SHORT).show();
        doService(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void doService(Context context) {
        JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
        JobInfo.Builder builder = new JobInfo.Builder(1, new ComponentName(context, TestJobService.class));  //指定哪个JobService执行操作
        builder.setMinimumLatency(TimeUnit.MILLISECONDS.toMillis(5000)); //执行的最小延迟时间
        builder.setOverrideDeadline(TimeUnit.MILLISECONDS.toMillis(5010));  //执行的最长延时时间
        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);  //任何网络状态
        builder.setRequiresCharging(false); // 未充电状态
        jobScheduler.schedule(builder.build());
    }
}

3.声明JobService

package com.example.sun.jobservicedemo;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by sun on 10/03/2018.
 */

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class TestJobService extends JobService{
    @Override
    public boolean onStartJob(JobParameters params) {
        doJob();
        jobFinished(params, false);
        return true;
    }

    private void doJob() {
        Log.e("aaa","5秒");
        Toast.makeText(this,"5秒",Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        return false;
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("aaa","service destory");
    }
}

错误不足之处或相关建议欢迎大家评论指出,谢谢!如果觉得内容可以的话麻烦喜欢(♥)一下

你可能感兴趣的:(【Android】JobService的使用)