1: package com.halzhang.android.notification; 2:
3: import android.app.Activity;
4: import android.app.Notification;
5: import android.app.NotificationManager;
6: import android.app.PendingIntent;
7: import android.content.Intent;
8: import android.os.Bundle;
9: import android.view.View;
10: import android.widget.Button;
11: /**
12: * 通知
13: * @author 張漢國
14: */
15: public class NotificationDemo extends Activity {
16: /** Called when the activity is first created. */
17: //通知id
18: private static final int NOTICE_ID = 1222;
19: private Button notify;
20: private Button cancel;
21: @Override
22: public void onCreate(Bundle savedInstanceState) {
23: super.onCreate(savedInstanceState);
24: setContentView(R.layout.main);
25: notify = (Button) findViewById(R.id.noti);
26: cancel = (Button) findViewById(R.id.cancel);
27: notify.setOnClickListener(new View.OnClickListener() {
28:
29: @Override
30: public void onClick(View v) {
31: notityMe();
32: }
33: });
34:
35: cancel.setOnClickListener(new View.OnClickListener() {
36:
37: @Override
38: public void onClick(View v) {
39: final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
40: //取消通知
41: manager.cancel(NOTICE_ID);
42: }
43: });
44: }
45:
46: private void notityMe(){
47: //獲得通知管理器,通知是一項(xiàng)系統(tǒng)服務(wù)
48: final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
49: //初始化通知對(duì)象 p1:通知的圖標(biāo) p2:通知的狀態(tài)欄顯示的提示 p3:通知顯示的時(shí)間
50: Notification notification = new Notification(R.drawable.icon, "通知測(cè)試", System.currentTimeMillis());
51: //點(diǎn)擊通知后的Intent,此例子點(diǎn)擊后還是在當(dāng)前界面
52: PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationDemo.class), 0);
53: //設(shè)置通知信息
54: notification.setLatestEventInfo(this, "通知title", "通知信息內(nèi)容", intent);
55: //通知
56: manager.notify(NOTICE_ID, notification);
57: }
58: }