您的位置:首页 > 移动开发 > Android开发

Android 通知栏点击取消

2015-11-12 14:54 381 查看
看了很多文章写了怎么取消通知栏的,自己试了就是取消不掉。后来尝试之后实现了,代码写错了。

public class LoginSuccess  extends Activity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success_login);
Toast.makeText(LoginSuccess.this, "登陆成功", 2000).show();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("通知栏标题 ")
.setContentText("通知内容")
//        		.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))//点击意图
.setTicker("通知首次出现在通知栏,带上升动画效果的")
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setPriority(Notification.PRIORITY_DEFAULT)
.setAutoCancel(true)//用户点击就自动消失
.setOngoing(true)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
//Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
.setSmallIcon(R.drawable.taobao);//设置通知小ICON
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(100, notification);
//        notificationManager.cancel(100);//通知以后自动消失了

}

}


错误的代码是:其实已经不是一个对象了。

Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(100, builder.build());


推荐:/article/1970429.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: