Android 自定义广播实例以及系统广播(基础)(二)

本文介绍如何在Android中实现广播接收器的有序排序,通过设置优先级和使用sendOrderedBroadcast方法。有序广播允许拦截和修改短信内容,可用于创建简单的黑名单拦截功能。虽然目前尚未完整实现拦截黑名单,但讨论了读取短信的权限设置和接收短信的类的编写。下次将分享更多相关内容。
摘要由CSDN通过智能技术生成

继续昨天讲的内容,昨天讲的广播的无序排序那么今天来讲讲有序排序。
其实只要你设置接受者的优先级,再把发送方式改成有序排序(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)
            }


        }

    }
}

好了今天的分享就到这里了,下次见啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值