- 浏览: 579145 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (182)
- android/ophone开发完全讲义 (4)
- android常用代码 (6)
- android工具 (9)
- android基础 (40)
- android进阶 (75)
- android经验记录 (3)
- android框架 (2)
- android面经 (5)
- android网络 (5)
- android资讯 (0)
- android资源 (17)
- 生活印象 (3)
- androidNDK (4)
- android开源 (1)
- android性能优化 (1)
- android设计 (2)
- android博客论坛收集 (1)
- android手机用户体验 (1)
- android动画 (1)
- android外文资料 (1)
- 汉字排序 (1)
最新评论
-
小猫咪201:
困惑了很久,终于找到答案了
一个常见的android内存泄露 问题 -
王雪龙:
pixels = dps * (density / 160). ...
关于Android的nodpi,xhdpi,hdpi,mdpi,ldpi -
Turr:
楼主给力,解决了我遇到的问题
解决ActivityGroup的sub Activity中spinner的WindowManager$BadTokenException的问题 -
boyuan2000cn:
你好,我转载了您的此篇博客,如果有版权问题,请告诉我,谢谢!转 ...
关于Android的nodpi,xhdpi,hdpi,mdpi,ldpi -
twlkyao:
请教您个问题,如何将drawable对象转换为id类型?
Android中Bitmap, Drawable, Byte,ID之间的转化
NotificationManager和Notification的使用总结
- 博客分类:
- android进阶
这几天一直在修改twigee的源代码,其中一个要加入的功能是常驻Notification栏,以前写的时候只能出现在“通知”这一组中,想把它放在“正在运行”组中却不知道怎么放,查了下官方文档,找到了方法,在notification的flags字段中加一下“FLAG_ONGOING_EVENT”就可以了。同时我也把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()。 要创建一个自定义的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);
(1)、使用系统定义的Notification
(2)、使用自定义的Notification
发表评论
-
Android 检查当前是否已经连接上 Internet
2012-02-27 09:28 1java代码: 1 ConnectivityManager ... -
对ContentProvider中getType(Uri uri)和android.intent.category.DEFAULT的理解
2012-02-27 09:21 1582学习了ContentProvider,想做个通讯录,结果 ... -
Android ContentProvider的介绍(很详细)
2012-02-21 17:46 139563一、ContentProvider的概念 Content ... -
一个常见的android内存泄露 问题
2012-02-20 16:11 4896最近在公司看一个算比较大的android项目的源码, ... -
一个常见的android内存泄露 问题
2012-02-20 16:10 0最近在公司看一个算比较大的android项目的源码,发 ... -
Android网络电台的一种实现方案
2012-02-20 15:12 3643随着电子产品的飞速 ... -
android 如何判断程序是否在前台运行
2012-02-16 15:31 5418private boolean isTopActivit ... -
Android Intent和PendingIntent的区别详细分析
2012-02-16 14:08 1514刚才一个例子中用到了PendingIntent,与之前学过的I ... -
编码问题(UTF-8、gb2312、unicode)
2012-02-14 09:38 1907相信大家很多人跟我一样,对于编码一直感觉云山雾罩,说知道吧?就 ... -
关于androidSDK登录时出现oauth2.0空白页的BUG
2012-02-09 16:21 1382在RenrenListenerFactory中,line 12 ... -
java中yield(),sleep()以及wait()的区别
2012-01-31 18:41 920往往混淆了这三个函数的使用。 从操作系统的角度讲, ... -
Toast大全(五种情形)建立属于你自己的Toast
2012-01-29 17:23 1551Toast用于向用户显示一些帮助/提示。下面我做了5中效果 ... -
Android主流屏幕分辨率介绍
2012-01-18 14:45 1391对于Android游戏开发我们不得不像iPhone那样思 ... -
关于混淆外包jar包出错的解决方法
2012-01-18 11:30 1758-optimizationpasses 5-do ... -
新浪微博SDK
2011-12-28 16:38 1157DEMO程序:http://code.google.co ... -
Android中自定义Dialog外形,去除黑底和白色边框
2011-12-08 16:29 7425在做Android开发中经常会使用到自定义样式的Dialo ... -
汉字排序
2011-12-07 09:44 1061Comparator comparator = Coll ... -
Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性
2011-11-29 19:29 13550在Android中,TextView是我们最常用的用来显示文本 ... -
屏幕分辨率适配
2011-10-19 18:55 3088屏幕分辨率:1024x600 d ... -
演化理解 Android 异步
2011-10-07 20:34 1039在学习"Android异步加载图像小结" ...
相关推荐
android 状态栏的图标与文字提醒 NotificationManager与Notification
首先,发送一个状态栏通知必须用到两个类:NotificationManager、Notification。 NotificationManager:是状态栏通知的管理类,负责发通知、清楚通知等。 NotificationManager是一个系统Service,必须通过...
调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotifacationManager服务 通过构造器创建一个Notification对象 为Notification设置各种属性 通过NotificationManager发送Notification。
首先,发送一个状态栏通知必须用到两个类: NotificationManager 、 Notification。 NotificationManager : 是状态栏通知的管理类,负责发通知、清楚通知等。 NotificationManager 是一个系统Service,必须通过 ...
自学 Android 做的第二十七个 Android 程序,使用了 NotificationManager、Notification 和 Notification.Builder
NULL 博文链接:https://forlan.iteye.com/blog/2264722
这个例子演示Android 在状态栏... NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(0, myNotification);//发送Notification
本文主要讲解状态栏通知Notification、NotificationManager,小编觉得非常详细,给大家一个参考,希望对大家学习有所帮助。
可以设置一条通知,当然可以更新一条通知,我们通过在调用NotificationManager.notify(ID, notification)时所使用的ID来更新它。为了更新你之前发布的通知,你需要更新或者创建一个NotificationCompat.Builder对象,...
本文实例讲解了通知Notification使用方法,此知识点就是用作通知的显示,包括振动、灯光、声音等效果,分享给大家供大家参考,具体内容如下 效果图: MainActivity: import java.io.File; import android....
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, notification); or Json2Notification.from(context)....
要使用消息通知,必须要用到两个类:NotificationManager和Notification,其他NotificationManager的初始化是用getSystemService方法,并且通过notify方法来向android系统发送消息栏通知和显示。效果 :代码: 代码...
在使用手机时,当有未接来电或者新短消息时,手机会给...使用Notification和NotificationManager类发送和显示通知也比较简单,大致可以分为以下四个步骤 (1)调用getSystemService() 方法获取系统的NotificationManag
在使用手机时,当有未接来电或者是新短消息时,手机会给出...使用Notification和NotificationManager类发送和显示通知也比较简单,大致可分为以下4个步骤。 (1)调用getSystemService()方法获取系统的NotificationManag
notification类表示一个持久的通知,将提交给用户使用NotificationManager。已添加的Notification.Builder,使其更容易构建通知。
关于通知Notification相信大家都不陌生了,平时上QQ的时候有消息来了或者有收到了短信,手机顶部就会显示有新消息什么的,就类似这种。今天就稍微记录下几种Notification...3.0以前的通知和3.0以后的通知是有些区别的。
这个自动检测升级Apk的功能,相信每个应用都会使用到。...其中使用Service、BroadcastReceiver、OkHttp下载、Gson解析json、NotificationManager、Notification 运用的知识也挺多的,也可以功大家学习使用。
Android界面 NotificationManager使用Bitmap做图标,如何实现呢,本文将介绍解决方法,需要的朋友可以参考下
Notification可以理解为通知的意思一般用来显示广播信息 用Notification就必须要用到NotificationManager 想用Notification一般有三个步骤,如下所示 ① 一般获得系统级的服务NotificationManager。 调用Context....