android安全:flag FLAG_RECEIVER_REGISTERED_ONLY的意义
来源:互联网 发布:python nonzero 编辑:程序博客网 时间:2024/05/10 06:28
尊重原创作者,转载请注明出处:
http://blog.csdn.net/gemmem/article/details/8859493
做过时钟widget的朋友应该对ACTION_TIME_TICK都很熟悉,大家都用这个广播来更新UI界面上的时间,但是新版本的android中,却对TIME_TICK广播做了限制,系统不再允许在AndroidManifest.xml里面静态注册receiver来接受TIME_TICK广播了,程序员们被迫采用动态注册的方式来监听TIME_TICK广播。不知道大家碰到这个情况时是什么感想?
也许你当时在埋怨google害得你改代码,而且动态注册容易失效,因为进程可能会因为内存不足而被kill掉。
这不是给开发者添乱吗?
google为什么突然不允许采用xml方式注册TIME_TICK了呢?
TIME_TICK广播是alarmManagerService发送的,我们还是先看看源码:
AlarmManagerService.java
public AlarmManagerService(Context context) { mContext = context; mDescriptor = init(); // We have to set current TimeZone info to kernel // because kernel doesn't keep this after reboot String tz = SystemProperties.get(TIMEZONE_PROPERTY); if (tz != null) { setTimeZone(tz); } PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); mTimeTickSender = PendingIntent.getBroadcast(context, 0, new Intent(Intent.ACTION_TIME_TICK).addFlags( Intent.FLAG_RECEIVER_REGISTERED_ONLY), 0); Intent intent = new Intent(Intent.ACTION_DATE_CHANGED);
关键代码如下:
mTimeTickSender = PendingIntent.getBroadcast(context, 0, new Intent(Intent.ACTION_TIME_TICK).addFlags( Intent.FLAG_RECEIVER_REGISTERED_ONLY), 0);
原来google在intent中加入了FLAG_RECEIVER_REGISTERED_ONLY这个flag,这就是为什么xml注册会报错的原因。
我们再来看看广播ACTION_SCREEN_ON/OFF
void com.android.server.PowerManagerService.initInThread()
void initInThread() { …… mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON); mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF); mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); …… }
再来看一个广播,ACTION_BATTERY_CHANGED
电池电量发生变化的时候,系统发送此广播
void com.android.server.BatteryService.sendIntent()
private final void sendIntent() { // Pack up the values and broadcast them to everyone Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_REPLACE_PENDING); …… }
看到没有?它们都加了flag FLAG_RECEIVER_REGISTERED_ONLY。
也就是说,这些系统广播都只能动态注册。
下面说说google这样做的目的:
很多病毒程序为了保证自己被终止后能够再次运行,都会在xml中注册一些系统广播,企图利用这些系统高频广播来实现自启动。
比如在老版本的android系统中,病毒程序可以通过监听TIME_TICK来启动自己的service后台运行,做一些隐秘的工作,而且就算自己被kill掉了,也能很快重新启动。
而一旦这些系统广播加了flag FLAG_RECEIVER_REGISTERED_ONLY,这些病毒程序就没辙了。
google的这一改动无疑提升了android系统的安全性。
- android安全:flag FLAG_RECEIVER_REGISTERED_ONLY的意义
- Android的Intent Flag
- Android的跳转flag
- Activity中的intent的FLAG代表的意义
- Android Intent Flag的介绍
- Android Intent Flag的介绍
- Android Intent Flag的介绍
- Android Intent Flag的介绍
- Android Intent Flag的介绍
- Android Intent Flag的介绍
- Android PendingIntent的Flag设置
- Android Intent Flag的介绍
- Android Intent Flag的介绍
- Android Intent Flag的介绍
- Android Intent常见的FLAG
- Android Intent Flag的介绍
- Android 新建activity的intent 的flag
- 视图机制对于数据库的安全意义
- win7修改eclipse3.7 默认字体的方法!
- MFC类成员函数作为线程函数的解决办法
- yum帮助信息
- NSThread 和 NSTimer
- ubuntu12增加环境变量
- android安全:flag FLAG_RECEIVER_REGISTERED_ONLY的意义
- haoop 错误记录
- Unity3D教程:实现基于Socket通讯的公共聊天室
- 随机 取题
- Non-negative Matrix Factorization and Probabilistic Latent Semantic Analysis
- oracle分配权限
- 由setlocale解决中文显示引发的探索
- VS2005 :Fatal Error C1902:程序数据库管理器不匹配,解决办法
- 如何使用Axure RP Widget Library?