1. 程式人生 > >Android燈光系統--通知燈深入分析【轉】

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

本文轉自:https://www.cnblogs.com/lkq1220/p/6406261.html

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之後,即是熄屏時候,通知燈閃爍 } }); } }