两个按钮,一个发送notification,一个删除notification。
1.notification.java中
sendNoti = (Button)findViewById(R.id.send);
delNoti = (Button)findViewById(R.id.del);
sendNoti.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//创建一个启动其他Activity的Intent
Intent intent = new Intent();
//传递键值对
intent.putExtra("testIntent", "123");
//设置召唤出的类
intent.setClass(notification.this,OtherActivity.class);
PendingIntent pi = PendingIntent.getActivity(notification.this, 0, intent, 0);
//创建一个Notification
Notification notify = new Notification(R.drawable.icon, "启动其他Activity的通知",System.currentTimeMillis());
//这个flags表示点击后,消息图标会在状态栏自动消失
notify.flags = Notification.FLAG_AUTO_CANCEL;
//为Notification设置图标,该图标显示在状态栏
//notify.icon = R.drawable.notify;
//notify.icon = R.drawable.icon;
//为Notification设置文本内容,该文本会显示在状态栏
//notify.tickerText = "启动其他Activity的通知";
//设置发送时间
//notify.when = System.currentTimeMillis();
//设置声音
//notify.defaults = Notification.DEFAULT_SOUND;
//设置默认声音,默认震动,默认闪光灯
//notify.defaults = Notification.DEFAULT_ALL;
//设置事件信息
notify.setLatestEventInfo(notification.this, "2B通知", "点击查看", pi);
//获取系统的NotificationManager服务
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//发送通知
notificationManager.notify(NOTIFICATION_ID, notify);
- //绑定delNoti监听器
delNoti.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//获取系统的Notification服务
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//取消通知
notificationManager.cancel(NOTIFICATION_ID);
2.在包中新建一个OtherActivity.java(必须在同一个包中,不然会出现无法召唤出此Activity
//设置该Activity显示的页面
setContentView(R.layout.other);
//取得从上一个Activity当中传递过来的Intent对象
Intent intent = getIntent();
//从Intent当中根据key取得value
String value = intent.getStringExtra("testIntent");
//根据控件的ID得到响应的控件对象
myTextView = (TextView)findViewById(R.id.myTextView);
//为控件设置Text值
myTextView.setText(value);
3.新建一个other.xml文件
就一行字和一幅画,字显示的是程序里传递过来的。画在res文件夹下的drawable-mdpi里放着
代码