Android广播的发送和接受
来源:互联网 发布:怎么用java做简历 编辑:程序博客网 时间:2024/05/06 02:50
1.广播接收者
广播接收者简单地说就是接收广播意图的Java类,此Java类继承BroadcastReceiver类,重写:
public void onReceive(Context context,Intent intent),其中intent可以获得传递的数据;
广播意图就是通过Context.sendBroadcast(Intent intent)或Context.sendOrderedBroadcast(Intent intent)发送的意图,通过这个语句,能够广播给所有满足条件的组件,比如intent设置了action="com.xiazdong",则所有在AndroidManifest.xml中设置过<action android:name="com.xiazdong"/>的广播接收者都能够接收到广播;
注:onReceive方法必须在10秒内完成,如果没有完成,则抛“Application No Response”当广播接收者onReceive方法需要执行很长时间时,最好将此耗时工作通过Intent发送给Service,由Service完成,并且不能使用子线程解决,因为BroadcastReceiver是接收到广播后才创建的,并且生命周期很短,因此子线程可能在没有执行完就已经被杀死了
代码:
public class MyReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context arg0, Intent arg1) {// TODO Auto-generated method stubSystem.out.println("MyReceiver:接受广播");int code = getResultCode();String data = getResultData();Bundle bundle = getResultExtras(true);System.out.println(code+", "+data+", "+bundle.getString("name")+", "+bundle.getInt("age"));}}需要在AndroidManifest.xml注册该Receive
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name="com.swust.myreceiver.MyReceiver"> <intent-filter > <action android:name="com.swust.broadcast.TEST"/> </intent-filter> </receiver> </application>
如果需要定义一个临时接受者,可以使用代码来描述:
1.先定义一个Java类继承BroadcastReceiver类
<span style="font-family:SimSun;font-size:12px;">public class CodeReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {System.out.println("CodeReceiver 收到广播!!!");}}</span>2.在主程序中注册该广播接受者类
public class MainActivity extends Activity {private CodeReceiver receiver;private IntentFilter filter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);filter = new IntentFilter("<span style="font-family:SimSun;">com.swust.broadcast.TEST</span>");filter.setPriority(100);receiver = new CodeReceiver();registerReceiver(receiver, filter);// Activity创建时注册接收者}@Overrideprotected void onDestroy() {super.onDestroy();unregisterReceiver(receiver);// Activity退出时注销接收者}}这样,程序启动可以接受广播,程序退出就不会再接受广播了。
2.广播发送者
通常广播发送方就是调用Context.sendBroadcast()的程序,而广播接收者就是继承BroadcastReceiver的程序;
通常广播发送方都是通过隐式意图,这样才能发送给多人;
广播发送方分为普通广播和有序广播;
同步广播:发送方发出后,几乎同时到达多个广播接收者处,某个接收者不能接收到广播后进行一番处理后传给下一个接收者,并且无法终止广播继续传播;Context.sendBroadcast(intent);
有序广播:广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,在AndroidManifest.xml的<intent-filter android:priority="xxx">设置;比如存在3个广播接收者A、B、C,优先级A>B>C,因此A最先收到广播,当A收到广播后,可以向广播中添加一些数据给下一个接收者(intent.putExtra()),或者终止广播(abortBroadcast());Context.sendOrderedBroadcast(intent);
代码:
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void send(View v){Intent intent = new Intent("com.swust.broadcast.TEST");intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);switch(v.getId()){case R.id.nomalbt:sendBroadcast(intent);break;case R.id.orderbt:Bundle bundle = new Bundle();bundle.putString("name", "张三");bundle.putInt("age", 21);sendOrderedBroadcast(intent, null, null, null, 1, "MainActivity", bundle);break;}}}
广播发送者要求接受者必须具有一定的权限才可以接受,接受者也可以要求发送者具有一定权限才会接受广播。
3.短信拦截
定义接受者类
public class SmsReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {Object[] pdus = (Object[]) intent.getExtras().get("pdus");// 获取短信数据for (Object pdu : pdus) {// 遍历SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdu);// 把数据封装成SmsMessage对象String number = msg.getOriginatingAddress();// 获取号码String content = msg.getMessageBody();// 获取内容Date date = new Date(msg.getTimestampMillis());// 获取时间System.out.println(date + ": " + number + ": " + content);if ("12306".equals(number))abortBroadcast();}}}注册及添加权限
<uses-permission android:name="android.permission.RECEIVE_SMS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.swust.blacklist.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.swust.blacklist.SmsReceiver"> <intent-filter > <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver> </application>
- Android广播的发送和接受
- android中BroadCastReceiver使用(广播的接受和发送)
- android 随手记 广播的发送和接受
- android 随手记 广播的发送和接受
- Android中 广播发送 和 接受 的简单示例
- 接受和发送广播
- 接受和发送广播
- (八)React Native实现调用android原生java方法并实现广播的发送和接受
- 广播的发送和接受过程是怎样的
- Android发送权限受限的广播,指定接受方!
- android 跨应用程序广播发送接受
- 跨应用发送和接受广播
- Android Broadcast 的接受和发送
- Android中短信的接受广播和短信的解析
- Android 广播的传递和接受 动态注册 基本用法
- Android 广播的传递和接受 动态注册 基本用法
- android短信发送和接受
- Android动态广播的发送和接收
- 黑盾网安 网站渗透基础类学习
- 海量数据统计:海量日志提取最常访问IP,最常使用的query
- Unity3D-rigidBody.velocity
- 如何彻底卸载MySQL
- vim插件:pyflakes
- Android广播的发送和接受
- TCP拥塞控制 (1)
- iOS随机颜色
- HDU—2899—strange fuction—【二分】【精度控制】
- java中获取二维数组或者多维数组第几维的方法
- [leetcode] Next Permutation
- iOS 如何选择delegate、notification、KVO?
- java的基本特征:封装,继承,多态
- 传智播客html视频教程(第六季) 杨中科