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>