01/10/2018, 01:14

Vấn đề trong việc quản lí nhiều Jobs với JobScheduler

Các bác cho em hỏi là làm sao đề mình có thề chạy nhiều Job trong JobScheduler. Khi em code ntn thì nó cứ kêu và chạy cả hai cùng một lúc ( theo cái 1 ) @@

Main

ComponentName componentName = new ComponentName(getPackageName(),JobService.class.getName());
final JobScheduler myjobJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
final JobInfo builder1 = new JobInfo.Builder(1,componentName).setPeriodic(3000).setPersisted(true).build();
final JobInfo builder2 = new JobInfo.Builder(2,componentName).setPeriodic(10000).setPersisted(true).build();

    btstart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myjobJobScheduler.schedule(builder1);
            myjobJobScheduler.schedule(builder2);
        }
    });
    btstop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Stop","stop");
            myjobJobScheduler.cancelAll();
        }
    });

Jobservice

public class JobService extends android.app.job.JobService {
@Override
public boolean onStartJob(JobParameters params) {
Log.d(“param jobinfo id”,params.getJobId()+"");
myjopHander.sendMessage(Message.obtain(myjopHander,params.getJobId(),params));
return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    myjopHander.removeMessages(params.getJobId());
    Log.d("Đã dừng","Dừng "+params.getJobId());
    return false;
}
private Handler myjopHander = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        Toast.makeText(getApplicationContext(),"Da chay "+((JobParameters)msg.obj).getJobId(),Toast.LENGTH_SHORT).show();
        jobFinished((JobParameters) msg.obj,true);
        Log.d("RUn ","run "+((JobParameters)msg.obj).getJobId() );
        return false;
    }
});

}

Khi em bấm start thì nó chạy cả 2 cái cùng lúc ( mà cái thừ 2 m để sau 10 s lận mà )
Logcat:

01-09 15:55:55.045 9091-9091/com.cylasion.jobshedulertest D/param jobinfo id: 2
01-09 15:55:55.062 9091-9091/com.cylasion.jobshedulertest D/param jobinfo id: 1
01-09 15:55:55.095 9091-9091/com.cylasion.jobshedulertest D/RUn: run 2
01-09 15:55:55.141 9091-9091/com.cylasion.jobshedulertest D/RUn: run 1
01-09 15:55:55.321 9091-9190/com.cylasion.jobshedulertest W/EGL_emulation: eglSurfaceAttrib not implemented
01-09 15:55:55.321 9091-9190/com.cylasion.jobshedulertest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f78c0c9cd80, error=EGL_SUCCESS
01-09 15:55:57.196 9091-9190/com.cylasion.jobshedulertest W/EGL_emulation: eglSurfaceAttrib not implemented
01-09 15:55:57.196 9091-9190/com.cylasion.jobshedulertest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f78c0c9cdc0, error=EGL_SUCCESS
01-09 15:56:25.229 9091-9091/com.cylasion.jobshedulertest D/param jobinfo id: 2
01-09 15:56:25.234 9091-9091/com.cylasion.jobshedulertest D/RUn: run 2
01-09 15:56:25.403 9091-9190/com.cylasion.jobshedulertest W/EGL_emulation: eglSurfaceAttrib not implemented
01-09 15:56:25.403 9091-9190/com.cylasion.jobshedulertest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f78c0c9cd80, error=EGL_SUCCESS
01-09 15:56:25.454 9091-9091/com.cylasion.jobshedulertest D/param jobinfo id: 1
01-09 15:56:25.518 9091-9091/com.cylasion.jobshedulertest D/RUn: run 1

EM xin cảm ơn trc ạ

Bài liên quan
0