优化连续执行 TimerTask 的服务运行

发布于 2024-12-05 05:36:41 字数 1203 浏览 1 评论 0原文

我需要一个应该始终运行的服务,直到它被我的活动显式停止为止,并且即使由于某些问题(START_STICKY 标志)而停止,也应该重新启动。该服务应该使用 TimerTask 持续执行某些操作(每隔几秒)。我最终得到了以下代码。

public class SomeService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    TimerTask timerTask;
    final Handler handler = new Handler();
    Timer timer = new Timer();

    @Override
    public void onCreate() {
        // code to execute when the service is first created
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        // code to execute when the service is shutting down
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startid) {
        // code to execute when the service is starting up
        timerTask = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                                   //KEEP RUNNING SOME ERRANDS HERE
                        }
                    }
                });
            }
        };
        timer.scheduleAtFixedRate(timerTask, 100L, 1700L);
    }

}

无论如何,我可以优化它以连续运行吗?

I need a service that should always be running till its stopped explicitly by my activity and should start again even if it is stopped due to some issue (START_STICKY flag). This service should continuously do something (every couple of seconds) using a TimerTask. I ended up with the following code.

public class SomeService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    TimerTask timerTask;
    final Handler handler = new Handler();
    Timer timer = new Timer();

    @Override
    public void onCreate() {
        // code to execute when the service is first created
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        // code to execute when the service is shutting down
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startid) {
        // code to execute when the service is starting up
        timerTask = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                                   //KEEP RUNNING SOME ERRANDS HERE
                        }
                    }
                });
            }
        };
        timer.scheduleAtFixedRate(timerTask, 100L, 1700L);
    }

}

Is there anyway that I can optimize this to run continuously?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔芯 2024-12-12 05:36:41

每秒运行听起来相当过分,但是有什么理由不使用 AlarmManager 来触发 IntentService 呢?然后系统将负责可靠地触发您的服务。能否实现可靠的1秒重新触发,我不知道。出于马克在其他答案中提到的原因,这似乎是一个坏主意。

Running every second sounds pretty excessive, but is there a reason why you don't use the AlarmManager to trigger an IntentService? Then the system would be responsible for triggering your service reliably. Whether you can achieve reliable 1 second retriggers, I don't know. Seems like a bad idea for the reasons Mark is mentioning in the other answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文