Android有序广播(系统有序广播SMS)
2017-02-10 15:59
330 查看
无序广播可以查看我的第一篇博客,地址安卓之自定义广播发送者和接受者(动态-静态-黏性)
有序广播实现
系统有序广播SMS
有序广播通过Context.sendOrderedBroadcast来发送。所有的receiver依次执行。
有序广播的实现
首先需要创建一个发送者
上面是布局文件
对应的java代码
接受者01
AndroidManifest.xml
MyReceived01.java
接受者02
**
以上就是有序广播的实现可以看到因为01号设置了android:priority=”999”优先级,所有01号优先接受到了广播(android:priority这个值从1000~-1000,数值越大,优先级别就越高。)
系统有序广播SMS(简单实现)
首先需要在清单文件中添加读取短信的权限
java代码
以上就是SMS的实现了,有兴趣的小伙伴可以尝试根据短信内容判断是否为广告信息然后拦截它。
有序广播实现
系统有序广播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的实现了,有兴趣的小伙伴可以尝试根据短信内容判断是否为广告信息然后拦截它。
相关文章推荐
- Android四大组件——BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- Android 接收系统广播,开机启动Service,SMS,Battery
- Android四大组件——BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- Android四大组件——BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- android 监听系统新安装程序的广播
- android 监听系统新安装程序的广播
- Android 2.2.1系统广播大全
- android有序广播和无序广播的区别
- Android系统中的广播(Broadcast)机制简要介绍和学习计划
- Android系统中关于短信(SMS)的操作
- android有序广播和无序广播的区别
- Android利用系统广播---监听应用程序安装和卸载
- Android 系统广播
- 通过广播Intent控制Android系统自带的音乐播放器
- Android利用系统广播---监听应用程序安装和卸载
- android-BroadcastReceiver 发送有序广播
- Android利用系统广播---监听应用程序安装和卸载
- Android利用系统广播---监听应用程序安装和卸载
- Android的BroadcastReciver收不到Broadcast(系统广播)的原因之一
- Android利用系统广播---监听应用程序安装和卸载