Android 服务 + 倒计时 = 不工作

标签 android service

好吧,这让我很生气。倒计时服务有限制吗?

我尝试了很多方法让倒数计时器在服务中工作,但总是失败。它永远不会到达 finish() 部分!

那么服务和倒计时有限制吗?请帮助我...

这是服务:

public class ss extends IntentService {

      public ss() {
          super("ss");
      }

      @Override
      protected void onHandleIntent(Intent intent) {

        new CountdownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {

     }

     public void onFinish() {
         executemymethod(); //it never gets here!
     }
  }.start();

      }



}

如您所见,代码简单且正确,但仍然是 executemymethod();永远不会真正执行!没有错误...请给我一个解决方案!

最佳答案

如果你想做这种事情,你应该使用常规服务而不是 IntentService。这是文档对 IntentService 的描述:

Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

由于倒计时是异步的,因此在您启动倒计时计时器后工作就完成了。 IntentService 然后立即完成,并可能关闭线程。

关于Android 服务 + 倒计时 = 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246545/

相关文章:

c - 如何在 win32 中安装/卸载 Windows 服务?

php - 通过php和bash执行服务命令

java - 为什么我的 listfragment 不保留其状态?

android - 像 Youtube 应用程序一样搜索 View

android - 更新 Android Studio 后似乎无法修复此错误

angularjs - Angular 元数据服务 [$injector :unpr]

android - 在不耗尽电池的情况下与服务器保持持续的互联网连接

android - 在 android gradle 3.6.1 中运行构建任务后未生成 Jar

android - 使用AsyncTask在后台服务中添加android进度对话框,获取致命异常

android - 设置 IMediaPlaybackService