Why use IntentService?
The code of the service runs in the main thread by default. If some time-consuming operations are performed in the service, the UI will become unresponsive. Therefore, the IntentService automatically creates a sub-thread and stops automatically after executing the task.
Steps for usage:
1. Create a new class, extends IntentService class, and implement the onHandleIntent() method:
public class MyIntentService3 extends IntentService { public MyIntentService3() { super("MyIntentService3"); } @Override protected void onHandleIntent(Intent intent) { //Execute the time-consuming operation, after the execution, the IntentService automatically closes } }
2. Start the Service in the main thread
Intent intent =new Intent(this,MyService.class); startService(intent);