Android的 - 服务:只重复一次 [英] Android - Service: Repeats only once

查看:215
本文介绍了Android的 - 服务:只重复一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有scheduleAtFixedRate问题。
它只会调用一次,不重复。有什么不好?

I have a problem with scheduleAtFixedRate. It will only called once and does not repeat. what is wrong?

public class TimerService extends Service {
static final int UPDATE_INTERVAL=3000;
private Timer timer = new Timer();
private Date date;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    repeatJob();
    //Here we want this service to continue running until it is explicitly stopped, so return sticky
    return START_STICKY;
}

private void repeatJob() {
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            Looper.prepare();

            date = new Date();
            displayToast(date.toString());
            Log.d("repeat", new Date().toString());

            Looper.loop();
        }
    }, 0, UPDATE_INTERVAL);
}

private void displayToast(String text) {
    Toast.makeText(this, text, Toast.LENGTH_LONG).show();
}

}

我所说的服务片段活动:

I call the service in fragment activity:

getActivity().startService(new Intent(getActivity().getApplicationContext(), TimerService.class));

我怎样才能得到服务日期(也许更多的价值在未来)的当前值片段活动?

How can I get the current value of date (and maybe more values in future) from service to fragment activity?

感谢您!

推荐答案

您在这里做这么多奇怪的事情,我不知道究竟是什么原因导致的问题,但我会告诉你如何做到这一点正常完成:

You are doing so many odd things here, I'm not sure what exactly causes the problem, but I'll tell you how this would be done normally:

您应该使用 AlarmManager 安排服务为重复执行,并摆脱了定时器。定时器只有当你的过程是活着的作品,而AlarmManager会记住,即使你的应用程序的进程未运行(在这种情况下,它会启动它)挂起的意图。这也是更多的内存和电池效率的,因为它是一个管理计划任务的所有应用程序的集中式系统,不仅你的。

You should use AlarmManager to schedule the service for repeated execution, and get rid of the Timer. The Timer only works if your process is alive, while the AlarmManager will remember pending intents even if your app's process is not running (and in this case it will start it). It is also more memory and battery efficient, as it is a centralized system that manages programmed tasks for all apps, not only yours.

该服务不应该重新安排本身,只是做它需要做的任务。要知道,他们在主线程中运行,所以你需要创建一个工作线程。考虑使用 IntentService ,因为它给你这个已经完成。传回的结果的活动,你的服务可以发送意图包含在Extras中的数据的活动。你也可以广播的结果,并处理它在广播接收机中的活性,或者也可以结合到服务和注册回调。

The service should not re-schedule itself, just do the task it needs to do. Be aware they run in the Main Thread, so you'd need to create a worker thread. Consider using an IntentService as it gives you this already done. To pass back the result to the activity, your service can send an intent to the activity containing the data in the extras. You could also broadcast the result and handle it in a broadcast receiver in the activity, or you can also bind to the service and register a callback.

删除尺蠖行,你不需要这个除暴安良的计时器之后。

Remove the Looper lines, you don't need this after getting rid of the timer.

最后,你似乎是有点困惑的 START_STICKY 标志做了什么。它不会使该服务的继续运行,直到它被明确停止的。有没有办法保证服务将无限期地在Android中运行。这是什么标志呢,就是告诉,如果它得到内存不足的系统,它杀死的服务,一旦恢复,应该尝试重新创建服务,但没有重新提供未决意图。更多信息这里

Finally, you seem to be a bit confused about what the START_STICKY flag does. It does not make the service to "continue running until it is explicitly stopped". There's no way to guarantee a service will be running indefinitely in Android. What this flag does is to tell the system that, in case it gets short of memory and it kills the service, once it recovers it should try to re-create the service, but without redelivering pending intents. More info here.

这篇关于Android的 - 服务:只重复一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