- 浏览: 228951 次
- 性别:
- 来自: 广州
最新评论
-
Janne:
你好 有源代码?可以发到我的邮箱里学学吗?2731049993 ...
achartengine画出动态折线图的效果 -
anbo724:
我的邮箱 anbo724@gmail.com谢谢@
achartengine画出动态折线图的效果 -
anbo724:
你好 请问有源码没《?谢谢
achartengine画出动态折线图的效果 -
weiday123:
额,觉得这个会不会占堆内存?
AdapterView、Adapter优化 -
wen742538485:
为什么没有呢?权限没加还是发创建了给你删了再想创建?是不允许重 ...
Android中为你的应用程序添加桌面快捷方式
http://www.cnblogs.com/newcj/archive/2011/03/14/1983782.html
最近一直在研究 android ,并一边研究一边做应用。其中遇到了把程序通知常驻在 Notification 栏,并且不能被 clear 掉(就像android QQ一样)的问题。经过研究实现了其功能,现把 Notification 的使用总结如下:
Notification 的使用需要导入 3 个类
import android.app.PendingIntent;
import android.app.NotificationManager;
import android.app.Notification;
代码示例及说明
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());
n.flags = Notification.FLAG_AUTO_CANCEL;
Intent i = new Intent(arg0.getContext(), NotificationShow.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
n.setLatestEventInfo(
arg0.getContext(),
"Hello,there!",
"Hello,there,I'm john.",
contentIntent);
nm.notify(R.string.app_name, n);
下面依次对每一段代码进行分析:
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
创建 NotificationManager,其中创建的 nm 对象负责“发出”与“取消” Notification。
Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());
n.flags = Notification.FLAG_ONGOING_EVENT;
创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。其中创建的 n 对象用来描述出现在系统通知栏的信息,之后我们将会看到会在 n 对象上设置点击此条通知发出的Intent。
n.flags = Notification.FLAG_AUTO_CANCEL;
设置 n.flags 为 Notification.FLAG_AUTO_CANCEL ,该标志表示当用户点击 Clear 之后,能够清除该通知。
Intent i = new Intent(arg0.getContext(), NotificationShow.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent
请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例
Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity。更多请参见 “ (转载)Android下Affinities和Task ”
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位。
其中 PendingIntent.FLAG_UPDATE_CURRENT 表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据。
这里再简要说一下 Intent 与 PendingIntent 的区别:
Intent :意图,即告诉系统我要干什么,然后系统根据这个Intent做对应的事。如startActivity相当于发送消息,而Intent是消息的内容。
PendingIntent :包装Intent,Intent 是我们直接使用 startActivity , startService 或 sendBroadcast 启动某项工作的意图。而某些时候,我们并不能直接调用startActivity , startServide 或 sendBroadcast ,而是当程序或系统达到某一条件才发送Intent。如这里的Notification,当用户点击Notification之后,由系统发出一条Activity 的 Intent 。因此如果我们不用某种方法来告诉系统的话,系统是不知道是使用 startActivity ,startService 还是 sendBroadcast 来启动Intent 的(当然还有其他的“描述”),因此这里便需要PendingIntent。
n.setLatestEventInfo(
arg0.getContext(),
"Hello,there!",
"Hello,there,I'm john.",
contentIntent);
设置显示在通知下拉框中的信息,参数依次为:Context,标题,内容,PendingIntent。
nm.notify(R.string.app_name, n);
启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification。
如何使自己的Notification像Android QQ一样能出现在 “正在运行的”栏目下面
其实很简单,只需设置Notification.flags = Notification.FLAG_ONGOING_EVENT;便可以了。
如何改变 Notification 在“正在运行的”栏目下面的布局
创建 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 便可以了,如:
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews rv = new RemoteViews(Main.this.getPackageName(), R.layout.notification_view);
rv.setImageViewResource(R.id.image, R.drawable.chat);
rv.setTextViewText(R.id.text, "Hello,there,I'm john.");
n.contentView = rv;
n.contentIntent = contentIntent;
nm.notify(R.string.app_name, n);
注意,如果使用了contentView,那么便不要使用Notification.setLatestEventInfo。如果setLatestEventInfo在赋给 Notification.contentView 的代码之后,那么contentView的效果将被覆盖,显示的便是 setLatestEventInfo 的效果;如果 setLatestEventInfo 在 Notification.contentView 的代码之前,那么显示的便是 Notification.contentView 的效果,也就是说不管你想要setLatestEventInfo 或 contentView 的自定义效果,请保证始终只有一句设置代码,因为在最后一句绑定的时候,之前的设置contentView或setLatestEventInfo的代码都是完全没有必要的。
Technorati 标签: Android,Notification
最近一直在研究 android ,并一边研究一边做应用。其中遇到了把程序通知常驻在 Notification 栏,并且不能被 clear 掉(就像android QQ一样)的问题。经过研究实现了其功能,现把 Notification 的使用总结如下:
Notification 的使用需要导入 3 个类
import android.app.PendingIntent;
import android.app.NotificationManager;
import android.app.Notification;
代码示例及说明
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());
n.flags = Notification.FLAG_AUTO_CANCEL;
Intent i = new Intent(arg0.getContext(), NotificationShow.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
n.setLatestEventInfo(
arg0.getContext(),
"Hello,there!",
"Hello,there,I'm john.",
contentIntent);
nm.notify(R.string.app_name, n);
下面依次对每一段代码进行分析:
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
创建 NotificationManager,其中创建的 nm 对象负责“发出”与“取消” Notification。
Notification n = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());
n.flags = Notification.FLAG_ONGOING_EVENT;
创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。其中创建的 n 对象用来描述出现在系统通知栏的信息,之后我们将会看到会在 n 对象上设置点击此条通知发出的Intent。
n.flags = Notification.FLAG_AUTO_CANCEL;
设置 n.flags 为 Notification.FLAG_AUTO_CANCEL ,该标志表示当用户点击 Clear 之后,能够清除该通知。
Intent i = new Intent(arg0.getContext(), NotificationShow.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent
请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例
Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity。更多请参见 “ (转载)Android下Affinities和Task ”
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位。
其中 PendingIntent.FLAG_UPDATE_CURRENT 表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据。
这里再简要说一下 Intent 与 PendingIntent 的区别:
Intent :意图,即告诉系统我要干什么,然后系统根据这个Intent做对应的事。如startActivity相当于发送消息,而Intent是消息的内容。
PendingIntent :包装Intent,Intent 是我们直接使用 startActivity , startService 或 sendBroadcast 启动某项工作的意图。而某些时候,我们并不能直接调用startActivity , startServide 或 sendBroadcast ,而是当程序或系统达到某一条件才发送Intent。如这里的Notification,当用户点击Notification之后,由系统发出一条Activity 的 Intent 。因此如果我们不用某种方法来告诉系统的话,系统是不知道是使用 startActivity ,startService 还是 sendBroadcast 来启动Intent 的(当然还有其他的“描述”),因此这里便需要PendingIntent。
n.setLatestEventInfo(
arg0.getContext(),
"Hello,there!",
"Hello,there,I'm john.",
contentIntent);
设置显示在通知下拉框中的信息,参数依次为:Context,标题,内容,PendingIntent。
nm.notify(R.string.app_name, n);
启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification。
如何使自己的Notification像Android QQ一样能出现在 “正在运行的”栏目下面
其实很简单,只需设置Notification.flags = Notification.FLAG_ONGOING_EVENT;便可以了。
如何改变 Notification 在“正在运行的”栏目下面的布局
创建 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 便可以了,如:
PendingIntent contentIntent = PendingIntent.getActivity(
arg0.getContext(),
R.string.app_name,
i,
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews rv = new RemoteViews(Main.this.getPackageName(), R.layout.notification_view);
rv.setImageViewResource(R.id.image, R.drawable.chat);
rv.setTextViewText(R.id.text, "Hello,there,I'm john.");
n.contentView = rv;
n.contentIntent = contentIntent;
nm.notify(R.string.app_name, n);
注意,如果使用了contentView,那么便不要使用Notification.setLatestEventInfo。如果setLatestEventInfo在赋给 Notification.contentView 的代码之后,那么contentView的效果将被覆盖,显示的便是 setLatestEventInfo 的效果;如果 setLatestEventInfo 在 Notification.contentView 的代码之前,那么显示的便是 Notification.contentView 的效果,也就是说不管你想要setLatestEventInfo 或 contentView 的自定义效果,请保证始终只有一句设置代码,因为在最后一句绑定的时候,之前的设置contentView或setLatestEventInfo的代码都是完全没有必要的。
Technorati 标签: Android,Notification
发表评论
-
Android Tween动画之RotateAnimation实现图片不停旋转
2012-11-26 22:38 1040本文主要介绍Android中如何使用rotate实现图片不停旋 ... -
Android实现widget定时更新
2012-11-04 20:20 883在开发Android的widget时,第一个需要解决的问题就是 ... -
来自腾讯、谷歌、百度等名企的精选面试五十题
2012-10-07 23:08 904http://www.apkway.com/thread-90 ... -
Android 中Parcelable的作用
2012-09-24 09:53 839android提供了一种新的类型:Parcel。本类被用作封装 ... -
[Android算法] 【eoeAndroid索引】史上最牛最全android开发知识汇总
2012-09-13 09:33 1095http://www.eoeandroid.com/threa ... -
安卓航班推荐70个具有商业实战性的精品Android源码
2012-08-01 00:00 915http://www.apkway.com/thread-58 ... -
Android测试教程汇总
2012-08-02 14:51 1063http://www.apkway.com/thread-67 ... -
Service 与 Thread 的区别
2012-07-26 00:10 880Service 与 Thread 的区别 很多时候,你可能 ... -
android 使用百度地图画轨迹
2012-07-26 00:08 2585import android.content.Context ... -
android百度地图半径画圆
2012-07-26 00:07 2707Java代码 import android.content ... -
Android下获取开机时间
2012-07-26 00:05 1300我的思路是:程序里注册个广播接收器,接收开机启动的广播,当程序 ... -
android 高仿【优酷】圆盘旋转菜单 的实现
2012-07-26 00:03 1336MyAnimation.java Java代码 pack ... -
android 3D 转盘效果(附源码)
2012-07-25 23:41 1781一个仿3D的转盘效果,有倒影特效,旋转图标还可自动放大缩小。由 ... -
Android Thread
2012-07-23 10:47 1014创建新线程的常用方式: 1. 直接使用Thread创建 ... -
Android 通过手说tts中文语音包实现中文朗读
2012-07-22 17:09 1776Android 通过手说tts中文语音包实现中文朗读 ... -
Android 使用HTTPClient调用Web请求(查询手机号码区域)
2012-07-21 00:33 1201Android通过Apache HttpClient调用网上提 ... -
Android+struts2+JSON方式的手机开发
2012-07-21 00:14 1139http://topmanopensource.iteye.c ... -
android九宫格实现
2012-07-21 00:03 940android九宫格实现,开始以为很复杂,其实只要知道了如何布 ... -
Android ListView圆角实现
2012-07-20 23:59 1181在android上开发项目,如 ... -
Android 将一个Activity转化为View显示出来
2012-07-19 10:27 2067最近看到好多opengl牛人写了些立方体,卷页之类的华丽的代码 ...
相关推荐
Android NOtification 使用Android NOtification 使用
使用android做的小例子 可以在通知栏显示消息和小图标 点击进去可以进入一个页面 主要用于notification的学习 基于android2.3开发
通知的使用大全,自定义等多方面的通知样式
主要介绍了Android Notification 使用方法详解的相关资料,这里提供实例来帮助大家理解掌握这部分内容,需要的朋友可以参考下
Android Notification使用详解 Notification 核心代码(链式调用):适用于Android 4.0以上(不兼容低版本) Notification noti = new Notification.Builder(this) .setContentTitle(标题名称) .setContentText...
android notification完全解析Demo
主要介绍了Android Notification使用方法总结的相关资料,这里提供了四种使用方法,需要的朋友可以参考下
主要利用了notification service 和receiver 实现了用户通知栏更新,失败了点击重新下载,下载好了点击自动安装,布局很丑,请根据自己项目的实际需求自行修改
android 利用notification 通知栏 实现动态下载.
这个项目整合了Android平台所有Notification的使用方法,有些使用场景不生效,不知道是否是我使用方法有问题。
Notification显示音乐下载的界面
android notification 通知 源码 例子
使用android push notification service 实现即时通知
简单介绍Android Notification用法 详细介绍NotificationManageService和StatusBarManageService与App层交互过程
Android 5.X Notification的使用
类型:NotificationDemo 功能:从网上下载apk存储到手机指定目录,并在通知栏里显示其下载进度,如果下载错误,或者不想要该apk可以删除 涉及知识: 文件流、网络下载链接协议、读写权限、Handler、Notification、...
android notification Service 文件下载
android notification 软件更新