我有一个服务,它请求另一个类,它启动一个 AsyncTask
服务->天气类->执行方法->异步任务->执行
这是在服务中启动的
new Weather(this).execute(); // the execute is a method of the class, not of the AsyncTask
如何在 Service 中检测到 AsyncTask 已完成,以便我可以调用 stopSelf?
我有一个服务,它请求另一个类,它启动一个 AsyncTask
服务->天气类->执行方法->异步任务->执行
这是在服务中启动的
new Weather(this).execute(); // the execute is a method of the class, not of the AsyncTask
如何在 Service 中检测到 AsyncTask 已完成,以便我可以调用 stopSelf?
在这种情况下,您可以使用stopService(intent) 。
当前在我们的应用程序中使用的方法是注册一个单一BroadcastReceiver
的对象android.app.Service
,以侦听从衍生的底层触发的广播事件AsyncTask#onPostExecute
。这在我们的场景中是有利的,因为我们的服务产生了三个 AsyncTasks,并且我们希望在所有三个 AsyncTasks 都完成时停止父服务,即我们的服务级别有三个命中BroadcastReceiver
。
在我偶然发现这个问题很久之后,我想我会在这里添加这个,但希望它可以帮助其他人......