IntentService后台服务

1.创建自定义后台服务

可new->Service->IntentService

public class MyIntentService extends IntentService{

2.在方法中添加执行代码

protected void onHandleIntent(Intent intent){

                      ....

            //执行代码

}

3.启动后台服务

Intent intent = new Intent(MainActivity.this , MyIntentService.class);

startService(intent);

4.注意

后台服务会因程序完全停止而停止

如需关闭程序后继续执行任务,需用其他后台任务处理机制,如JobScheduler或WorkManager等

你可能感兴趣的:(java,android)