Android.os.SystemClock

本文深入探讨了Android开发中时间间隔管理及设备电源管理的关键技术,包括使用SystemClock方法进行精确时间测量,以及PowerManager API实现唤醒锁控制以平衡屏幕亮度和设备续航。文章还详细介绍了PowerManager的常用API及其唤醒锁的操作方式,为开发者提供高效能应用的开发指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android.os.SystemClock
文档中对System.currentTimeMillis()进行了相应的描述,就是说它不适合用在需要时间间隔的地方,如Thread.sleep, Object.wait等,因为可以通过System.setCurrentTimeMillis来改变它的值。

要用时间间隔,推荐使用SystemClock中的相关方法。
 


Java代码 
 1.SystemClock.currentThreadTimeMillis(); // 在当前线程中已运行的时间 
2.SystemClock.elapsedRealtime(); // 从开机到现在的毫秒书(手机睡眠(sleep)的时间也包括在内) 
3.SystemClock.uptimeMillis(); // 从开机到现在的毫秒书(手机睡眠的时间不包括在内) 
4.SystemClock.sleep(100); // 类似Thread.sleep(100);但是该方法会忽略InterruptedException 
5.SystemClock.setCurrentTimeMillis(1000); // 设置时钟的时间,和System.setCurrentTimeMillis类似 
6. 
7.// 时间间隔 
8.long timeInterval = SystemClock.uptimeMillis() - lastTime; 
9.// do something with timeInterval 

android.os.PowerManager
PowerManager的flag的意思:

 


Java代码
 1.// PowerManager的一般用法,请求和释放唤醒锁 
2.PowerManager powerMgr = (PowerManager) 
3.        getSystemService(Context.POWER_SERVICE); 
4.int flags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP; 
5.WakeLock wakeLock = powerMgr.newWakeLock(flags, "for debug purpose tag"); 
6.wakeLock.acquire(); // 获取唤醒锁 
7.wakeLock.release(); // 释放唤醒锁 
8.// 在游戏中,会将acquire放在onResume中;将release放在onPause,这样在程序运行时就可以保持屏幕常量;在程序处在后台时,就恢复原来的设置。 
9. 
10. 
11.// PowerManager的其它api的使用 
12.powerMgr.goToSleep(SystemClock.uptimeMillis() + 100); // 100ms后进入睡眠 
13.powerMgr.isScreenOn(); // 屏幕是否亮着 
14.powerMgr.userActivity(SystemClock.uptimeMillis()+100, true); // 相当于按home键,会引起从睡眠激活 
15. 
16. 
17.// WakeLock的其它api 
18.wakeLock.acquire(1000); // 获取唤醒锁,并在1000ms后释放 
19.wakeLock.isHeld(); // 当前是否持有唤醒锁 
20.// 是否使用引用计数,默认是启用的。引用计数应该就是第一次请求为1,第二次加1,再一次再加1。 
21.// 在释放时,只有引用计数为0时才被视为完全释放(所以要多次调用release) 
22.wakeLock.setReferenceCounted(true);  

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2011-11/48325p2.htm

 

我刚开始弄framework,我想问问我该如何看源码,比如下面这段我该怎么看呢? /* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.quickstep; import static com.android.launcher3.util.Executors.MAIN_EXECUTOR; import static com.android.quickstep.util.ActiveGestureLog.INTENT_EXTRA_LOG_TRACE_ID; import android.annotation.TargetApi; import android.content.Intent; import android.graphics.PointF; import android.os.Build; import android.os.SystemClock; import android.os.Trace; import android.view.View; import androidx.annotation.BinderThread; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.UiThread; import com.android.launcher3.DeviceProfile; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.statemanager.StatefulActivity; import com.android.launcher3.taskbar.TaskbarUIController; import com.android.launcher3.util.RunnableList; import com.android.quickstep.RecentsAnimationCallbacks.RecentsAnimationListener; import com.android.quickstep.views.DesktopTaskView; import com.android.quickstep.views.RecentsView; import com.android.quickstep.views.TaskView; import com.android.systemui.shared.recents.model.ThumbnailData; import com.android.systemui.shared.system.InteractionJankMonitorWrapper; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; /** * Helper class to handle various atomic commands for switching between Overview. *
最新发布
03-13
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值