Android 自定义广播实例以及系统广播(基础)(二)
来源:互联网 发布:vb radiobutton 分组 编辑:程序博客网 时间:2024/06/08 17:43
继续昨天讲的内容,昨天讲的广播的无序排序那么今天来讲讲有序排序。
其实只要你设置接受者的优先级,再把发送方式改成有序排序(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) } } }}
好了今天的分享就到这里了,下次见啦。
0 0
- Android 自定义广播实例以及系统广播(基础)
- Android 自定义广播实例以及系统广播(基础)(二)
- Android广播之发送自定义广播+本地广播(二)
- Android 自定义广播和系统广播(电话状态)(基础)
- Android自定义广播(Broadcast)与系统广播
- Android自定义广播实例
- android 广播(二)
- Android 发送系统广播与自定义广播
- android 中的广播 ,系统广播和自定义广播
- Android笔记(二十五)发送自定义广播和本地广播
- 【Android】广播大全(二)
- Android系统广播(来电示例) 系统广播大全
- Android之广播三(发送自定义广播)
- Android之BroadcastReceiver(二):接收系统广播
- Android中的广播机制(二)----- 发送广播
- Android广播管理二--广播注册(registerReceiver)流程分析
- Android广播之接收系统广播(一)
- Android 自定义广播(无序广播) BroadcastReceiver广播接受者(静态、动态)
- pat a1006
- Linux编程里getopt_long_only函数用法详解
- 机器学习笔记 - Logistic Regression
- Linux驱动技术(四) _异步通知技术
- Java容器(十):subList的陷阱
- Android 自定义广播实例以及系统广播(基础)(二)
- RBAC权限管理
- ASCII 对照表
- USB接口、串口、并口有何区别
- UML状态图、活动图
- Go和Gin的环境配置
- win命令启动程序python脚本
- cookie 和session 的区别详解
- luci模块练习,详细步骤。。