Android燈光系統--通知燈深入分析


Android燈光系統--通知燈深入分析

通知的類別

  • 聲音

  • 振動

  • 閃燈

APP如何發出通知燈請求

  1. getSystemService(獲得通知服務)

  2. 構造notification

    • 類別

    • 其他參數(顏色,onMS,offMS)

  3. 發出通知

系統如何處理

  1. 啟動通知Service

  2. 收到通知之后

    • 分辨通知類型

    • 執行響應操作

  3. 對於通知燈

    • 獲得LightService

    • 執行燈光相關操作

APP如何獲得通知服務

  1. ContextImp:resigsterService

  2. 返回一個NotificationManager對象

  3. 構造Notification

  4. NotificationManager.notify()將通知發送出去

發送通知之后如何調用通知燈

  1. Service=getService() //獲得某個服務

    • 注冊有Notification服務

    • 根據名字Notification獲得Service服務

  2. Service.enqueueNotificationwithTag //放入通知隊列

  3. 通過enqueueNotificationwithTag中的buzzBeepBlinkLocked判斷是否是屬於哪種通知類別

  4. 獲得通知屬於閃燈,調用updateLightsLocked()

  5. 取出notification當中的參數,調用mNotificationLights類當中的setFlashing

    • 注冊LightManager服務

    • 根據ID從LightManager中返回獲取mNotificationLights類

編寫模擬通知燈安卓程序

  1. 定義按鈕,控制20S之后熄屏亮燈

    • 定義Flashing boolean型變量,用於控制按鈕操作

    • 設置按鈕響應函數--判斷按鈕操作,改變按鈕text值,並且發出通知

  2. 構造通知執行方法 - 實現Runnable接口方法

    • 獲得按鈕狀態

      • 調用開通知燈函數

      • 獲得通知服務

      • 構造通知,設置參數

      • 發送通知

    • 關閉通知燈函數

      • 獲得通知服務

      • 取消通知燈服務

  3. 通知

    • 延遲20S通知調用postDelayed函數

附上詳細代碼:


package com.example.alienware.app_0002_lightdemo;

import android.app.Notification;

import android.app.NotificationManager;

import android.os.Handler;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.Button;

import android.view.View;

/*

* 模擬熄屏時候,短信等通知發生時候,通知燈亮起

* 設置屏幕背光亮時間為15s,才可以進行下列實驗

* Date:2017.2.16  Author:LKQ

* 代碼原創者:韋東山老師

*/

public class MainActivity extends AppCompatActivity {

    private Button mLightButton = null;

    boolean Flashing = false;

    final private int LED_NOTIFICATION_ID = 109;

    private Handler mLightHandler = new Handler();

    private LightRunnable mLightRunnable = new LightRunnable();

    //實現消息通知后的執行方法

    class LightRunnable implements Runnable{

        @Override

        public void run() {

            if(Flashing){

                BlueFlashLight();  //藍燈閃亮

            }

            else{

                ClearLED();       //關閉通知燈

            }

        }

    }

    private void BlueFlashLight()

    {

        NotificationManager nm = (NotificationManager)getSystemService( NOTIFICATION_SERVICE );  //獲取通知服務

        Notification notif = new Notification();      //構造通知類型

        notif.flags = Notification.FLAG_SHOW_LIGHTS;  //設置通知類型為通知燈

        notif.ledARGB = 0xFF0000ff;                   //顏色

        notif.ledOnMS = 1000;

        notif.ledOffMS = 1000;                        //閃爍時間為1S

        nm.notify(LED_NOTIFICATION_ID, notif);        //發送通知

    }

    private void ClearLED()

    {

        NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );

        nm.cancel( LED_NOTIFICATION_ID );             //關閉通知燈

    }

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mLightButton = (Button)findViewById(R.id.button);

        mLightButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                // Perform action on click

                Flashing = !Flashing;

                if (Flashing) {

                    mLightButton.setText("Stop Flashing the Light !");

                } else {

                    mLightButton.setText("Flashing Light at 20S");

                }

                mLightHandler.postDelayed(mLightRunnable, 20000);  //20S之后,即是熄屏時候,通知燈閃爍

            }

        });

    }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM