android 中服务器使用 countdowns有限制吗?

我使用很多方法来让一个 countdowns 计时器能在服务器中运行,但是总是失败,从来不能到达finish()部分。
那么android中服务器使用 countdowns有限制吗?
service:

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();一直执行不到,但是也没有错误,怎么解决?

如果你的计时器在服务中运行但总是没有到达 onFinish() 部分,可能是因为 IntentService 在处理完 Intent 后就会终止。在 onHandleIntent() 方法中启动的计时器可能在 IntentService 终止之前就已经结束了。


如果你想在服务中运行计时器,你可以使用 Service 来代替 IntentService。Service 在处理完 Intent 后不会自动终止,因此可以在 Service 中启动和管理计时器。


那么你可以尝试更换为 Service,

public class ss extends Service {
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
        new CountdownTimer(30000, 1000) {
     public void onTick(long millisUntilFinished) {
     }
     public void onFinish() {
         executemymethod(); 
     }
  }.start();
         return START_STICKY;
      }
}

或者可以使用 android.os.CountDownTimer 来代替 new CountdownTimer

new CountDownTimer(30000, 1000) {
    public void onTick(long millisUntilFinished) {
    }
    public void onFinish() {
        executemymethod();
    }
}.start();

这两种方法都是正常的,可以在服务中使用。如果依然出现问题,请提供更多代码和错误信息,我可以帮你进一步分析。