android service using SystemClock.elapsedRealTime() instead of SystemClock.uptimeMillis() works in emulator but not in samsung captivate?

非 Y 不嫁゛ 提交于 2020-01-07 03:25:08

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/4534914/android-service-using-systemclock-elapsedrealtime-instead-of-systemclock-uptim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!