当前位置: 移动技术网 > 移动技术>移动开发>Android > android通知栏的实现方法分析

android通知栏的实现方法分析

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android通知栏的实现方法。分享给大家供大家参考,具体如下:

这几天一直在修改twigee的源代码,其中一个要加入的功能是常驻notification栏,以前写的时候只能出现 在“通知”这一组中,想把它放在“正在运行”组中却不知道怎么放,查了下官方文档,找到了方法,在notification的flags字段中加一下 “flag_ongoing_event”就可以了。同时我也把notification的使用方法给总结了一下。详见下文:

(1)、使用系统定义的notification

以下是使用示例代码:

//创建一个notificationmanager的引用
string ns = context.notification_service;
notificationmanager mnotificationmanager = (notificationmanager)getsystemservice(ns);
// 定义notification的各种属性
int icon = r.drawable.icon; //通知图标
charsequence tickertext = "hello"; //状态栏显示的通知文本提示
long when = system.currenttimemillis(); //通知产生的时间,会在通知信息里显示
//用上面的属性初始化 nofification
notification notification = new notification(icon,tickertext,when);
/*
* 添加声音
* notification.defaults |=notification.default_sound;
* 或者使用以下几种方式
* notification.sound = uri.parse("file:///sdcard/notification/ringer.mp3");
* notification.sound = uri.withappendedpath(audio.media.internal_content_uri, "6");
* 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"flag_insistent"
* 如果notification的defaults字段包括了"default_sound"属性,则这个属性将覆盖sound字段中定义的声音
*/
/*
* 添加振动
* notification.defaults |= notification.default_vibrate;
* 或者可以定义自己的振动模式:
* long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒
* notification.vibrate = vibrate;
* long数组可以定义成想要的任何长度
* 如果notification的defaults字段包括了"default_vibrate",则这个属性将覆盖vibrate字段中定义的振动
*/
/*
* 添加led灯提醒
* notification.defaults |= notification.default_lights;
* 或者可以自己的led提醒模式:
* notification.ledargb = 0xff00ff00;
* notification.ledonms = 300; //亮的时间
* notification.ledoffms = 1000; //灭的时间
* notification.flags |= notification.flag_show_lights;
*/
/*
* 更多的特征属性
* notification.flags |= flag_auto_cancel; //在通知栏上点击此通知后自动清除此通知
* notification.flags |= flag_insistent; //重复发出声音,直到用户响应此通知
* notification.flags |= flag_ongoing_event; //将此通知放到通知栏的"ongoing"即"正在运行"组中
* notification.flags |= flag_no_clear; //表明在点击了通知栏中的"清除通知"后,此通知不清除,
* //经常与flag_ongoing_event一起使用
* notification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部
* //如果要使用此字段,必须从1开始
* notification.iconlevel = ; //
*/
//设置通知的事件消息
context context = getapplicationcontext(); //上下文
charsequence contenttitle = "my notification"; //通知栏标题
charsequence contenttext = "hello world!"; //通知栏内容
intent notificationintent = new intent(this,main.class); //点击该通知后要跳转的activity
pendingintent contentintent = pendingintent.getactivity(this,0,notificationintent,0);
notification.setlatesteventinfo(context, contenttitle, contenttext, contentintent);
//把notification传递给 notificationmanager
mnotificationmanager.notify(0,notification);

如果想要更新一个通知,只需要在设置好notification之后,再次调用 setlatesteventinfo(),然后重新发送一次通知即可,即再次调用notify()。

(2)、使用自定义的 notification

要 创建一个自定义的notification,可以使用remoteviews。要定义自己的扩展消息,首先 要初始化一个remoteviews对象,然后将它传递给notification的contentview字段,再把pendingintent传递给 contentintent字段。以下示例代码是完整步骤:

1、创建一个自 定义的消息布局 view.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<imageview android:id="@+id/image" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginright="10dp" />
<textview android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textcolor="#000" />
</linearlayout>

2、 在程序代码中使用remoteviews的方法来定义image和text。然后把remoteviews对象传到contentview字段

remoteviews contentview = new remoteviews(getpackagename(),r.layout.view);
contentview.setimageviewresource(r.id.image,r.drawable.icon);
contentview.settextviewtext(r.id.text,”hello,this message is in a custom expanded view”);
notification.contentview = contentview;

3、 为notification的contentintent字段定义一个intent(注意,使用自定义view不需要 setlatesteventinfo()方法)

intent notificationintent = new intent(this,main.class);
pendingintent contentintent = pendingintent.getactivity(this,0,notificationintent,0);
notification.contentintent = contentintent;

4、发送通知

mnotificationmanager.notify(2,notification);

以下是全部示例代码:

//创建一个 notificationmanager的引用
string ns = context.notification_service;
notificationmanager mnotificationmanager = (notificationmanager)getsystemservice(ns);
// 定义notification的各种属性
int icon = r.drawable.icon; //通知图标
charsequence tickertext = "hello"; //状态栏显示的通知文本提示
long when = system.currenttimemillis(); //通知产生的时间,会在通知信息里显示
//用上面的属性初始化 nofification
notification notification = new notification(icon,tickertext,when);
remoteviews contentview = new remoteviews(getpackagename(),r.layout.view);
contentview.setimageviewresource(r.id.image, r.drawable.iconempty);
contentview.settextviewtext(r.id.text, "hello,this is jc");
notification.contentview = contentview;
intent notificationintent = new intent(this,main.class);
pendingintent contentintent = pendingintent.getactivity(this,0,notificationintent,0);
notification.contentintent = contentintent;
//把notification传递给notificationmanager
mnotificationmanager.notify(0,notification);

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网