首页 技术 正文
技术 2022年11月14日
0 收藏 778 点赞 5,242 浏览 4486 个字

1. 发送广播

使用以下三个API可以发送广播

public void click(View view){
Intent intent = new Intent();
intent.setAction("com.itheima.xxxooo");
//把这个自定义的广播发送出去
//sendBroadcast(intent); //发送一条无序的广播事件
//如果广播事件是无序发送出去的 所有的广播接受者 都会接受到这个事件//如果广播是有序的发送出去的, 广播接收者会按照优先级 接受到广播事件
// 有序广播 特点: 高优先级的广播接受者 可以终止掉 广播事件
//sendOrderedBroadcast(intent, null);//第三个参数设置, 无论此接受者的优先级有多低,或是其他中断了广播,他最后都能接收到
sendOrderedBroadcast(intent, null, new FinalRecevier(), null, 0, null, null);
}

2. 接收广播,继承BroadCastReceiver, 需要在xml权限中注册接收器, 并且有些广播需要添加权限

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("reveriver 1 接收到了广播");
Toast.makeText(context, "检查到了 自定义的广播事件", 1).show();
}
}

AndroidManifest.xml 注册接受者  可以设置优先级  int, 值越大 优先级越高

<receiver android:name=".MyBroadcastReceiver" >
<intent-filter android:priority="1000">
<action android:name="com.kevin.xxxooo" >
</action>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiver2" >
<intent-filter android:priority="1000">
<action android:name="com.kevin.xxxooo" >
</action>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiver3" >
<intent-filter android:priority="1000">
<action android:name="com.kevin.xxxooo" >
</action>
</intent-filter>
</receiver>

广播接收者的注册可以在xml文件中, 也可以在代码中注册(一般在 onCreate这样的开始创建的方法中注册)

MyBroadcastReceiver  myReceiver = new MyBroadcastReceiver ();
IntentFilter filter = new IntentFilter();
filter.addAction("com.kevin.xxxooo");
registerReceiver(myReceiver, filter);

3. 示例:拦截电话拨号

public class OutCallReceiver extends BroadcastReceiver {//当有广播事件产生的时候 就会执行onrecevie方法
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("onreceiver 发现了新的外拨电话...");
String number = getResultData();//外拨的电话号码
System.out.println("number="+number);
//替换掉这个号码SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
String ipnumber = sp.getString("ipnumber", "");
String newnumber = ipnumber+number;
//设置外拨的电话号码
setResultData(newnumber);
//不会生效的 广播终止不了 显示的指定了接受者
abortBroadcast();
}
}

AndroidManifest.xml  需要设置权限 和 注册广播接收者

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.ipdail"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.ipdail.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 定义了一个广播接受者 new出来了一个收音机 ,设置action 就相当于设置了监听的频道 -->
<receiver android:name=".OutCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application></manifest>

4.示例: 短信拦截

public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("短信收到了...");Object[] pdus = (Object[]) intent.getExtras().get("pdus");
for (Object pdu : pdus) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);
String body = smsMessage.getMessageBody();
String sender = smsMessage.getOriginatingAddress();
System.out.println("body:" + body);
System.out.println("sender:" + sender);
if ("5556".equals(sender)) {
// 短信的拦截, 高优先级的receiver将短信拦截,低优先级的就接受不到了
abortBroadcast();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(sender, null, "我已经喜欢上 xxx了 ,你去死吧", null, null);
}}
}
}

AndroidManifest.xml 权限配置

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.smslistener"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.Translucent" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <receiver android:name=".SmsReceiver" >
<intent-filter android:priority="1000" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application></manifest>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289