zoukankan      html  css  js  c++  java
  • Android灯光系统通知灯【转】

    本文转载自: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>

  • 相关阅读:
    使用 requests 维持会话
    使用 requests 发送 POST 请求
    使用 requests 发送 GET 请求
    requests 安装
    使用 urllib 分析 Robots 协议
    使用 urllib 解析 URL 链接
    使用 urllib 处理 HTTP 异常
    使用 urllib 处理 Cookies 信息
    使用 urllib 设置代理服务
    按单生产程序发布
  • 原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/10199094.html
Copyright © 2011-2022 走看看