Android.os.SystemClock

本文介绍了Android系统中SystemClock类的各种方法,包括获取不同类型的系统时间及如何使用这些时间来计算时间间隔。同时,还详细讲解了PowerManager类及其成员WakeLock的使用方法,帮助开发者了解如何正确地管理和控制设备的唤醒状态。

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

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

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

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

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

Java代码

// PowerManager的一般用法,请求和释放唤醒锁  
PowerManager powerMgr = (PowerManager)         getSystemService(Context.POWER_SERVICE);  
int flags = PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP; 
WakeLock wakeLock = powerMgr.newWakeLock(flags, "for debug purpose tag");  
wakeLock.acquire(); // 获取唤醒锁  
wakeLock.release(); // 释放唤醒锁  
// 在游戏中,会将acquire放在onResume中;将release放在onPause,这样在程序运行时就可以保持屏幕常量;在程序处在后台时,就恢复原来的设置。 
// PowerManager的其它api的使用  
powerMgr.goToSleep(SystemClock.uptimeMillis() + 100); // 100ms后进入睡眠  
powerMgr.isScreenOn(); // 屏幕是否亮着  
powerMgr.userActivity(SystemClock.uptimeMillis()+100, true); // 相当于按home键,会引起从睡眠激活 
// WakeLock的其它api  
wakeLock.acquire(1000); // 获取唤醒锁,并在1000ms后释放  
wakeLock.isHeld(); // 当前是否持有唤醒锁  
// 是否使用引用计数,默认是启用的。引用计数应该就是第一次请求为1,第二次加1,再一次再加1。  
// 在释放时,只有引用计数为0时才被视为完全释放(所以要多次调用release)  
wakeLock.setReferenceCounted(true);  
我刚开始弄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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值