国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android應用開發(fā)(23)獲取亮滅屏狀態(tài)

這篇具有很好參考價值的文章主要介紹了Android應用開發(fā)(23)獲取亮滅屏狀態(tài)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Android應用開發(fā)學習筆記——目錄索引

在android 應用程序獲取亮滅屏狀態(tài)的常用方法有:

  1. 使用PowerManager提供的API函數(shù)
  2. 注冊Broadcast監(jiān)聽

一、使用PowerManager提供的API函數(shù)

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函數(shù)isInteractive()在應用程序中使用非常簡單:在需要獲取的地方調(diào)用一下即可。

?二、注冊監(jiān)聽ACTION_SCREEN_ON/OFF

    /* 注冊監(jiān)聽 */
    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);
    }

    /* 監(jiān)聽亮屏和滅屏事件 */
    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");
    }

    /* 獲取屏幕狀態(tài)通過PowerManager */
    private void checkScreenStatusByPowerManager() {
        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        boolean isScreenOn = false;
        // Android 4.4W (KitKat Wear)系統(tǒng)及以上使用新接口獲取亮屏狀態(tài)
        if (Build.VERSION.SDK_INT >= 20) {
            isScreenOn = pm.isInteractive();
        } else {
            isScreenOn = pm.isScreenOn();
        }
        if (isScreenOn) {
            printScreenStatus("first On");
        } else {
            printScreenStatus("first Off");
        }
    }

    /* 注冊監(jiān)聽 */
    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);
    }

    /* 監(jiān)聽亮屏和滅屏事件 */
    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);
    }
...
}

?完整源碼

百度網(wǎng)盤鏈接:百度網(wǎng)盤 請輸入提取碼 提取碼:test

github下載地址:

GitHub - liuzhengliang1102/AndroidStudio-LearnAppDevelopment

ScreenStatusBroadcastReceiver目錄

運行

運行l(wèi)og:

---------------------------- 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應用開發(fā)(23)獲取亮滅屏狀態(tài),Android應用開發(fā)學習筆記,android,android studio

??

點此查看Android應用開發(fā)學習筆記的完整目錄文章來源地址http://www.zghlxwxcb.cn/news/detail-618609.html

