- 浏览: 115647 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
zhifeiji512:
George_ghc 写道第一段代码给cursor赋值第二段代 ...
Android中Cursor关闭的问题
1. BroadcastReceiver
BroadcastReceiver用来接收如电量低、信号弱等即时广播通知,发送侧调用Context.sendBroadcast(Intent)或Context.sendOrderedBroadcast(Intent)可发送这类广播通知,而BroadcastReceiver则用onReceive (Context, Intent)方法来处理到接受到的广播通知。一个BroadcastReceiver的生命周期和onReceive()一致,其宿主进程和Activity、Service的一样,因此在onReceive不能有长时间的循环处理,不能显示Dialog的UI。
一个BroadcastReceiver可以在AndroidManifest.xml中静态声明和在程序动态注册,其可处理的通知由在声明或注册时定义的IntentFilter决定。
-
动态注册的例子: 如果在Activity中动态注册,必须在onResume()中注册,在onPause()中取消注册。
private IntentReceiver mDataReceiver = new IntentReceiver() { @Override public void onReceiveIntent(Context context, Intent intent) { Log.d("hogeRecv","onReceiveIntent: "+intent); } } public void onResume() { super.onResume(); String action = "cn.com.chen.TestReceiver"; IntentFilter filter = new IntentFilter(action); this.registerReceiver(mDataReceiver,filter); } public void onPause() { super.onPause(); unregisterReceiver(mDataReceiver); }
-
静态声明同Service和Activity的声明类似。
<application> ... > <receiver name="receiver类名"> <intent-filter> <action name="cn.com.chen.TestReceiver"> </intent-filter> </receiver> </application>
在onReceive()中,不能直接同Activity交互,仅可Toast显示一个简单的通知消息、或通过NotificationManager设置一个Notification。对于Service,可以通过Context.startService()启动,但不能用Context.bindService()来绑定。如果需要同一个已启动的Service异步通信,可以通过BroadcastReceiver.peekService (Context myContext, Intent service)(返回值为IBinder)。
例:
IBinder binder = peekService(ctx, new Intent(ctx, MediaPlaybackService.class)); IMediaPlaybackService sService = IMediaPlaybackService.Stub.asInterface(binder);
Context.sendBroadcast()是一个完全异步处理,该函数调用后,其所能匹配的BroadcastReceiver则同时执行。而Context.sendOrderedBroadcast()则调用一次则能执行一个匹配的BroadcastReceiver,执行顺序只有IntentFilter的android:priority属性决定。
2. NotificationManager
Notification可以有多种形式,比如在StatusBar上的一个icon(通过该Icon可以启动一个Activity等)、LED闪烁、Screen的背景灯闪烁、震动等。用户拖动StatusBar,可以使Notification扩展显示为一个List UI。如果选择一个Nofitication,则触发该Nofitication所关联的PendingIntent所定义的行为。PendingIntent可以包含启动一个Activity、BroadcastReceiver、Service等信息。
int icon = R.drawable.notification_icon; // StatusBar上的图标 CharSequence tickerText = "Hello"; // 启动时,在StatusBar上显示的走马灯 long when = System.currentTimeMillis(); // Notification启动时间 Context CharSequence contentTitle = "My notification"; // (扩展显示时)Notification的标题 CharSequence contentText = "Hello World!"; // (扩展显示时)Notification的消息 Notification notification = new Notification(icon, tickerText, when); Intent notificationIntent = new Intent(this, MyActivity.class); // 后续的PendingIntent用 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Context context = getApplicationContext(); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
此外,还可以通过设置Notification的属性来设置提示音、震动等。
notification.defaults |= Notification.DEFAULT_SOUND; notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); notification.defaults |= Notification.DEFAULT_VIBRATE; long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate; notification.defaults |= Notification.DEFAULT_LIGHTS; notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300; notification.ledOffMS = 1000; notification.flags |= Notification.FLAG_SHOW_LIGHTS; NotificationManager用来管理Notification,用Context.getSystemService(String)可从系统中获获取。NotificationManager的方法notify(int id, Notification)可以显示一个Notification到StatusBar上,方法cancel(int id)可删除一个Notification。参数ID用来标识Notification,在一个程序中必须唯一。 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); Notification notification = ... // Notification设置 mNotificationManager.notify(1, notification);
发表评论
-
repo用法
2012-03-16 14:24 1201repo用法(转载) http://blogold ... -
Git使用指南
2012-03-15 14:43 8471.创建目录 $ git config --glo ... -
关于Android的多种屏幕支持
2011-12-14 14:23 1216首先是几个基本概 ... -
android ListView 修改背景
2011-12-14 09:39 1821修改listview时存在的 ... -
详解android:scaleType属性
2011-11-28 17:09 6352android:scaleType是控制图片如何res ... -
Android使用AIDL设计和调用远程接口
2011-11-24 18:14 3980在Android中, 每个应 ... -
Android开发 系统应用调用,Intent使用总结
2011-11-09 09:48 1298转载来源网上资料。 显示网页: 1. Ur ... -
ListView结合ContextMenu 获取点击项的Position
2011-11-07 17:35 1423ContextMenu称为上下文菜单,一般在控件上长按时 ... -
List of Android MIME types and Uri's
2011-11-07 16:35 1567This is a list of URIs that can ... -
隐藏输入法
2011-11-07 14:16 795在manifest里设置,android:windowSoft ... -
SlidingDrawer 使用
2011-10-28 17:51 3960关于SlidingDrawer控件 1.重要属性 ... -
Notification
2011-10-21 16:49 1541可以给Notification设置缺省的表现形式: ... -
Android获取其他包的Context实例
2011-10-20 11:17 829Android中有Context ... -
Android 中Message,MessageQueue,Looper,Handler详解+实例
2011-10-18 13:23 876一、几个关键概念 ... -
Activity 的生命周期 以及 横屏竖屏切换时 Activity 的状态变化
2011-10-18 13:21 1038转自:http://www.blogjava.ne ... -
免费的Android UI库及组件推荐
2011-10-18 13:10 2961转自:http://www.iteye.com/news/22 ... -
众多Android 开源项目推荐,给力工作给力学习
2011-10-18 11:54 619转自:http://www.cnblogs.com/Terry ... -
下拉刷新
2011-10-18 11:10 749下拉刷新 参考代码 https://gith ... -
canvas.translate(x,y)理解
2011-10-17 15:59 2641translate(float x,float y)函数是将整 ... -
android.view.WindowManager$BadTokenException: Unable to add window
2011-09-28 10:19 1755android.view.WindowManager$B ...
相关推荐
Android系统中的广播Broadcast,Receiver例子,可以用LogCat察看整个运行的生命周期.MainActivity界面上有两个按钮,分别是Start Counter和Stop Counter按钮,点击前者开始计数,而点击后者则停止计数。
3 9 2 广播接收器:Broadcast Receiver 3 9 3 服务 Service 3 9 4 内容提供者 Content Provider 第4章 对话框 信息提示和菜单 4 1 对话框 4 2 信息提示 4 2 1 Toast信息框 4 2 2 通知 Notification 4 3 菜单 第5...
主要利用了notification service 和receiver 实现了用户通知栏更新,失败了点击重新下载,下载好了点击自动安装,布局很丑,请根据自己项目的实际需求自行修改
Android基础阶段:平台架构特性(JAVA/C) Market/应用程序组件 环境搭建与部署/打包与发布 AVD/DDMS/AAPT 调试与测试 相关资源访问/资源制作 Activity/Service/Broadcast Receiver/Content Provider/原理(生命周期)及...
◆ 解释了如何创建后台服务、通知和Cloud to Device Messaging ◆ 演示了如何创建动态的、交互式的主屏幕微件和Live Wallpaper ◆ 探索了硬件和通信API,包括蓝牙、电话、Wi-Fi Direct、 NFC和Android ...
----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...
7.3.1 Intent Receiver 7.3.2 Intent Filter 7.4 设置Activity许可 7.5 应用实例详解:电话拨号程序 7.5.1 实例分析 7.5.2 实例实现 第8章 Android中的后台服务Service 8.1 Service的作用 8.2 Service的实现 8.2.1 ...
主要内容包括Android的四大应用程序组件(Activity、Service、Content Provider和Broadcast Receiver)、布局、菜单、控件、资源和本地化、可视化UI设计、对话框、Toast和通知栏(Notification)、Fragment、ActionBar、...
下面就具体介绍一下Broadcast Receiver组件的用法。 下面是Android Doc中关于BroadcastReceiver的概述: ①广播接收器是一个专注于接收广播通知信息,并做出对应处理的组件。很多广播是源自于系统代码的──比如,...
2.8.2 使用broadcast receiver监听广播 2.8.3 android本地广播 2.9 小结 第3章 设备界面演练 3.1 文字标签处理 3.2 改变屏幕窗口元素的背景颜色 3.3 改变textview的文字颜色 3.4 实现textview文字的替换 3.5 获取...
Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...
Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...
主要内容包括Android的四大应用程序组件(Activity、Service、Content Provider和Broadcast Receiver)、布局、菜单、控件、资源和本地化、可视化UI设计、对话框、Toast和通知栏(Notification)、Fragment、ActionBar、...
5.1.1 Remote Views在通知栏上的应用 / 219 5.1.2 Remote Views在桌面小部件上的应用 / 221 5.1.3 Pending Intent概述 / 228 5.2 Remote Views的内部机制 / 230 5.3 Remote Views的意义 / 239 第6章 ...
2.1.2 工程目录结构及自动生成内容 20 2.1.3 android 包和manifest 清单文件 22 2.1.4 重命名应用程序中的部分文件 23 2.2 activity 的生命周期 23 2.2.1 秘诀2:使用其他的生命周期方法 24 2.2.2 秘诀3:强制...
r函数通知 InputDispatcher ,有 键盘事件发生了 1. 调用 validateKeyEvent 函数来验 证action参数是否正确 2. 参数action检查通过后,还通过 policyFlags 参数来检查一下同时是 否有ALT和SHIFT键被按下 3. 调用 ...
Google Android SDK开发范例大全(完整版)共4个分卷 目录 第1章 了解.深入.动手做. 1.1 红透半边天的Android 1.2 本书目的及涵盖范例范围 1.3 如何阅读本书 1.4 使用本书范例 1.5 参考网站 第2章 Android初体验 2.1...
当音频或视频媒体正在播放并且播放器的窗口没有聚焦时(例如:收听背景音频,或在观看视频时通过按“主页”按钮),会添加另一个通知来控制播放器的重新播放或重新调整播放器的焦点窗户。 尽管存在其他来从野外...
2.2 建立第一个Android项目(HelloAndroid!) 2.3 Android应用程序架构——从此开始 2.4 可视化的界面开发工具 2.5 部署应用程序到Android手机 第3章 用户人机界面 3.1 更改与显示文字标签——TextView标签的使用 ...