Android有序广播(系统有序广播SMS)
来源:互联网 发布:杜蕾斯震动棒 知乎 编辑:程序博客网 时间:2024/05/16 03:05
无序广播可以查看我的第一篇博客,地址安卓之自定义广播发送者和接受者(动态-静态-黏性)
- 有序广播实现
- 系统有序广播SMS
什么是有序广播?
有序广播,即从优先级别最高的广播接收器开始接收,接收完了如果没有丢弃,就下传给下一个次高优先级别的广播接收器进行处理,依次类推,直到最后。
无序与有序的区别
普通广播通过Context.sendBroadcast()方法来发送
有序广播通过Context.sendOrderedBroadcast来发送。所有的receiver依次执行。
有序广播的实现
- 首先需要创建一个发送者
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.zy.sender.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入广播内容" android:id="@+id/et_main_content" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送" android:onClick="send" /></LinearLayout>
上面是布局文件
- 对应的java代码
public class MainActivity extends AppCompatActivity { private EditText et_main_content; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_content = (EditText) findViewById(R.id.et_main_content); intent = new Intent(); //设置广播的名字(设置Action) intent.setAction("com.zy.sender.MainActivity.996444703"); } public void send(View view){ String content=et_main_content.getText().toString(); //携带数据 intent.putExtra("data",content); //发送广播(无序广播)// sendBroadcast(intent); //发送黏性广播 //sendStickyBroadcast(intent); //发送有序广播 sendOrderedBroadcast(intent,null); }}
- 接受者01
AndroidManifest.xml
<receiver android:name=".MyReceived01"> <intent-filter android:priority="999"> <action android:name="com.zy.sender.MainActivity.996444703"></action> </intent-filter> </receiver>
MyReceived01.java
public class MyReceived01 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if("com.zy.sender.MainActivity.996444703".equals(intent.getAction())){ String data=intent.getStringExtra("data"); Log.i("test","01号接受者收到广播了:"+data); } }}
- 接受者02
**
<receiver android:name=".MyReceived02"> <intent-filter> <action android:name="com.zy.sender.MainActivity.996444703"></action> </intent-filter> </receiver>
public class MyReceived02 extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if("com.zy.sender.MainActivity.996444703".equals(intent.getAction())){ String data=intent.getStringExtra("data"); Log.i("test","02号接受者收到广播了:"+data); } }}
以上就是有序广播的实现可以看到因为01号设置了android:priority=”999”优先级,所有01号优先接受到了广播(android:priority这个值从1000~-1000,数值越大,优先级别就越高。)
- 系统有序广播SMS(简单实现)
首先需要在清单文件中添加读取短信的权限
<!--读取短信的权限--> <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<receiver android:name=".MySMSReceived"> <intent-filter android:priority="1000" > <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> </intent-filter> </receiver>
java代码
public class MySMSReceived extends BroadcastReceiver { private String body; @Override public void onReceive(Context context, Intent intent) { if("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())){ Log.i("test","有短信进来了"); //获取短信的内容 Bundle bundle=intent.getExtras(); Object[] objects= (Object[]) bundle.get("pdus"); SmsMessage[] smsMessages=new SmsMessage[objects.length]; for (int i = 0; i < objects.length; i++) { smsMessages[i]=SmsMessage.createFromPdu((byte[])objects[i]); } for (SmsMessage message : smsMessages) { String address = message.getDisplayOriginatingAddress(); body = message.getDisplayMessageBody(); Log.i("test","发送人:"+ address); Log.i("test","内容:"+ body); } } }}
以上就是SMS的实现了,有兴趣的小伙伴可以尝试根据短信内容判断是否为广告信息然后拦截它。
1 0
- Android有序广播(系统有序广播SMS)
- android广播,有序广播介绍
- 43.android广播-有序广播
- android有序广播
- Android有序广播OrderedBroadcast
- android-BroadcastReceiver有序广播
- Android 发送有序广播
- Android-发送有序广播
- Android之有序广播
- Android实践:有序广播
- Android 发送有序广播
- Android -----有序广播
- Android有序广播
- Android有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- swagger出现no response from server错误的解决办法
- cpp——类——成员访问权限
- 初识nodeJs
- web编程从入门到项目实战-Package.json篇
- testng.xml文件结构组成及节点属性说明
- Android有序广播(系统有序广播SMS)
- linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟
- JavaEE struts2 Action的结果配置
- SDUT3371 解题报告
- CGLIB学习笔记
- SpringMVC浅谈
- POI导出Excel笔记
- svn的多项目并行源码管理与自动发布思考
- java获取kafka中的消息数据