Application Context 和 Widget Context 的区别
来源:互联网 发布:青海民族大学宿舍网络 编辑:程序博客网 时间:2024/06/05 03:23
最近在制作桌面时钟应用的过程中,发现如果在AndroidManifest.xml中注册的<receiver>中添加
<intent-filter> <action android:name="android.intent.action.TIME_TICK"/> <action android:name="android.intent.action.TIME_SET"/> <action android:name="android.intent.action.TIMEZONE_CHANGED"/></intent-filter>是无效的。即在继承自AppWidgetProvider的类中onReceive方法无法正常接收到ACTION_TIME_TICK系统消息。
查看了API之后发现有这么一段话:
You can not receive this through components declared in manifests, only by exlicitly registering for it with Context.registerReceiver().
因此删去了XML中的配置,改为在AppWidgetProvider继承类的onEnable方法中添加如下代码:
context.registerReceiver(this, new IntentFilter(Intent.ACTION_TIME_TICK));context.registerReceiver(this, new IntentFilter(Intent.ACTION_TIME_CHANGED));context.registerReceiver(this, new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED));其中的context为onEnable(Context context)的形参。
运行后报错:
android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
查阅资料,在stackoverflow上找到解决方案:【http://stackoverflow.com/questions/5112529/register-broadcastreceiver-to-widget-difference-of-context-object】
将context改为Context app_ctx = context.getApplicationContext();
然后再用 app_ctx 去注册Receiver,问题的确解决了。
But i still concern two questions:
Question❶: The difference between Widget Context and Application Context and other Context object (Activity Context)'
Question❷: Because context object was use usually so when to use Application context and when to use other Context.
For ❷ i had found explain relate with memory leak (Ref. 2), but i think it may not enough (Ref.2)
So if your have answer please let me known, any answer appreciated.
Thanks
★Referecens:
(1) http://groups.google.com/group/android-developers/browse_thread/thread/790da1a655f4a227/0b8d6aad1dc2d371?hl=en&lnk=gst&q=Broadcast+Receiver+From+Widget#0b8d6aad1dc2d371
(2) http://developer.android.com/resources/articles/avoiding-memory-leaks.html
除此以外,还有一点令我很困惑:究竟为何不能直接在XML中注册?- Application Context 和 Widget Context 的区别
- Application context和Activity context的区别
- Application context和Activity context的区别
- Application context和Activity context的区别
- Application context和Activity context的区别
- Application context和Activity context的区别
- Application context和Activity context的区别
- Application context和Activity context的区别
- Activity的context和Application的context的区别
- application的context和activity的context区别
- Application context和Activity context的区别及应用
- Spring的servlet context和application context
- Android 的Context和application
- Context和Application的理解
- Application Context 和 Activity Context
- Application中的Context和Activity中的Context区别
- Activity context 和 Application context 区别与联系
- Activity context 和 Application context 区别与联系
- c遍历文件夹
- poj 2391 floyd + 二分搜索 + 拆点建图 + dinic
- RemoteDroid源代码下载及分析
- POJ 1575 Easier Done Than Said?(我的水题之路——三重标记)
- SSAS处理时找不到属性键,由全角字符引起的。
- Application Context 和 Widget Context 的区别
- hadoop如何发音
- USACO Section 5.3 Milk Measuring - DFSID+DP...
- OJ做题常见的几个错误
- gcc汇编
- [转]Windows Sysinternals 微软官方免费的极品实用绿色小工具合集,绝对值得你收藏
- Vim实战手册(9)Vimide——打造你自己的IDE(B)
- 【转】FileTypesMan 强大的文件类型管理工具 (文件扩展名关联/右键菜单管理利器)
- 【转】WebMatrix 从零建站如此简单!来自微软的免费网站服务器快速建站套件