Android的服务使用SystemClock.elapsedRealTime()而不是SystemClock.uptimeMillis()在模拟器,但不是在三星着迷的作品? [英] android service using SystemClock.elapsedRealTime() instead of SystemClock.uptimeMillis() works in emulator but not in samsung captivate?

查看:6010
本文介绍了Android的服务使用SystemClock.elapsedRealTime()而不是SystemClock.uptimeMillis()在模拟器,但不是在三星着迷的作品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里计算器第一个问题:)

我正在一点点的Andr​​oid 2.2的应用程序登录的CPU频率使用。它被设置为将使用一个新的线程写入数据每10秒的服务。的code表示该部分是非常基本的(见下文)。

它工作正常,但它不会跟踪时间,而手机是睡着了(这我知道,是预期的行为)。因此,我改变了code使用SystemClock.elapsedRealTime()代替。问题是,在模拟器两个命令是等价的,但在手机应用程序将启动线程,但它永远不会执行mHandler.postAtTime命令。任何有关为什么发生这种情况或如何克服这个问题的建议是大大AP preciated。

PS:叠梁()不被调用。这不是问题。

  mUpdateTimeTask =新的Runnable(){
        公共无效的run(){
            长米利斯= SystemClock.uptimeMillis() - mStartTime;
            INT秒=(INT)(米利斯/ 1000);
            INT分钟=秒/ 60;
            秒秒= 60%;            串频率= readCPU();
            如果(频率== NULL)
                Toast.makeText(CPU_log_Service.this。CPU频率是不可读\\ n请确保该文件读取权限,Toast.LENGTH_LONG).show();
            字符串str =新的String((*分60秒+)+,+频率+\\ n);
            如果叠梁()(WRITELOG(STR)!);
            mHandler.postAtTime(此,mStartTime +(((分钟* 60)+秒+ 10)* 1000));
    }};    mStartTime = SystemClock.uptimeMillis();
    mHandler.removeCallbacks(mUpdateTimeTask);
    mHandler.postDelayed(mUpdateTimeTask,100);


解决方案

作为文档状态,为 postAtTime(),时基是uptimeMillis()。 因此,你不能只是决定使用 elapsedRealTime()

我会建议您只需使用 postDelayed(),以消除全时基问题。

First question here in stackoverflow :)

I'm running a little android 2.2 app to log cpu frequency usage. It is set up as a service that will write the data every 10 seconds using a new thread. The code for that part is very basic (see below).

It works fine, except that it would not keep track of time while the phone is asleep (which, I know, is the expected behavior). Thus, I changed the code to use SystemClock.elapsedRealTime() instead. Problem is, in emulator both commands are equivalent, but in the phone the app will start the thread but it will never execute the mHandler.postAtTime command. Any advice regarding why this is happening or how to overcome the issue is greatly appreciated.

PS: stopLog() is not being called. That's not the problem.

    mUpdateTimeTask = new Runnable() {
        public void run() {
            long millis = SystemClock.uptimeMillis() - mStartTime;
            int seconds = (int) (millis / 1000);
            int minutes = seconds / 60;
            seconds     = seconds % 60;

            String freq = readCPU ();
            if (freq == null)
                Toast.makeText(CPU_log_Service.this, "CPU frequency is unreadable.\nPlease make sure the file has read rights.", Toast.LENGTH_LONG).show();
            String str = new String ((minutes*60 + seconds) + ", " + freq + "\n");
            if (!writeLog (str)) stopLog();
            mHandler.postAtTime(this, mStartTime + (((minutes * 60) + seconds + 10) * 1000));
    }};

    mStartTime = SystemClock.uptimeMillis();
    mHandler.removeCallbacks(mUpdateTimeTask);
    mHandler.postDelayed(mUpdateTimeTask, 100);

解决方案

As the documentation states, for postAtTime(), "The time-base is uptimeMillis()." Hence, you cannot just decide to use elapsedRealTime().

I would recommend you simply use postDelayed(), to eliminate the whole time-base problem.

这篇关于Android的服务使用SystemClock.elapsedRealTime()而不是SystemClock.uptimeMillis()在模拟器,但不是在三星着迷的作品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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