Android自定義屏保

Android亮屏、熄屏的時候會發(fā)出廣播,通常在熄屏廣播中啟動屏保

  • 注冊屏幕廣播
 /**
     * 注冊廣播
     *
     * @param mContext 上下文環(huán)境
     * @param receiver 將要注冊的廣播
     */
    public void registerScreenSaverReceiver(Context mContext, ScreenReceiver receiver) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        mContext.registerReceiver(receiver, filter);
    }

在該廣播的Action中有著這樣的注釋:

/**
     * Broadcast Action: Sent when the device wakes up and becomes interactive.
     * <p>
     * For historical reasons, the name of this broadcast action refers to the power
     * state of the screen but it is actually sent in response to changes in the
     * overall interactive state of the device.
     * </p><p>
     * This broadcast is sent when the device becomes interactive which may have
     * nothing to do with the screen turning on.  To determine the
     * actual state of the screen, use {@link android.view.Display#getState}.
     * </p><p>
     * See {@link android.os.PowerManager#isInteractive} for details.
     * </p>
     * You <em>cannot</em> receive this through components declared in
     * manifests, only by explicitly registering for it with
     * {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)
     * Context.registerReceiver()}.
     *
     * <p class="note">This is a protected intent that can only be sent
     * by the system.
     */

這就是說,由于歷史原因,該廣播Action表示屏幕的電信號狀態(tài),但是實際上該廣播是在屏幕不可進行交互時發(fā)出
注:不能通過在Manifest.xml文件注冊的方式接收該廣播,僅僅支持通過代碼registerReceiver(Broadcast ,IntentFilter)的方式進行注冊

在 onReceive(Context context, Intent intent) 方法中進行開啟自定義屏保:

public class ScreenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //拿到屏幕鎖,亮屏,隨即釋放,否則屏幕就會保持常量
        WakeUtil.getInstance().wakeScreenLock();
        WakeUtil.getInstance().releaseScreenLock();
        //屏蔽系統(tǒng)的屏保
        mKeyguardManager = (KeyguardManager) MyApplication.getInstance().getSystemService(Context.KEYGUARD_SERVICE);
        mKeyguardLock = mKeyguardManager.newKeyguardLock("ScreenLockService");
        mKeyguardLock.disableKeyguard();
        //TODO 開啟自定義的屏保;例如:跳轉(zhuǎn)一個Activity,顯示屏保圖片

    }
}
  • 保持屏幕常量

通常,保持屏幕常量有如下方法:

  1. getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    官方注釋:
/** Window flag: as long as this window is visible to the user, keep the device's screen turned on and bright. */
        public static final int FLAG_KEEP_SCREEN_ON     = 0x00000080;

即當window對用戶可見時,保持屏幕常量。

  1. 通過PowerManager.WakeLock
mScreenWakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,
                this.getClass().getCanonicalName());
if (!mScreenWakeLock.isHeld()) {
     mScreenWakeLock.acquire();
}

對于方法newWakeLock(int levelAndFlags, String tag),下面看看該flag和level分別代表什么意義。

PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_DIM_WAKE_LOCK
        /**
         *  獲得喚醒鎖時亮屏
         * <p>
         *     正常情況下,喚醒鎖并不能真正喚醒設(shè)備,一旦屏幕已經(jīng)打開,它們就只能繼續(xù)工作。
         *     正如視頻軟件正常工作一樣。
         * </p>
         */
        public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
       /**
         *  保證亮屏狀態(tài) (可能會變暗);
         *  允許關(guān)閉背光
         * @deprecated Most applications should use
         * {@link android.view.WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON} instead
         * of this type of wake lock, as it will be correctly managed by the platform
         * as the user moves between applications and doesn't require a special permission.
         */
        @Deprecated
        public static final int SCREEN_DIM_WAKE_LOCK = 0x00000006;

可以看出該方法已經(jīng)過時,系統(tǒng)更推薦使用方法1.

3.通過設(shè)置系統(tǒng)進入屏保的時間

    try {
            Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, paramInt);
        } catch (Exception localException) {
            localException.printStackTrace();
        }

paramInt代表進入屏?;蜴i屏的時間,單位毫秒ms。這里可設(shè)置為Integer.MAX_VALUE。

同樣可以通過這種方式進行屏幕亮度(0-255)等的設(shè)置:

        try {
            Settings.System.putInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, paramInt);
        } catch (Exception localException) {
            localException.printStackTrace();
        }
  • 進入淺休眠(熄屏狀態(tài)下CPU仍在運行)
      mCpuWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getCanonicalName());

      synchronized (WakeUtil.class) {
            if (!mCpuWakeLock.isHeld()) {
                mCpuWakeLock.acquire();
            }
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 2.1 Activity 2.1.1 Activity的生命周期全面分析 典型情況下的生命周期:在用戶參與的情況下...
    AndroidMaster閱讀 3,294評論 0 8
  • 1.什么是Activity?問的不太多,說點有深度的 四大組件之一,一般的,一個用戶交互界面對應一個activit...
    JoonyLee閱讀 5,867評論 2 51
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,378評論 0 17
  • 鬼魅曾見過陽光吧,竊賊未必未被偷竊。 我作魂一樣的存在,與你的影子相愛; 我作竊賊一樣偷來,卻被偷一顆心去。 我將...
    木土有阿杜閱讀 211評論 0 1
  • 找了很多菜譜,取長補短,終于花費了一個下午完成了這道豬肉香菇燒賣。最后完成要吃的時候才發(fā)現(xiàn)整個準備流程忘記拍照了,...
    復明的瞎子閱讀 353評論 2 2

友情鏈接更多精彩內(nèi)容