Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用长时间运行的OnHandleContent优雅地处理IntentService的关闭?_Android_Android Service - Fatal编程技术网

Android 如何使用长时间运行的OnHandleContent优雅地处理IntentService的关闭?

Android 如何使用长时间运行的OnHandleContent优雅地处理IntentService的关闭?,android,android-service,Android,Android Service,假设我在Android中有一个IntentService,它有一个onHandleIntent方法,执行时间很长(分钟/小时)。如何优雅地处理服务关闭?为了论证,让我们说手机在一个需要一个小时完成的意图的中间关闭。 我假设正确的做法是: 实现onDestroy方法,使其设置一个标志,指示服务已关闭 确保onHandleIntent中的长时间运行工作已中断,以便可以定期检查此标志,如果该标志为真,则可以正常停止工作 似乎是最接近我想要的,但我想看到这个问题得到更具体的回答。在IntentServi

假设我在Android中有一个IntentService,它有一个onHandleIntent方法,执行时间很长(分钟/小时)。如何优雅地处理服务关闭?为了论证,让我们说手机在一个需要一个小时完成的意图的中间关闭。 我假设正确的做法是:

  • 实现onDestroy方法,使其设置一个标志,指示服务已关闭

  • 确保onHandleIntent中的长时间运行工作已中断,以便可以定期检查此标志,如果该标志为真,则可以正常停止工作

  • 似乎是最接近我想要的,但我想看到这个问题得到更具体的回答。在IntentService的文档中,它说:

    所有请求都在一个工作线程上处理——它们可能需要花费必要的时间(并且不会阻塞应用程序的主循环),但一次只处理一个请求


    我的问题是,如果用户在执行过程中关闭了手机,那么“只要有必要”显然是不正确的。那么,如何优雅地关机呢?

    这取决于您在服务中所做的工作。您是否需要保持进度,或者如果保存不正确,数据是否会被破坏?我需要保持进度。