A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© android_vae 中级黑马   /  2016-6-1 13:34  /  406 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

广播接收器BroadcastReceiver
Activity.this.sendBroadcast(intent)发送广播)
1.实现BroadcastReceiver
2.复写onReceive()方法
3.1Androidmanifest.xml文件中注册BroadcastRecevicer
<receiver android:name = "BroadcastReceiver实现类名">
         //决定接收何种广播
         <intent-filter>
                   <action android:name ="android.intent.action.EDIT "/>
         </intent-filter>
</receiver>
此种方法程序关闭依然可以接收广播
3.2在程序代码中注册BroadcastRecevicer
3.2.1生成BrodcastReceiver对象
3.2.2 生成IntentFilter对象
IntentFilter  filter  = new  IntentFilter( );
3.2.3IntentFilter添加一个Action
filter.addAction();
3.2.4注册广播接收器
         Activity.this. registerReceiver(receiver,filter);receiverBroadcastRecevicer对象;filterIntentfilter对象)
取消注册:unregisterReceiver(receiver);
         (如果一个BrodcastReceiver用于更新UI,那么通常会使用这种方法进行注册,在Activity启动时注册,在Activity不可见后取消注册)
         BroadcastRecevicer生命周期:到onReceiver()结束。
Android内置的BrodcastActions
1.ACTION_CAMERA_BUTTON                  相机按钮按下事件
2. ACTION_BATTERY_LOW               低电量事件
3.ACTION_DATE_CHANGED           日期改变
4.ACTION_POWER_CONNECTED   接通电源
5. ACTION_REBOOT                          重启系统
6.ACTION_SCREEN_ON                     屏幕打开
······

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马