Android:除了打开和关闭屏幕外,是否还有其他意图监听CPU的“睡眠”和“唤醒”?

Pam*_*thi 5 android android-intent

我的理解是,现有的“屏幕关闭”和“打开”意图并不完全表示设备分别处于睡眠和唤醒状态。设备上的所有应用程序都拥有部分唤醒锁,设备将不会进入深度睡眠状态,但屏幕可能会关闭/打开。

是否有意图收听CPU“ WAKE UP”和“ SLEEP”?

有什么办法,我们知道CPU从深度睡眠中唤醒了吗?

dav*_*ung 3

在对后台应用程序的某些计时行为进行故障排除时,我需要一个工具来完成此操作。所以我创建了自己的课程来做到这一点。请参阅下面的代码。使用方法如下:

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();
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)