前些天刚刚做一个项目的预演,要用到广播通知,一下是一些小的学习的心得:
显示声明一个类继承BroadcastReceiver,实现onReceive这个方法:
这你需要的地方启用广播:
Intent intent = new Intent();
intent.setAction('action1');
sendBroadcast(intent);
当然你可以用intent传递一些值在广播中获取这些值,之快就自己发挥了。
广播的启动还要注册,一般两种注册方式,这里是在AndroidManifest.xml中进行注册:
<receiver android:name=".broadcast.ReceiveBroadCast">
<intent-filter>
<action android:name="action1">
</action>
</intent-filter>
</receiver>
注册的时候可以注册不同的action,用于启用广播时完成不同的操作,最后不要忘了配置权限:
<uses-permission android:name="android.permission.INTERNET" />
广播启动后要显示通知:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// 申明notification的对象
Notification notification = new Notification(R.drawable.icon, msg, System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
// 构造intent对象
Intent intent = null;
intent = new Intent();
//这里可以点击通知跳转到你要的activity中
//cotext.startActivity();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
// 使用pendingIntent对Intent进行包装
PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 通知显示时,会伴随音乐
// notification.defaults = Notification.DEFAULT_SOUND;
// 发送消息
notification.setLatestEventInfo(context, msg, mi.getMsg(), pendingIntent);
// 通知该消息可以发布了
notificationManager.notify(id, notification);
显示声明一个类继承BroadcastReceiver,实现onReceive这个方法:
这你需要的地方启用广播:
Intent intent = new Intent();
intent.setAction('action1');
sendBroadcast(intent);
当然你可以用intent传递一些值在广播中获取这些值,之快就自己发挥了。
广播的启动还要注册,一般两种注册方式,这里是在AndroidManifest.xml中进行注册:
<receiver android:name=".broadcast.ReceiveBroadCast">
<intent-filter>
<action android:name="action1">
</action>
</intent-filter>
</receiver>
注册的时候可以注册不同的action,用于启用广播时完成不同的操作,最后不要忘了配置权限:
<uses-permission android:name="android.permission.INTERNET" />
广播启动后要显示通知:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// 申明notification的对象
Notification notification = new Notification(R.drawable.icon, msg, System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
// 构造intent对象
Intent intent = null;
intent = new Intent();
//这里可以点击通知跳转到你要的activity中
//cotext.startActivity();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
// 使用pendingIntent对Intent进行包装
PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 通知显示时,会伴随音乐
// notification.defaults = Notification.DEFAULT_SOUND;
// 发送消息
notification.setLatestEventInfo(context, msg, mi.getMsg(), pendingIntent);
// 通知该消息可以发布了
notificationManager.notify(id, notification);
发表评论
-
android aidl 主进程子进程间数据相互传递
2012-12-18 11:49 3908android中进程间的通信对与大量的接口的调用的时候,一般是 ... -
android 实现在titlebar上显示进度条
2012-11-13 18:48 832先是在setContextView之前设置界面的样式 requ ... -
android 实现下拉刷新的功能
2012-11-13 15:41 4251在android中有时我们要实现下拉刷新的功能,我在前日人的基 ... -
android aidl进程间方法的调用
2012-10-10 14:21 1604android 进程的通信很多,可以用intent传递数据,可 ... -
listView 点击一个Item效果
2012-01-13 17:20 6本人刚做一个项目需求是这样的:展示通话记录的列表,点击每一条记 ... -
android aidl
2012-01-11 11:12 1023android中的进程间的通信很多,下面粗略的讲解一下如何实现 ... -
android 权限大全
2011-12-22 20:08 851android.permission.ACCESS_CHECK ... -
android Action大全
2011-12-21 20:26 1111String ADD_SHORTCUT_ACTION 动作 ... -
android 自定义桌面
2011-12-03 16:23 13681、 把背景图片push到SDCard中 adb pu ... -
android 游戏开发加载界面获取资源文件中图片ID的集合
2011-12-03 13:18 4940主要给大家介绍如何加载界面的图片。正如前面Abs ... -
android 判断网络是否连接可用
2011-12-03 13:06 1664// 判断网络是否正常 public static boole ... -
火星通讯录
2011-11-25 20:27 1381一、用.rar打开apk文档,得到文档结构图如下所示 ... -
android 四种加载模式2
2011-11-16 11:13 963singleInstance模式解决了这个问题(绕了这么半天才 ... -
activity 的四种加载的模式1
2011-11-16 11:11 991在android应用的开发中有时会activity之间的重复的 ... -
android 面试题
2011-11-09 20:17 981android 面试题 1.android中进程和进程间的通信 ... -
android ksoap调用天气预报
2011-11-08 19:27 5098下面例子改自网上例子:http://express.ruank ... -
android NDK
2011-11-03 21:34 2015Cygwin+Android NDK的安装 时 间 版 本 ... -
android 判断sdcard是否存在,以及写入权限
2011-10-16 16:57 5735做android 开发的时候经常涉及到权限的问题,而我们像sd ... -
android bitmap
2011-10-14 11:02 819对于android终端应用软件开发的人员来说图片显示时,如果图 ... -
android popupwindow,调用本地的图库,照相
2011-10-14 10:51 5383我们写项目的时候又是要自定义弹出美观的悬浮操作窗口,这里我们可 ...
相关推荐
android notification完全解析Demo
主要利用了notification service 和receiver 实现了用户通知栏更新,失败了点击重新下载,下载好了点击自动安装,布局很丑,请根据自己项目的实际需求自行修改
Notification显示音乐下载的界面
类型:NotificationDemo 功能:从网上下载apk存储到手机指定目录,并在通知栏里显示其下载进度,如果下载错误,或者不想要该apk可以删除 涉及知识: 文件流、网络下载链接协议、读写权限、Handler、Notification、...
Android Notification 常见样式代码及发送demo,其中包含源码及一个app-release.apk,可直接发送固定样式,也可自定义样式。可以作为通知测试的小工具,也可作为参考demo。
这个项目整合了Android平台所有Notification的使用方法,有些使用场景不生效,不知道是否是我使用方法有问题。
Android Notification消息框 Toast弹出框用法演示范例,本例中关于 Toast弹出框的演示,演示了适时的 Toast和长时间的 Toast,关于Notification的定义,则演示了高级Notification的用法,自定义4种Notification的...
android Notification
android notification 软件更新
android notification Service 文件下载
android 利用notification 通知栏 实现动态下载.
Android notification进度条 demo,欢迎下载!!!!!!!!!!!!!!!!!!!!!!!!!!!!
android notification 通知 源码 例子
Android NOtification 使用Android NOtification 使用
Convert JSON to Android Notification. This is very useful for push notification (GCM). Usage Notification notification = Json2Notification.from(context).with(json).notification(); NotificationManager ...
简单介绍Android Notification用法 详细介绍NotificationManageService和StatusBarManageService与App层交互过程
了解Android Notification的基本用法
Android Notification通知 简单使用 demo下载。 解决问题:兼容高低版本,帮助用户打开通知开关,android5.x手机状态栏icon是白块问题。 详细文章请参考 ...
各种各样的android Notification效果
kinds of notification