1 public class NotificationTest extends Activity
2 {
3 static final int NOTIFICATION_ID = 0x123;
4 NotificationManager nm;
5
6 @Override
7 public void onCreate(Bundle savedInstanceState)
8 {
9 super.onCreate(savedInstanceState);
10 setContentView(R.layout.main);
11 // 获取系统的NotificationManager服务
12 nm = (NotificationManager)
13 getSystemService(NOTIFICATION_SERVICE);
14 }
15
16 // 为发送通知的按钮的点击事件定义事件处理方法
17 public void send(View source)
18 {
19 // 创建一个启动其他Activity的Intent
20 Intent intent = new Intent(NotificationTest.this
21 , OtherActivity.class);
22 PendingIntent pi = PendingIntent.getActivity(
23 NotificationTest.this, 0, intent, 0);
24 Notification notify = new Notification.Builder(this)
25 // 设置打开该通知,该通知自动消失
26 .setAutoCancel(true)
27 // 设置显示在状态栏的通知提示信息
28 .setTicker("有新消息")
29 // 设置通知的图标
30 .setSmallIcon(R.drawable.notify)
31 // 设置通知内容的标题
32 .setContentTitle("一条新通知")
33 // 设置通知内容
34 .setContentText("恭喜你,您加薪了,工资增加20%!")
35 // // 设置使用系统默认的声音、默认LED灯
36 // .setDefaults(Notification.DEFAULT_SOUND
37 // |Notification.DEFAULT_LIGHTS)
38 // 设置通知的自定义声音
39 .setSound(Uri.parse("android.resource://org.crazyit.ui/"
40 + R.raw.msg))
41 .setWhen(System.currentTimeMillis())
42 // 设改通知将要启动程序的Intent
43 .setContentIntent(pi).build();
44 // 发送通知
45 nm.notify(NOTIFICATION_ID, notify);
46 }
47
48 // 为删除通知的按钮的点击事件定义事件处理方法
49 public void del(View v)
50 {
51 // 取消通知
52 nm.cancel(NOTIFICATION_ID);
53 }
54 }