Android灯光系统--通知灯深入分析

通知的类别

  • 声音
  • 振动
  • 闪灯

APP如何发出通知灯请求

  1. getSystemService(获得通知服务)
  2. 构造notification
  • 类别
  • 其他参数(颜色,onMS,offMS)
  1. 发出通知

系统如何处理

  1. 启动通知Service
  2. 收到通知之后
  • 分辨通知类型
  • 执行响应操作
  1. 对于通知灯
  • 获得LightService
  • 执行灯光相关操作

APP如何获得通知服务

  1. ContextImp:resigsterService
  2. 返回一个NotificationManager对象
  3. 构造Notification
  4. NotificationManager.notify()将通知发送出去

发送通知之后如何调用通知灯

  1. Service=getService() //获得某个服务
  • 注册有Notification服务
  • 根据名字Notification获得Service服务
  1. Service.enqueueNotificationwithTag //放入通知队列
  2. 通过enqueueNotificationwithTag中的buzzBeepBlinkLocked判断是否是属于哪种通知类别
  3. 获得通知属于闪灯,调用updateLightsLocked()
  4. 取出notification当中的参数,调用mNotificationLights类当中的setFlashing
  • 注册LightManager服务
  • 根据ID从LightManager中返回获取mNotificationLights类

编写模拟通知灯安卓程序

  1. 定义按钮,控制20S之后熄屏亮灯
  • 定义Flashing boolean型变量,用于控制按钮操作
  • 设置按钮响应函数--判断按钮操作,改变按钮text值,并且发出通知
  1. 构造通知执行方法 - 实现Runnable接口方法
  • 获得按钮状态
  • 调用开通知灯函数
  • 获得通知服务
  • 构造通知,设置参数
  • 发送通知
  • 关闭通知灯函数
  • 获得通知服务
  • 取消通知灯服务
  1. 通知
  • 延迟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之后,即是熄屏时候,通知灯闪烁

            }

        });

    }

}


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之后,即是熄屏时候,通知灯闪烁

            }

        });

    }

}