我正在学习如何使用处理程序线程。我提到了互联网上的一些教程,但不幸的是,大多数教程都提供了解释 理论上没有提供简洁明了的例子。
在下面的代码中,我只想向Messenger的处理程序发送消息,并且我希望此值可以在的handleMessage()中进行处理。 HandlerThread类。
发生的事情是,下面的JobSchedulerService01启动了,调用了onStartJob,这三种方法都是
prepareLooper()
onLooperPrepared()
sendMessage()
被调用,但是没有来自handleMessage()方法的日志,而且,从未调用jobFinished()。
请让我知道如何正确使用HandlerThread来完成上述任务。
代码
public class JobSchedulerService01 extends JobService {
private final static String TAG = JobSchedulerService01.class.getSimpleName();
public final static String ACTION_JOB_SCHEDULER_01 = "action_start_job_01";
private JobParameters jobParameters = null;
private MyHandlerThread mMyHandlerThread = null;
@Override
public boolean onStartJob(JobParameters params) {
Log.w(TAG, "onStartJob");
this.setJobParameters(params);
int jobID = params.getJobId();
Toast.makeText(this, "JobID: " + jobID + "JobName: " + TAG, Toast.LENGTH_SHORT).show();
Log.i(TAG, "JobID: " + jobID + " JobName: " + TAG );
this.mMyHandlerThread = new MyHandlerThread(params.toString());
this.mMyHandlerThread.start();
this.mMyHandlerThread.prepareLooper();
this.mMyHandlerThread.sendMessage(20);
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
Log.w(TAG, "onStopJob");
return false;
}
private class MyHandlerThread extends HandlerThread {
private Handler mHandler = null;
public MyHandlerThread(String name) {
super(name);
Log.i(TAG, "MyHandlerThread: " + name + "started.." );
}
@Override
protected void onLooperPrepared() {
super.onLooperPrepared();
Log.w(TAG, "onLooperPrepared called");
mHandler = new Handler(this.getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int maxIteration = msg.getData().getInt("what");
Log.v(TAG, mHandler.getClass() + "MaxIteration: " + maxIteration);
for (int i = 0; i <= maxIteration; i++) {
Log.i(TAG, "Job: " + getJobParameters().toString() + " publishes: " + i);
try {
Thread.sleep(1 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
jobFinished(getJobParameters(), false);
}
};
}
public void prepareLooper() {
Log.w(TAG, "prepareLooper called");
this.mHandler = new Handler(this.getLooper());
}
public void sendMessage(int val) {
Log.w(TAG, "sendMessage called");
Log.w(TAG, "this.mHandler initialized: " + this.mHandler);
Bundle bundle = new Bundle();
bundle.putInt("what", val);
Message msg = this.mHandler.obtainMessage();
msg.setData(bundle);
this.mHandler.sendMessage(msg);
}
}
public void setJobParameters(JobParameters jobParameters) {
this.jobParameters = jobParameters;
}
public JobParameters getJobParameters() {
return this.jobParameters;
}
}
日志:
.servicewithid_01 W/JobSchedulerService01: onStartJob
.servicewithid_01 I/JobSchedulerService01: JobID: 1 JobName: JobSchedulerService01
.servicewithid_01 I/JobSchedulerService01: MyHandlerThread: android.app.job.JobParameters@1028756started..
.servicewithid_01 W/JobSchedulerService01: prepareLooper called
.servicewithid_01 W/JobSchedulerService01: sendMessage called this.mHandler initialized: Handler (android.os.Handler) {5d403d7}
.servicewithid_01 W/JobSchedulerService01: onLooperPrepared called