到了這里,關(guān)于Android應用開發(fā)(23)獲取亮滅屏狀態(tài)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • Android應用開發(fā)(1)Android Studio開發(fā)環(huán)境搭建

    Android應用開發(fā)(1)Android Studio開發(fā)環(huán)境搭建

    Android應用開發(fā)學習筆記——目錄索引 本章介紹搭建Android Studio應用開發(fā)環(huán)境,創(chuàng)建并編譯App工程,運行和調(diào)試App。 谷歌開發(fā)者網(wǎng)站可直接下載AndroidStudio,下載鏈接:https://developer.android.com/studio 點擊【Download Android Studio Electric Eel】 勾選【I have read and agree with the above terms and

    2024年02月07日
    瀏覽(29)
  • Flutter應用開發(fā),系統(tǒng)樣式改不了?SystemChrome 狀態(tài)欄、導航欄、屏幕方向……想改就改

    Flutter應用開發(fā),系統(tǒng)樣式改不了?SystemChrome 狀態(tài)欄、導航欄、屏幕方向……想改就改

    開發(fā)APP時,我們經(jīng)常要客制化狀態(tài)欄、導航欄欄等的樣式和風格,F(xiàn)lutter開發(fā)APP時如何滿足這些客制化要求呢? 自定義狀態(tài)欄和導航欄的樣式:您可以使用 SystemChrome 來定義狀態(tài)欄和導航欄的顏色、文字樣式等,以滿足您的設計需求。 隱藏系統(tǒng)級界面元素:如果您希望在應用

    2024年02月10日
    瀏覽(18)
  • GPT應用開發(fā):編寫插件獲取實時天氣信息

    GPT應用開發(fā):編寫插件獲取實時天氣信息

    歡迎閱讀本系列文章!我將帶你一起探索如何利用OpenAI API開發(fā)GPT應用。無論你是編程新手還是資深開發(fā)者,都能在這里獲得靈感和收獲。 本文,我們將繼續(xù)展示聊天API中插件的使用方法,讓你能夠輕松駕馭這個強大的工具。 首先給大家展示下插件的運行效果,如下圖所示:

    2024年01月20日
    瀏覽(25)
  • 鴻蒙(HarmonyOS)應用開發(fā)——從網(wǎng)絡獲取數(shù)據(jù)(題目答案)

    1.在http模塊中,多個請求可以使用同一個httpRequest對象,httpRequest對象可以復用。 錯誤(False) 2.使用http模塊發(fā)起網(wǎng)絡請求后,可以使用destroy方法中斷網(wǎng)絡請求。 正確(True) 3.Web組件onConfirm(callback: (event?: { url: string; message: string; result: JsResult }) = boolean)事件,返回false時候觸發(fā)網(wǎng)頁

    2024年01月20日
    瀏覽(33)
  • 蘋果個人開發(fā)者賬號申請+獲取證書+上架應用商城

    蘋果個人開發(fā)者賬號申請+獲取證書+上架應用商城

    一、先注冊一個蘋果賬號,打開蘋果開發(fā)者中心Sign In - Apple 1、如果Apple ID可以直接登錄,否則先進行創(chuàng)建 2、點擊創(chuàng)建后跳轉(zhuǎn)至填寫注冊信息頁面 填寫內(nèi)容完成后,點擊提交,驗證郵箱以及手機號 二、開啟雙重認證 使用iPhone登錄相同Apple ID,打開雙重認證 三、在IPhone App S

    2024年04月10日
    瀏覽(37)
  • Android應用開發(fā)(7)按鍵(Button)

    Android應用開發(fā)(7)按鍵(Button)

    Android應用開發(fā)學習筆記——目錄索引 本章介紹按鈕(Button)控件的常見用法,結(jié)合前面Android應用開發(fā)(5)文本顯示(TextView)介紹按鈕(Button)監(jiān)聽點擊和長按事件,禁用和啟用等。 參考google官網(wǎng):https://developer.android.com/reference/android/widget/Button 按鈕控件Button由TextView派生

    2024年02月05日
    瀏覽(31)
  • Android移動應用開發(fā)的學習路線

    Android移動應用開發(fā)的學習路線。以下是一個基本的學習路線,供你參考: 1. Java基礎 學習Java的基本語法和面向?qū)ο缶幊蹋∣OP)的概念 了解Java的數(shù)據(jù)類型、變量和常量 學習控制結(jié)構(gòu)(如條件語句、循環(huán)語句)和函數(shù) 2. Android基礎 了解Android平臺的基本概念和架構(gòu) 學習Android的

    2024年02月11日
    瀏覽(39)
  • HarmonyOS NEXT應用開發(fā)之Web獲取相機拍照圖片案例

    HarmonyOS NEXT應用開發(fā)之Web獲取相機拍照圖片案例

    介紹 本示例介紹如何在HTML頁面中拉起原生相機進行拍照,并獲取返回的圖片。 效果預覽圖 使用說明 點擊HTML頁面中的選擇文件按鈕,拉起原生相機進行拍照。 完成拍照后,將圖片在HTML的img標簽中顯示。 實現(xiàn)思路 添加Web組件,設置onShowFileSelector屬性,接收HTML頁面中input的點

    2024年03月23日
    瀏覽(24)
  • 安卓開發(fā)后臺應用周期循環(huán)獲取位置信息上報服務器

    安卓開發(fā)后臺應用周期循環(huán)獲取位置信息上報服務器

    最近有需求,在APP啟動后,退到后臺,還要能實現(xiàn)周期獲取位置信息上報服務器,研究了一下實現(xiàn)方案。 一、APP退到后臺后網(wǎng)絡請求實現(xiàn) APP退到后臺后,實現(xiàn)周期循環(huán)發(fā)送網(wǎng)絡請求。目前嘗試了兩種方案是OK,如下: (1)AlarmManager + 前臺服務 +廣播的方案,可以正常實現(xiàn),

    2024年02月16日
    瀏覽(23)
  • Android應用開發(fā)(4)視圖布局基本屬性

    Android應用開發(fā)(4)視圖布局基本屬性

    Android應用開發(fā)學習筆記——目錄索引 本章介紹視圖(View)的基本概念及其用法,包括:如何設置視圖的寬度和高度,如何設置視圖的外部間距和內(nèi)部間距,如何設置視圖的外部對齊方式和內(nèi)部對齊方式等。 在Android中,什么是視圖(View)?View是Android中所有控件的基類,不

    2024年02月03日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包