package com.tarena.notification.demo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RemoteViews;
import android.widget.TextView;
public class NotificationDemoActivity extends Activity {
/** Called when the activity is first created. */
// 通知的唯一标示,建议取一个不太常用的数字
public static final int NOTIFICATION_ID = 123321456;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(this);
textView.setText("演示生成通知。");
Button button = new Button(this);
button.setText("更新播放信息");
layout.addView(textView);
layout.addView(button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 找到通知的管理者
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 创建一个通知
Notification notification = new Notification(R.drawable.no_cd,
"收到播放音乐更新通知", System.currentTimeMillis());
// 指定这个通知的布局文件
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.notification);
// 设置通知显示的内容
remoteViews.setImageViewResource(R.id.image, R.drawable.no_cd);
remoteViews.setTextViewText(R.id.text, "专辑Ablume1的歌曲:myMusic");
// 将内容指定给通知
notification.contentView = remoteViews;
// 指定点击通知后跳到那个Activity
notification.contentIntent = PendingIntent.getActivity(
NotificationDemoActivity.this, 0, new Intent(
NotificationDemoActivity.this,
NotificationDemoActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
// //或者启动一个Service
// notification.contentIntent=PendingIntent.getService(
// NotificationDemoActivity.this, 0,new
// Intent(NotificationDemoActivity.this,NotificationDemoActivity.class),
// PendingIntent.FLAG_UPDATE_CURRENT);
// //或者启动一个Broadcast广播
// notification.contentIntent=PendingIntent.getBroadcast(
// NotificationDemoActivity.this, 0,new
// Intent(NotificationDemoActivity.this,NotificationDemoActivity.class),
// PendingIntent.FLAG_UPDATE_CURRENT);
// 指定通知可以清除
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// 指定通知不能清除
// notification.flags|=Notification.FLAG_NO_CLEAR;
// 通知显示的时候播放默认声音
notification.defaults |= Notification.DEFAULT_SOUND;
// 其实通知也能支持震动的
// 需要加入震动权限
//<uses-permission android:name="android.permission.VIBRATE"/>
// 如何修改 Notification 的震动,以重复的方式 1 秒震动、 1 秒停止,共 5 秒。
long[] vibrate = new long[] { 1000, 1000, 1000, 1000, 1000 };
notification.vibrate = vibrate;
// 手机闪光 (Notification.FLAG_SHOW_LIGHTS)
// Notification 也包含属性来设置手机 LED 的颜色和闪烁频率
// ledARGB 属性用于设置 LED 的颜色,而 ledOffMS 和 ledOnMS 属性用来设置 LED
// 闪烁的频率和样式。
// 你可以设置 ledOnMS 属性为 1 , ledOffMS 属性为 0 来让 LED 始终亮着;
// 或者将两者设置为 0 来将 LED 关闭。
// 一旦你设置了 LED 的设定,你也必须为 Notification 的 flags 属性添加
// FLAG_SHOW_LIGHTS 标志位。
// 接下来的代码片段显示了如何将点亮红色的 LED :
//简单的直接设置某一个颜色
notification.ledARGB = Color.RED;
//复杂的设置自己的颜色--前提是手机支持,不然不起作用
notification.ledARGB = 0xaabbccdd;
notification.ledOffMS = 0;
notification.ledOnMS = 1;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
// 向NotificationManager注册一个notification,并用NOTIFICATION_ID作为管理的唯一标示
manager.notify(NOTIFICATION_ID, notification);
}
});
this.setContentView(layout);
}
}