Using IntentService

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);

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326215401&siteId=291194637