Android:除了屏幕打开和关闭之外,是否有任何意图可以监听CPU“睡眠”和“唤醒”?

时间:2015-02-16 07:32:22

标签: android android-intent

我的理解是,现有的Screen OFF和ON意图并不完全意味着设备处于睡眠状态并且分别被唤醒。设备上的任何应用程序都会保持部分唤醒锁定,设备不会处于深度睡眠状态,但屏幕可能会关闭/打开。

是否有任何意图来监听CPU" WAKE UP"和"睡眠" ?

有没有办法,我们知道CPU是从深度睡眠中醒来的?

1 个答案:

答案 0 :(得分:0)

在后台对我的应用程序中的某些计时行为进行故障排除时,我需要一个工具来执行此操作。因此,我上了自己的课来做。请参见下面的代码。使用方法如下:

CpuSleepDetector.getInstance().setSleepEndNotifier(new CpuSleepDetector.SleepEndNotifier() {
        @Override
        public void cpuSleepEnded(long sleepDurationMillis) {
            Log.d(TAG, "The CPU just exited sleep.  It was sleeping for "+sleepDurationMillis+" ms.");
        }
});
CpuSleepDetector.getInstance().logDump();

logDump方法会将最近的100个睡眠事件转储到LogCat。这对故障排除很有用,因为要使CPU入睡,我不仅要断开USB电缆与手机的连接,还必须关闭WiFi上的adb连接。这样,您可以在以后重新连接adb并使用logDump方法来获取最近的检测结果。

我知道这是一个老问题,但是希望这对其他人有用。

这是探测器类的代码:

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;

public class CpuSleepDetector {
    private static final String TAG = CpuSleepDetector.class.getSimpleName();
    private static CpuSleepDetector instance = null;
    private HandlerThread thread;
    private Handler handler;
    private SleepEndNotifier notifier;

    public static CpuSleepDetector getInstance() {
        if (instance == null) {
            instance = new CpuSleepDetector();
        }
        return instance;
    }
    private CpuSleepDetector() {
        thread = new HandlerThread("cpuSleepDetectorThread");
        thread.start();
        handler = new Handler(thread.getLooper());
        watchForSleep();
    }
    private void watchForSleep(){
        // uptime stalls when cpu stalls
        final long uptimeAtStart = SystemClock.uptimeMillis();
        final long realtimeAtStart = SystemClock.elapsedRealtime();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                long uptimeAtEnd = SystemClock.uptimeMillis();
                long realtimeAtEnd = SystemClock.elapsedRealtime();
                long realtimeDelta = realtimeAtEnd - realtimeAtStart;
                long uptimeDelta = uptimeAtEnd - uptimeAtStart;
                final long sleepTime = realtimeDelta - uptimeDelta;
                if (sleepTime > 1) {
                    detectedStalls.put(new Date(), sleepTime);
                    prune();
                    if (notifier != null) {
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                notifier.cpuSleepEnded(sleepTime);
                            }
                        });
                    }
                }
                watchForSleep();
            }
        }, 1000);
    }
    private HashMap<Date,Long> detectedStalls = new HashMap<Date,Long>();
    private HashMap<Date,Long> getDetectedStalls() {
        return detectedStalls;
    }
    private void prune() {
        int numberToPrune = detectedStalls.size() - 100;
        if (numberToPrune > 0) {
            HashMap<Date,Long> newDetectedStalls = new HashMap<Date,Long>();
            ArrayList<Date>  dates = new ArrayList<>(getDetectedStalls().keySet());
            Collections.sort(dates);
            for (int i = numberToPrune; i < detectedStalls.size(); i++) {
                newDetectedStalls.put(dates.get(i), detectedStalls.get(dates.get(i)));
            }
            detectedStalls = newDetectedStalls;
        }
    }
    public void logDump() {
        Log.d(TAG, "Last 100 known CPU sleep incidents:");
        ArrayList<Date>  dates = new ArrayList<>(getDetectedStalls().keySet());
        Collections.sort(dates);
        for (Date date: dates) {
            Log.d(TAG, ""+date+": "+getDetectedStalls().get(date));
        }
    }
    public void setSleepEndNotifier(SleepEndNotifier notifier) {
        this.notifier = notifier;
    }
    public interface SleepEndNotifier {
        public void cpuSleepEnded(long sleepDurationMillis);
    }
}