-1

}

一切正常,但是当按下暂停然后尝试恢复时,我仍然可以上传图像,但我无法将广播消息发送到活动。

4

3 回答 3

0

我假设 OP 的意思是他所说的,并且正在使用 IntentService 类,该类确实在自己的线程中运行。如果是这样,暂停服务正在执行的工作的一种方法是向它发送一个指示它暂停的 Intent。然后,您必须修改执行工作的代码以定期检查是否已发出暂停。

不过,我有点疑惑。为什么要暂停服务?它在后台运行,与用户在 Activity 中所做的事情异步。用户不得不暂停图像显示的用例是什么?处理此问题的一种方法是从 Activity 启动图像下载,然后允许用户在您的应用程序或其他应用程序中执行其他工作。下载所有图像后,发布通知并将 PendingIntent 附加到它。当用户单击通知时,控制权会传回您的应用程序。我认为这是使用异步工作实现应用程序生命周期的最佳模式。

于 2012-06-05T17:24:48.383 回答
0

Services, by default, are not in their own Thread. So pausing the service is as simple as calling Thread.sleep(). However, assuming you are running a Thread in your Service, which I would do since you are uploading images, I would have a a flag in the while loop of the Thread's run method. This flag would be the IsServicePaused boolean that would pause or break out of the running loop.

于 2012-06-05T16:55:33.437 回答
0

调用 Looper.loop() 并让它处理一条消息。

于 2012-06-05T18:00:44.683 回答