我有来自官方文档的JobIntentService:
public class SimpleJobIntentService extends JobIntentService {
static final int JOB_ID = 1000;
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, SimpleJobIntentService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(Intent intent) {
// We have received work to do. The system or framework is already
// holding a wake lock for us at this point, so we can just go.
Log.i("SimpleJobIntentService", "Executing work: " + intent);
String label = intent.getStringExtra("label");
if (label == null) {
label = intent.toString();
}
toast("Executing: " + label);
for (int i = 0; i < 5; i++) {
Log.i("SimpleJobIntentService", "Running service " + (i + 1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
Log.i("SimpleJobIntentService", "Completed service @ " + SystemClock.elapsedRealtime());
}
@Override
public void onDestroy() {
super.onDestroy();
toast("All work complete");
}
final Handler mHandler = new Handler();
// Helper for showing tests
void toast(final CharSequence text) {
mHandler.post(new Runnable() {
@Override public void run() {
Toast.makeText(SimpleJobIntentService.this, text, Toast.LENGTH_SHORT).show();
}
});
}
}
在运行此服务之前,在致电Thread.activeCount()
之后,我得到了
数字33。结束第一次运行的服务后,我得到了34,接下来是35,依此类推。
为什么呢有人可以解释我吗?结束后如何杀死该线程?这是JobIntentService的正常行为吗? IntentService可以正常工作,总是在结束后杀死线程。
我使用以下代码从片段启动此服务:
Intent intent = new Intent(getActivity(), SimpleJobIntentService.class);
SimpleJobIntentService.enqueueWork(getActivity(), intent);
无论如何,如果我这样启动服务:
getActivity().startService(new Intent(getActivity(), SimpleJobIntentService.class));
JobIntentService也无法删除线程