Android 安卓:有没有意图监听CPU;“睡眠”;及;“醒醒”;除了屏幕打开和关闭?
我的理解是,现有的屏幕关闭和打开意图并不完全意味着设备分别处于睡眠和唤醒状态。设备上的任何应用程序都持有部分唤醒锁,设备将不会处于深度睡眠状态,但屏幕可能处于关闭/打开状态 有没有意图听CPU“唤醒”和“睡眠”Android 安卓:有没有意图监听CPU;“睡眠”;及;“醒醒”;除了屏幕打开和关闭?,android,android-intent,Android,Android Intent,我的理解是,现有的屏幕关闭和打开意图并不完全意味着设备分别处于睡眠和唤醒状态。设备上的任何应用程序都持有部分唤醒锁,设备将不会处于深度睡眠状态,但屏幕可能处于关闭/打开状态 有没有意图听CPU“唤醒”和“睡眠” 有没有办法,我们知道CPU是从深度睡眠中唤醒的?在后台对我的应用程序的某些计时行为进行故障排除时,我需要一个工具来完成这项工作。所以我让我自己的班级来做。请参阅下面的代码。以下是您如何使用它: CpuSleepDetector.getInstance().setSleepEndNotif
有没有办法,我们知道CPU是从深度睡眠中唤醒的?在后台对我的应用程序的某些计时行为进行故障排除时,我需要一个工具来完成这项工作。所以我让我自己的班级来做。请参阅下面的代码。以下是您如何使用它:
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
方法获取最近的检测结果
我知道这是个老问题,但希望这对其他人有用
以下是detector类的代码:
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);
}
}
导入android.os.Handler;
导入android.os.HandlerThread;
导入android.os.Looper;
导入android.os.SystemClock;
导入android.util.Log;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Date;
导入java.util.HashMap;
公共类CpuSleepDetector{
私有静态最终字符串标记=CpuSleepDetector.class.getSimpleName();
私有静态CpuSleepDetector实例=null;
私有句柄线程;
私人经办人;
专用SleepEndNotifier通知程序;
公共静态CpuSleepDetector getInstance(){
if(实例==null){
实例=新的CpuSleepDetector();
}
返回实例;
}
私有CpuSleepDetector(){
线程=新HandlerThread(“cpuSleepDetectorThread”);
thread.start();
handler=新处理程序(thread.getLooper());
watchForSleep();
}
私有void watchForSleep(){
//cpu暂停时正常运行时间暂停
最终长时间UptimeStart=SystemClock.uptimeMillis();
final long realtimeAtStart=SystemClock.elapsedRealtime();
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
长UptimeEnd=SystemClock.uptimeMillis();
long realtimeAtEnd=SystemClock.elapsedRealtime();
长realtimedta=realtimeAtEnd-realtimeAtStart;
长uptimeDelta=UptimeEnd-UptimeStart;
最终长睡眠时间=realtimeDelta-uptimeDelta;
如果(睡眠时间>1){
detectedStalls.put(新日期(),休眠时间);
修剪();
if(通知程序!=null){
新的处理程序(Looper.getMainLooper()).post(新的Runnable()){
@凌驾
公开募捐{
通知程序.cpuSleepEnded(睡眠时间);
}
});
}
}
watchForSleep();
}
}, 1000);
}
private HashMap detectedStalls=new HashMap();
私有HashMap getDetectedStalls(){
返回检测档位;
}
私有空剪枝(){
int numberToPrune=detectedStalls.size()-100;
如果(numberToPrune>0){
HashMap newDetectedStalls=新HashMap();
ArrayList日期=新建ArrayList(getDetectedStalls().keySet());
集合。排序(日期);
for(int i=numberToPrune;i
如果CPU处于睡眠状态,您的代码将如何运行?是的,但我需要知道它何时从深度睡眠中醒来。也许您需要的是isPowerSafeMode()?若我从文档中正确理解,若用户选择了电源安全模式,则ISPowersafMode()将返回true。但我需要,当CPU从深度睡眠中恢复活力时。