android清除通知栏消息的代码

android清除通知栏消息的代码
这近项目快到结尾了,经理要我处理一个问题,就是我们程序关闭后,程序发出通知在状态栏上始终没有消除,需要手动的清楚,
体验效果极其不好,现在是想在程序推出后,把通知给消除了,琢磨了下,不知怎么清楚,看了下api有清除的方法,后面安心多了
,但有出现毛病了,我什么调用通知管理器把通知消除啊,他是开一个一个服务中的,我们不能new这个类,是系统的,当时想了下
决定发送广播清楚,当程序退出的时候,调用该广播把消息清楚,等到快写完的时候,才发现,既然是系统调用的,系统肯定有结束的回调啊
立马想到了ondesory()方法,因为我程序不管怎么退出,都会调用该方法,而且省了我很大的一笔功夫,代码也就一行!
写这个只想告诉自己,在应用系统的东西时候,我们应该遵循系统的规则进行游戏,该创建的时候就要创建,该释放的地方就要释放!
看来对系统的生命周期认识还不是很到位啊!
说了这么多废话,也贴上测试代码,随便写的
ification;
importjava.util.ArrayList;
3d打印建筑importandroid.app.Activity;
importandroid.app.Notification;
importandroid.app.NotificationManager;
importandroid.app.PendingIntent;
t.Intent;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;教学磁板
importandroid.util.Base64;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.LinearLayout;
importandroid.widget.TextView;
publicclassMainActivityextendsActivity{
privateinttype;
privateStringmessage;
privateNotificationManagernotiManager;
过滤减压器
Handlerhandler=newHandler(){
publicvoidhandleMessage(Messagemsg){
调漆设备//TODOAuto-generatedmethodstub
if(msg.what==1){
message="type====1";
}elseif(msg.what==2){
message="type====2";
}
notiManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notificationnotification=newNotification(R.drawable.icon,
"您有新消息"+type,System.currentTimeMillis());
Intentintent=newIntent();环保柴火灶
intent.setClass(getApplicationContext(),ReciveActivity.class);
intent.putExtra("hello","helloDialog");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
|Intent.FLAG_ACTIVITY_NEW_TASK);
Activity(
getApplicationContext(),100,intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(getApplicationContext(),type+"",
message,pendingIntent);
ify(0,notification);
}
};
@Override
publicvoidonCreate(BundlesavedInstanceState){
Create(savedInstanceState);
LinearLayoutlayout=newLinearLayout(this);
Buttonbutton=newButton(this);
button.setText("测试notification1");
button.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
type=1;
handler.sendEmptyMessage(1);
}
});
Buttonbutton2=newButton(this);
button2.setText("测试notification2");
button2.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
//handler.sendEmptyMessage(2);
//type=2;
notiManager.cancel(0);//调用他就行这个0notiManager创建时候传入的0是唯一的,所以可以根据他删除
}
});
layout.addView(button);
layout.addView(button2);
setContentView(layout);
}
lc谐振}

本文发布于:2024-09-22 13:32:00,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/328700.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:系统   通知   程序
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议