首页 > 系统 > Android > 正文

Android有序广播(系统有序广播SMS)

2019-11-09 13:59:40
字体:
来源:转载
供稿:网友

无序广播可以查看我的第一篇博客,地址安卓之自定义广播发送者和接受者(动态-静态-黏性)

有序广播实现系统有序广播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的实现了,有兴趣的小伙伴可以尝试根据短信内容判断是否为广告信息然后拦截它。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表