Android编程之通知栏的用法小结
本文实例总结了android编程中通知栏的用法。分享给大家供大家参考,具体如下:
很久没有使用android的通知功能了,今天把两年前的代码搬出来一看,发现很多方法都废弃了,代码中各种删除线看的十分不爽。于是乎,打开google,查看官方文档,学习最新的发送通知栏消息的方法。
本文中的代码均参照谷歌官方文档编写:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
1.首先,获取系统的通知服务:
2.发送一个最简单的通知
public void simplenotice(view view) { //此builder为android.support.v4.app.notificationcompat.builder中的,下同。 builder mbuilder = new builder(this); //系统收到通知时,通知栏上面显示的文字。 mbuilder.setticker("天津,晴,2~15度,微风"); //显示在通知栏上的小图标 mbuilder.setsmallicon(r.drawable.consult_answer); //通知标题 mbuilder.setcontenttitle("天气预报"); //通知内容 mbuilder.setcontenttext("天津,晴,2~15度,微风"); //设置大图标,即通知条上左侧的图片(如果只设置了小图标,则此处会显示小图标) mbuilder.setlargeicon(bitmapfactory.decoderesource(getresources(), r.drawable.share_sina)); //显示在小图标左侧的数字 mbuilder.setnumber(6); //设置为不可清除模式 mbuilder.setongoing(true); //显示通知,id必须不重复,否则新的通知会覆盖旧的通知(利用这一特性,可以对通知进行更新) nm.notify(1, mbuilder.build()); }
3.删除一个通知。参数即为通知的id
nm.cancel(1);
4.发送一个通知,点击通知后跳转到一个activity,从这个activity返回后,进入程序内的某一个页面(一般为主页)
//点击通知进入一个activity,点击返回时进入指定页面。 public void resultactivitybackapp(view view) { builder mbuilder = new builder(this); mbuilder.setticker("通知标题2"); mbuilder.setsmallicon(r.drawable.ic_launcher); mbuilder.setcontenttitle("通知标题2"); mbuilder.setcontenttext("点击通知进入一个activity,点击返回时进入指定页面。"); //设置点击一次后消失(如果没有点击事件,则该方法无效。) mbuilder.setautocancel(true); //点击通知之后需要跳转的页面 intent resultintent = new intent(this, resultactivitybackapp.class); //使用taskstackbuilder为“通知页面”设置返回关系 taskstackbuilder stackbuilder = taskstackbuilder.create(this); //为点击通知后打开的页面设定 返回 页面。(在manifest中指定) stackbuilder.addparentstack(resultactivitybackapp.class); stackbuilder.addnextintent(resultintent); pendingintent pintent = stackbuilder.getpendingintent(0, pendingintent.flag_update_current); mbuilder.setcontentintent(pintent); // mid allows you to update the notification later on. nm.notify(2, mbuilder.build()); }
同时,需要在manifest中为点击通知后打开的activity指定父activity.
<activity android:name=".resultactivitybackapp" android:parentactivityname=".mainactivity"> <meta-data android:name="android.support.parent_activity" android:value=".mainactivity" /> </activity>
(其中,activity的属性parentactivityname为api 16中的属性,meta-data中的代码为兼容api 16以下。因此,对于大多数程序,这两个地方都得写。)
5.和上述4类似,只是在打开的activity中返回时回到home页
//点击通知进入一个activity,点击返回时回到桌面 public void resultactivitybackhome(view view) { builder mbuilder = new builder(this); mbuilder.setticker("通知标题3"); mbuilder.setsmallicon(r.drawable.ic_launcher); mbuilder.setcontenttitle("通知标题3"); mbuilder.setcontenttext("点击通知进入一个activity,点击返回时回到桌面"); //设置点击一次后消失(如果没有点击事件,则该方法无效。) mbuilder.setautocancel(true); intent notifyintent = new intent(this, resultactivitybackhome.class); notifyintent.setflags(intent.flag_activity_new_task); pendingintent pintent = pendingintent.getactivity(this, 0, notifyintent, pendingintent.flag_update_current); mbuilder.setcontentintent(pintent); nm.notify(3, mbuilder.build()); }
6.带进度条的通知
public void progressnotice(view view) { final builder mbuilder = new builder(this); mbuilder.setticker("通知标题4"); mbuilder.setcontenttitle("picture download") .setcontenttext("download in progress") .setsmallicon(r.drawable.ic_launcher); // start a lengthy operation in a background thread new thread(new runnable() { @override public void run() { int progress; for (progress = 0; progress <= 100; progress++) { // sets the progress indicator to a max value, the current completion percentage, // and "determinate" state mbuilder.setprogress(100, progress, false); //不明确进度的进度条 // mbuilder.setprogress(0, 0, true); nm.notify(4, mbuilder.build()); // 模拟延时 try { thread.sleep(200); } catch (interruptedexception e) { e.printstacktrace(); } } // when the loop is finished, updates the notification mbuilder.setcontenttext("download complete"); // removes the progress bar mbuilder.setprogress(0, 0, false); nm.notify(4, mbuilder.build()); } } ).start(); }
7.扩展布局的通知。按住通知条下滑,可以查看更详细的内容
public void expandlayoutnotice(view view) { builder mbuilder = new builder(this); mbuilder.setticker("通知标题5"); mbuilder.setsmallicon(r.drawable.ic_launcher); mbuilder.setcontenttitle("通知标题5"); mbuilder.setcontenttext("按住通知下拉可显示扩展布局"); notificationcompat.inboxstyle inboxstyle = new notificationcompat.inboxstyle(); string[] events = new string[]{"beijing", "tianjin", "shanghai", "guangzhou"}; // 设置扩展布局的标题 inboxstyle.setbigcontenttitle("event tracker details:"); for (string s : events) { inboxstyle.addline(s); } mbuilder.setstyle(inboxstyle); nm.notify(5, mbuilder.build()); }
8.自定义布局的通知栏。(根据谷歌的官方文档不推荐这么做,因为使用这种方式时,对不同屏幕进行适配需要考虑的因素太多。而且,通知栏应该展示的就是最简明扼要的信息,对于大多数程序默认的布局已经足够了。)
//自定义布局的通知 public void customlayoutnotice(view view) { builder mbuilder = new builder(this); mbuilder.setticker("通知标题6"); mbuilder.setticker("通知标题6"); mbuilder.setsmallicon(r.drawable.ic_launcher); remoteviews remoteviews = new remoteviews(getpackagename(), r.layout.custom_layout_notice); mbuilder.setcontent(remoteviews); //为remoteviews上的按钮设置文字 remoteviews.setcharsequence(r.id.custom_layout_button1, "settext", "button1"); remoteviews.setcharsequence(r.id.custom_layout_button2, "settext", "button2"); //为remoteviews上的按钮设置点击事件 intent intent1 = new intent(this, customlayoutresultactivity.class); intent1.putextra("content", "from button1 click!"); pendingintent pintentbutton1 = pendingintent.getactivity(this, 0, intent1, pendingintent.flag_update_current); remoteviews.setonclickpendingintent(r.id.custom_layout_button1, pintentbutton1); intent intent2 = new intent(this, customlayoutresultactivity.class); intent2.putextra("content", "from button2 click!"); pendingintent pintentbutton2 = pendingintent.getactivity(this, 1, intent2, pendingintent.flag_update_current); remoteviews.setonclickpendingintent(r.id.custom_layout_button2, pintentbutton2); nm.notify(6, mbuilder.build()); }
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
推荐阅读