在android 应用程序获取亮灭屏状态的常用方法有:
- 使用PowerManager提供的API函数
- 注册Broadcast监听
一、使用PowerManager提供的API函数
frameworks/base/core/java/android/os/PowerManager.java
import android.os.Build;
import android.os.PowerManager;
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = false;
if (Build.VERSION.SDK_INT < 20) {
/* deprecated Use {@link #isInteractive} instead. */
isScreenOn = pm.isScreenOn();
} else {
isScreenOn = pm.isInteractive();
}
使用PowerManager的API函数isInteractive()在应用程序中使用非常简单:在需要获取的地方调用一下即可。
二、注册监听ACTION_SCREEN_ON/OFF
/* 注册监听 */
private void registerScreenReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mScreenReceiver = new ScreenReceiver();
registerReceiver(mScreenReceiver, filter);
}
/* 监听亮屏和灭屏事件 */
private class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_SCREEN_ON.equals(action)) {
Log.d(TAG, "screen is on");
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
Log.d(TAG, "screen is off");
}
}
}
四、测试程序
package com.example.screenstatusbroadcastreceiver;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private static String TAG = "lzl-test";
private ScreenReceiver mScreenReceiver;
private TextView mTextView;
private String mString = new String();
private String mStringLife = new String();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView)findViewById(R.id.textView);
checkScreenStatusByPowerManager();
registerScreenReceiver();
printLife("onCreate");
}
/* 获取屏幕状态通过PowerManager */
private void checkScreenStatusByPowerManager() {
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = false;
// Android 4.4W (KitKat Wear)系统及以上使用新接口获取亮屏状态
if (Build.VERSION.SDK_INT >= 20) {
isScreenOn = pm.isInteractive();
} else {
isScreenOn = pm.isScreenOn();
}
if (isScreenOn) {
printScreenStatus("first On");
} else {
printScreenStatus("first Off");
}
}
/* 注册监听 */
private void registerScreenReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mScreenReceiver = new ScreenReceiver();
registerReceiver(mScreenReceiver, filter);
}
/* 监听亮屏和灭屏事件 */
private class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_SCREEN_ON.equals(action)) {
printScreenStatus("On");
} else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
printScreenStatus("Off");
}
}
}
private String getNowTimeMs() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss:SSS");
return simpleDateFormat.format(new Date());
}
private void printLife(String string) {
Log.d(TAG, "[" + getNowTimeMs() + "]" + string);
mStringLife = String.format("%s[%s] %s: %s\n", mStringLife, getNowTimeMs(), "Main", string);
}
private void printScreenStatus(String string) {
Log.d(TAG, "[" + getNowTimeMs() + "]" + "screen is " + string);
mString = String.format("%s[%s] screen is: %s\n", mString, getNowTimeMs(), string);
mTextView.setText(mString);
}
...
}
完整源码
百度网盘链接:百度网盘 请输入提取码 提取码:test
github下载地址:
GitHub - liuzhengliang1102/AndroidStudio-LearnAppDevelopment
ScreenStatusBroadcastReceiver目录
运行
运行log:
---------------------------- PROCESS STARTED (25097) for package com.example.screenstatusbroadcastreceiver ----------------------------
2023-07-30 20:00:09.592 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:09:591]screen is first On
2023-07-30 20:00:09.594 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:09:594]onCreate
2023-07-30 20:00:09.603 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:09:603]onStart
2023-07-30 20:00:09.606 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:09:606]onResume
2023-07-30 20:00:16.793 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:16:793]onPause
2023-07-30 20:00:16.852 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:16:850]onStop
2023-07-30 20:00:17.797 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:17:795]screen is Off
2023-07-30 20:00:18.565 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:18:564]screen is On
2023-07-30 20:00:20.539 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:20:536]onRestart
2023-07-30 20:00:20.543 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:20:543]onStart
2023-07-30 20:00:20.553 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:20:552]onResume
2023-07-30 20:00:26.361 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:26:360]onPause
2023-07-30 20:00:26.393 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:26:393]onStop
2023-07-30 20:00:27.574 25097-25097 lzl-test com...screenstatusbroadcastreceiver D [20:00:27:570]screen is Off
使用xiaomi 13 Ultra运行截图如下: