Android应用开发(33)获取亮灭屏状态

Android应用开发学习笔记——目录索引

在android 应用程序获取亮灭屏状态的常用方法有:

  1. 使用PowerManager提供的API函数
  2. 注册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运行截图如下:

  

  

点此查看Android应用开发学习笔记的完整目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuzl_2010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值