请查看我的代码:
package com.yarin.android.Examples_04_23;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity01 extends Activity {
Button m_Button1;
//声明通知(消息)管理器
NotificationManager m_NotificationManager;
Intent m_Intent;
PendingIntent m_PendingIntent;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化NotificationManager对象
m_NotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//获取按钮对象
m_Button1 = (Button) findViewById(R.id.Button01);
//点击通知时转移内容
m_Intent = new Intent(Activity01.this, Activity02.class);
//主要是设置点击通知时显示内容的类
m_PendingIntent = PendingIntent.getActivity(Activity01.this, 0, m_Intent, 0);
m_Button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Notification m_Notification = new Notification();
//设置通知在状态栏显示的图标
m_Notification.icon = R.drawable.img1;
//当我们点击通知时显示的内容
m_Notification.tickerText = "Button1通知内容...........";
//通知时发出默认的声音
m_Notification.defaults = Notification.DEFAULT_SOUND;
//设置通知显示的参数
m_Notification.setLatestEventInfo(Activity01.this, "Button1", "Button1通知", m_PendingIntent);
//可以理解为执行这个通知
m_NotificationManager.notify(0, m_Notification);
}
});
}
}
当用户点击按钮click
时,应用栏上会显示通知。但是,在用户点击后,通知将始终保持
答案 0 :(得分:5)
只需在您的m_Button1点击监听器中添加一行:
m_Notification.defaults = Notification.DEFAULT_SOUND;
m_Notification.flags |= Notification.FLAG_AUTO_CANCEL;
答案 1 :(得分:4)
Notification notification = new Notification();
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL
来自文档
“如果在用户点击通知时应取消通知,则应将位置按位进入标志字段”