继续昨天讲的内容,昨天讲的广播的无序排序那么今天来讲讲有序排序。
其实只要你设置接受者的优先级,再把发送方式改成有序排序(sendOrderedBroadcast)就可以实现有序排序,大概的都和昨天的很像不做详讲了。
当你为有序排序的时候就可以拦截或修改短信内容了,借此可以做黑名单拦截了,等下会讲讲读取别人发过来的短信判断是否是骚扰短信,不过今天还不能完全把拦截黑名单做出来。
直接上代码了
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zking.sun.android_14_received01">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--配置广播接受者:
1.在清单文件中配置(静态广播)
2.使用java 代码(动态广播)
-->
<receiver android:name=".Receive01">
<!--设置优先级-->
<intent-filter android:priority="999">
<action android:name="com.zking.sun.android_14_sender.923"></action>
</intent-filter>
</receiver>
</application>
</manifest>
package com.zking.sun.android_14_received01;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
/**
* Created by sun on 2017/2/7.
*/
public class Receive01 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if("com.zking.sun.android_14_sender.923".equals(intent.getAction())){
String data = intent.getStringExtra("data");
Log.i("data","I'm receive one :"+data);
//改变内容
Bundle bundle = new Bundle();
bundle.putString("content",data+",今晚可以看星星");
setResultExtras(bundle);
// if("中奖了".equals(data)){
// //阻止
// abortBroadcast();
// }
}
}
}
接下来讲讲读取短信的,就是别人发送短信过来判断,是否是骚扰短信。
首先你要在清单文件中给他设读取短信的权限
<!--读取短信的权限-->
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--注册短信接收-->
<receiver android:name=".MySMSReceived">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
写一个类来收
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 < smsMessages.length; i++) {
smsMessages[i] = SmsMessage.createFromPdu((byte[]) objects[i]);
}
for (SmsMessage smsMessage : smsMessages) {
//得到联系人的地址
String address = smsMessage.getDisplayOriginatingAddress();
body = smsMessage.getMessageBody();//得到短息内容
Log.i("test","发送人:"+ address);
Log.i("test","内容:"+ body);
}
//骚扰短信的拦截
if (body.contains("中奖啦")){
//就拦截
Log.i("test","我读取到了,并且拦截了。。。。");
abortBroadcast();//不传播下去
//删除短信(利用ContentProvider)
}
}
}
}
好了今天的分享就到这里了,下次见啦。