本文转载自:https://blog.csdn.net/danwuxie/article/details/82193880

一、通知灯应用程序的编写

1、首先实现一个按钮功能

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">
 
 
 
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Flashing Light at 20S"
        android:id="@+id/button"
        />
 
</LinearLayout>

2、实现按钮的点击监听

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");
                }
                mLightHander.postDelayed(mLightRunnable, 20000);
            }
        });
3、实现延时

private Handler mLightHander = new Handler();
    private LightRunnable mLightRunnable = new LightRunnable();
 
    class LightRunnable implements Runnable {
        @Override
        public void run() {
            if (flashing) {
                FlashingLight();
            } else {
                ClearLED();
            }
        }
    }
 
   mLightHander.postDelayed(mLightRunnable, 20000);
4、实现通知

private void FlashingLight()
    {
        NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
        Notification notif = new Notification();
        notif.flags = Notification.FLAG_SHOW_LIGHTS;
        notif.ledARGB = 0xFF0000ff;
        notif.ledOnMS = 100;
        notif.ledOffMS = 100;
        nm.notify(LED_NOTIFICATION_ID, notif);
    }
5、取消通知

private void ClearLED()
    {
        NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
        nm.cancel(LED_NOTIFICATION_ID);
    }
6、实现点击延时

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");
                }
                mLightHander.postDelayed(mLightRunnable, 20000);
            }
        });
二、测试

用法:
1. 先在单板上"Setting"->"Display"->"Sleep"设为"15S"
2. 运行程序
3. 点击按钮后不再操作
4. 等屏幕再次变黑即可看到通知灯闪烁
 
注意:黑屏期间可以通过menu键或K1键返回程序界面
三、程序

package com.thisway.app_0002_lightdemo;
 
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.app.NotificationManager;
import android.app.Notification;
import android.view.View;
 
public class MainActivity extends AppCompatActivity {
 
    private Button mLightButton = null;
    boolean flashing = false;
    final private int LED_NOTIFICATION_ID = 123;
 
    private Handler mLightHander = new Handler();
    private LightRunnable mLightRunnable = new LightRunnable();
 
    class LightRunnable implements Runnable {
        @Override
        public void run() {
            if (flashing) {
                FlashingLight();
            } else {
                ClearLED();
            }
        }
    }
 
    private void FlashingLight()
    {
        NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
        Notification notif = new Notification();
        notif.flags = Notification.FLAG_SHOW_LIGHTS;
        notif.ledARGB = 0xFF0000ff;
        notif.ledOnMS = 100;
        notif.ledOffMS = 100;
        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");
                }
                mLightHander.postDelayed(mLightRunnable, 20000);
            }
        });
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
 
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
 
        return super.onOptionsItemSelected(item);
    }
}
 
 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
 
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Flashing Light at 20S"
        android:id="@+id/button" />
 
 
</LinearLayout>

Android灯光系统通知灯【转】的更多相关文章

  1. Android灯光系统--通知灯深入分析【转】

    本文转自:https://www.cnblogs.com/lkq1220/p/6406261.html Android灯光系统--通知灯深入分析 通知的类别 声音 振动 闪灯 APP如何发出通知灯请求 ...

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

    Android灯光系统--通知灯深入分析 通知的类别 声音 振动 闪灯 APP如何发出通知灯请求 getSystemService(获得通知服务) 构造notification 类别 其他参数(颜色, ...

  3. Android · 广告走灯

    layout <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...

  4. 013 Android 实现流水灯功能+自定义控件的样式(可以复用)

    1.介绍 (1)获取屏幕的焦点 android:focusable与android:focusableInTouchMode(获取屏幕焦点) 前者针对在键盘下操作的情况,如果设置为true,则键盘上下 ...

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

    Android通知灯的深入分析 通知的类别 声音 振动 闪灯 APP如何发出通知灯请求 getSystemService(通知服务) 构造notification 类别 其他参数(颜色,onMS,of ...

  6. Android开发-之认识palette

    Android开发中,Google工程师已经给我们封装好了很多的按钮,使得我们在开发中非常的方便和便捷. 那么今天就来认识一下常用的按钮,那么在之前的课程中我已经详细讲过了Button按钮,那么这里就 ...

  7. Android仿微信UI布局视图(圆角布局的实现)

    圆角button.或布局能够在xml文件里实现,但也能够使用图片直接达到所需的效果,曾经版本号的微信就使用了这样的方法. 实现效果图:    watermark/2/text/aHR0cDovL2Js ...

  8. android客服端+eps8266+单片机+路由器之远程控制系统

    用android客服端+eps8266+单片机+路由器做了一个远程控制的系统,因为自己是在实验室里,所以把实验室的门,灯做成了远程控制的. 控制距离有多远------只能说很远很远,只要你手机能上网的 ...

  9. Android Environment 获取各种路径的方法

    <pre name="code" class="java">package com.deepoon.beyond.environment; impo ...

随机推荐

  1. selenium获取文本

    # 标题list_title = driver.find_elements_by_xpath('//*[@id="share-content"]/div/div[1]/ul/li/ ...

  2. day1 hbuilder的使用

    一.互联网的原理 1.概述 html:用来制作网页. 互联网原理:上网即请求数据. 用户通过在浏览器上输入一个网址,通过HTTP协议向服务器发送请求,服务器做出响应,将相关的网页数据传输到本地计算机, ...

  3. sublime text3配置及相关小技巧

    1.下载&安装: 官方地址:http://www.sublimetext.com/,sublime text3又更新了,支持不依赖插件进行侧边栏颜色的更改,同时自带的皮肤颜色也有四种,十分方便 ...

  4. Python - 1. Built-in Atomic Data Types

    From:http://interactivepython.org/courselib/static/pythonds/Introduction/GettingStartedwithData.html ...

  5. 类中静态成员变量 && 无法解析的外部符号

    [1]如下代码及编译错误 如标题,不做赘述. [2]原因及解决方案 原因:之所以报如上编译错误,因为静态成员变量未初始化. 解决方案:类中静态成员需要在类外进行初始化.其格式为:类型 类名::静态成员 ...

  6. 设计模式之Visitor(访问者)(转)

    Visitor定义 作用于某个对象群中各个对象的操作. 它可以使你在不改变这些对象本身的情况下,定义作用于这些对象的新操作. 在Java中,Visitor模式实际上是分离了collection结构中的 ...

  7. JAVA基础3---运算符大全

    Java中的运算符有以下种类:算术运算符.关系运算符.位运算符.逻辑运算符.赋值运算符.其他的运算符 现在假设定义 int A = 10,B = 5: 一.算术运算符 运算符 描述 案例 + 等同于数 ...

  8. 囤币一族,被中国市场遗忘的价值币ADA

    囤币一族,被中国市场遗忘的价值币ADA ==========================长期囤币目标:trx十万个,ada一万个,eos五千个,nas一千个,ont一千个,eth一百个,比特币十个 ...

  9. animate和translate

    transition, transform, tanslate,animation分别为过渡,变换,平移.动画.transform的属性包括:rotate() / skew() / scale() / ...

  10. SpringMVC实现 MultipartFile 文件上传

    1. Maven 工程引入所需要的依赖包 2. 页面需要开放多媒体标签 3. 配置文件上传试图解析器 4. 接收图片信息,通过 IO 流写入磁盘(调用解析其中的方法即可) 如下: 1.1 引入所依赖的 ...