IntentService的简单实用

1、是Service的子类
2、任务结束时自动停止服务
代码

/**
*服务
**/
public class MyIntentService extends IntentService {
    public static final String TAG = "MyIntentService";
    public MyIntentService() {
        super(TAG);
        Log.i(TAG, "/MyIntentService");
    }
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        //收到
        Log.i(TAG, "onHandleIntent/" + Thread.currentThread() + "");
        String action = intent.getStringExtra(TAG);
        Log.i(TAG, "/" + action);
    }
    @Override
    public void onDestroy() {
        Log.i(TAG, "/onDestroy");
        super.onDestroy();
    }
}`

/**
*界面
**/
public class IntentServiceActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent_service);
        //启动
        Intent service = new Intent(this, MyIntentService.class);
        service.putExtra(MyIntentService.TAG, "taeyang");
        startService(service);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33373648/article/details/80338876