0

我不确定这是否是正确的方法,但我会尝试解释我想要做什么。

我有一个Activity创建一个带有标签的fragment调用。TemporaryFragment我想要做的是创建并启动一个包含 a 的服务TimerTimer然后更新其中的时间TextView

我正在考虑的方式是以某种方式,当Service启动时,TextView将从传递ActivityService,然后Service保持对它的引用。

另一种可能的方法是让Activity成为 的监听器,Service然后调用 中的方法Service来更新TextView

任何想法都会很棒,也许还有一些选择。

提前致谢。

补充 对不起,我还应该指定我需要这个计时器在后台运行。因此,当应用程序被发送到后台时,我需要计时器继续运行,并且只有在我告诉它时才停止。

4

4 回答 4

1

对于这样的小任务,Service 并不理想,而且 Service 可以独立于活动运行。如果您从移动应用程序的角度考虑,由于这个相对较小的原因,生成新线程或使用将新线程引入应用程序的计时器也不理想。

而是Handler在您的片段中使用。

在您的片段中创建处理程序

private Handler mHandler = new Handler();

执行您定义的任务调用

mHandler.postDelayed(mUpdateTask, 1000);

或者

mHandler.post(mUpdateTask);

并在片段中定义您的任务

private Runnable mUpdateTask = new Runnable() {
    public void run() {
        Toast.makeText(getActivity(), "hello world", Toast.LENGTH_SHORT).show();
        mHandler.postDelayed(this, 1000);
    }
};

如果您要显示类似时间的信息而不是类似倒计时的信息,请使用

mHandler.removeCallbacks(mUpdateTimeTask);

如果活动不可见,则在onPause()方法中停止执行任务,因为更新 UI 不相关并且可以节省电池(您在onResume()方法中再次启动任务)

于 2012-07-11T13:08:17.690 回答
0

我会通过使用线程来使用更简单的方法:

public class MainActivity extends Activity implements Callback {
    private static final int MSG_UPDATE = 1;
    private static final long INTERVAL = 1000; // in ms

    private final Handler handler = new Handler(this);
    private Thread worker;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_UPDATE:
                updateView();
                return true;
        }
        return false;
    }

    private void updateView() {
        // TODO tbd
    }

    @Override
    protected void onStart() {
        super.onStart();
        // start background thread
        worker = new Thread(new Runnable() {
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        Thread.sleep(INTERVAL);
                    } catch (InterruptedException e) {
                        break;
                    }
                    // send message to activity thread
                    handler.sendEmptyMessage(MSG_UPDATE);
                }
            }
        });
        worker.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        // stop background thread
        worker.interrupt();
        try {
            worker.join();
        } catch (InterruptedException e) {
        }
        worker = null;
    }
}
于 2012-07-11T12:21:58.010 回答
0

您可以TimerTask为此使用 Class。覆盖该TimerTask.run()方法,然后将其添加TimerTaskTimer类中。

还要检查这个问题:用定时器和定时器任务控制任务

于 2012-07-11T12:22:44.880 回答
0

基本上,计时器背后的想法是最终我将在我的应用程序中添加一些跟踪,因此即使应用程序不在前台,也需要它继续运行 - Disco S2

基于此评论,我建议您使用驻留在后台的本地服务,做它的东西(从 开始一个线程Service#onStart),直到它被stopService(..).

Activities on the other hand may bind and unbind to that service (see: bindService(..)) to get notified about updates or to communicate with the service in any way.

于 2012-07-11T14:19:34.160 回答