Java JobIntentService不像IntentService那样在完成后移除螺纹
我从官方文件获得了JobIntentService:Java JobIntentService不像IntentService那样在完成后移除螺纹,java,android,jobintentservice,Java,Android,Jobintentservice,我从官方文件获得了JobIntentService: public class SimpleJobIntentService extends JobIntentService { static final int JOB_ID = 1000; static void enqueueWork(Context context, Intent work) { enqueueWork(context, SimpleJobIntentService.class, JOB
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();
}
});
}
}
无论如何,如果我这样启动服务:
getActivity().startService(new Intent(getActivity(), SimpleJobIntentService.class));
JobIntentService也不会删除线程
getActivity().startService(new Intent(getActivity(), SimpleJobIntentService.class));