效果是连续点击一个按钮,触发startService,将任务交给IntentService处理;
public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //点击button触发 public void startService(View view) { Log.d(TAG, "click button to start service"); Intent intent = new Intent(this, TestIntentService.class); startService(intent); } } //service 代码 public class TestIntentService extends IntentService { private static final String TAG = "IntentService"; public TestIntentService() { super("intent-service"); Log.d(TAG, "constructor"); } @Override public void onCreate() { Log.d(TAG, "onCreate"); super.onCreate(); } @Override public void onStart(Intent intent, int startId) { Log.d(TAG, "onStart"); super.onStart(intent, startId); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "onHandleIntent"); for (int i = 0; i < 8; i++) { Log.d(TAG, "onHandleIntent ----"+Thread.currentThread().getName()+", i="+i); try { Thread.sleep(800); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public void onDestroy() { Log.d(TAG, "onDestroy"); super.onDestroy(); } } 多次频繁点击按钮,日志如下: 2019-05-22 11:13:41.793 5863-5863/com.linx.jnitest D/MainActivity: click button to start service 2019-05-22 11:13:41.876 5863-5863/com.linx.jnitest D/IntentService: constructor 2019-05-22 11:13:41.877 5863-5863/com.linx.jnitest D/IntentService: onCreate 2019-05-22 11:13:41.890 5863-5863/com.linx.jnitest D/IntentService: onStartCommand 2019-05-22 11:13:41.890 5863-5863/com.linx.jnitest D/IntentService: onStart 2019-05-22 11:13:41.891 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent 2019-05-22 11:13:41.891 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=0 2019-05-22 11:13:42.121 5863-5863/com.linx.jnitest D/MainActivity: click button to start service 2019-05-22 11:13:42.149 5863-5863/com.linx.jnitest D/IntentService: onStartCommand 2019-05-22 11:13:42.149 5863-5863/com.linx.jnitest D/IntentService: onStart 2019-05-22 11:13:42.484 5863-5863/com.linx.jnitest D/MainActivity: click button to start service 2019-05-22 11:13:42.489 5863-5863/com.linx.jnitest D/IntentService: onStartCommand 2019-05-22 11:13:42.490 5863-5863/com.linx.jnitest D/IntentService: onStart 2019-05-22 11:13:42.692 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=1 2019-05-22 11:13:43.493 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=2 2019-05-22 11:13:44.294 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=3 2019-05-22 11:13:45.095 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=4 2019-05-22 11:13:45.895 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=5 2019-05-22 11:13:46.696 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=6 2019-05-22 11:13:47.497 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=7 2019-05-22 11:13:48.300 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent 2019-05-22 11:13:48.300 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=0 2019-05-22 11:13:49.101 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=1 2019-05-22 11:13:49.901 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=2 2019-05-22 11:13:50.702 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=3 2019-05-22 11:13:51.503 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=4 2019-05-22 11:13:52.304 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=5 2019-05-22 11:13:53.104 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=6 2019-05-22 11:13:53.905 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=7 2019-05-22 11:13:54.708 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent 2019-05-22 11:13:54.708 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=0 2019-05-22 11:13:55.509 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=1 2019-05-22 11:13:56.310 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=2 2019-05-22 11:13:57.110 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=3 2019-05-22 11:13:57.911 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=4 2019-05-22 11:13:58.712 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=5 2019-05-22 11:13:59.513 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=6 2019-05-22 11:14:00.313 5863-6106/com.linx.jnitest D/IntentService: onHandleIntent ----IntentService[intent-service], i=7 2019-05-22 11:14:01.118 5863-5863/com.linx.jnitest D/IntentService: onDestroy