广播BroadcastReceiver(2)
发送有序广播的方法有:
public void sendOrderedBroadcast(Intent intent,String receiverPermission)
在接收有序广播时,能够自己为接收者指定优先级:
静态注冊广播接收者时,在AndroidMainfest.xml文件里为<receiver>的<intent-filter>节点配置
android:priority属性;
动态注冊广播接收者时。调用IntentFilter对象的setPriority()方法;
有序广播的优先级:
优先级属性值越大,则优先级越高;
优先级属性同样时,动态注冊的广播接收者优先级高于静态注冊的广播接收者。
同为静态/动态注冊的广播接收者。且优先级同样时,越早注冊的优先级越高。
同为静态注冊的广播接收者。且优先级同样,但归属于不同的应用程序时,
按系统载入应用程序的先后顺序确定优先级(通常表现为按应用程序的包名排序);
始终接收广播的方法:
某些应用程序可能须要始终接收特定的广播。比如手机管理类软件,则须要始终接收主叫、
来电、短信等广播,为了正常接收到这些广播。且让自己的应用程序处于高优先级,一般会:
使用一个静态注冊的广播接收者。接收开机广播;
接收到开机广播后,启动一个Service在后台执行;
在后台执行的Service中,动态的广播接收者,接收业务所需的广播。
有序广播的传递
中止有序广播
在广播接收者中,调用abortBroadcast()则能够中止有序广播的继续传递。
即优先级更低的BroadcastReceiver将不会收到该广播;
须要注意的是:使用该方法仅能用于接收有序广播,假设接收普通广播时调用
该方法则会导致程序异常。
在广播接收者们之间传递数据:
在有序广播的传递过程中,能够调用setResultXXX()系列方法设置数据,
比如:setResultData()、setResultExtras()等,并使用getResultXXX()系列方法获取这些数据。
管理呼出电话
呼出电话的广播为:
android.intent.action.NEW_OUTGOING_CALL
对呼出电话的管理的原理:
呼出电话是从拨号到呼叫的过程,当呼出电话时,系统会发出呼叫电话的有序广播,而且将须要呼叫的电话号码
使用setResultData()进行传递。处理程序接收到该广播后运行呼叫。
处理呼叫的程序接收该广播的优先级较低,因此,自己定义广播接收者。
在接收到呼出电话的广播后,改动setResult()传递的电话号码。就可以实现对呼出电话的管理。
对呼出的电话进行操作的实例:
Activity不用改动。默认状态就可以。
这里仅仅给出广播:
package com.example.chargecall;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;public class OutReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {// 改动呼出的电话号码:/*能够对电话号码进行多种操作。假设想要禁止呼出的电话号码,能够直接设置为空值* * */String number = getResultData();number = "12345" + number;setResultData(number);}}
在AndroidMainfest.xml中加入权限跟receiver:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<receiver android:name="com.example.chargecall.OutReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
对呼入的电话进行拦截:
拦截呼入电话的实现原理:
当存在呼入电话时,系统发出例如以下广播:
android.intent.action.PHONE_STATE
TeleohyManager是电话管理器,为其配置PhoneStateListener就可以监听相关状态(响铃、通话、空暇),且
该监听器的会调方法中可获取来电号码;
调用ITelephony接口定义的boolean endCall()方法就可以挂断电话。通过TelephonyManager的getTelephony()
方法就可以获得ITelephony接口的对象。
对呼入的电话进行操作的实例:
相同的。主界面使用默认的就可以,
给出广播:
package com.example.interceptcall;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;import com.android.internal.telephony.ITelephony;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;public class PhoneStateReceiver extends BroadcastReceiver {TelephonyManager manager;@Overridepublic void onReceive(Context context, Intent intent) {manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);PhoneStateListener listener = new InnerPhoneStateListener();int events = PhoneStateListener.LISTEN_CALL_STATE;manager.listen(listener, events);}private ITelephony getITelephony(){ITelephony iTelephony = null;Method method = null;try {method = TelephonyManager.class.getDeclaredMethod("getITelephony", (Class[])null);method.setAccessible(true);iTelephony = (ITelephony) method.invoke(manager, null);} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}return iTelephony;}private class InnerPhoneStateListener extends PhoneStateListener{@Overridepublic void onCallStateChanged(int state, String incomingNumber) {// TODO Auto-generated method stubswitch (state) {case TelephonyManager.CALL_STATE_IDLE://空暇状态break;case TelephonyManager.CALL_STATE_OFFHOOK://通话状态break;case TelephonyManager.CALL_STATE_RINGING://响铃状态if("15539187816".equals(incomingNumber)){//挂断电话try {getITelephony().endCall();} catch (RemoteException e) {e.printStackTrace();}}break;}super.onCallStateChanged(state, incomingNumber);}}
}
因为不能识别ITelephony,还须要自己定义aidl接口,这样才干够,
ITelephony:
package com.android.internal.telephony;interface ITelephony {boolean endCall();
}
注意包名要使用这里边的包名,
相同,要把权限加到AndroidMainfest.xml中,还要把写好的广播加入到这里边。
转载于:https://www.cnblogs.com/claireyuancy/p/7043929.html
广播BroadcastReceiver(2)相关推荐
- Android 广播BroadcastReceiver
一.全局广播动态使用 1.注册广播 BroadcastReceiver receiver= new BroadcastReceiver() {@Overridepublic void onReceiv ...
- 发送广播BroadcastReceiver
import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android ...
- 广播 BroadCastReceiver
1.概述 Broadcast Receiver包含两部分功能: 发送和接收广播消息 调用sendBroadCast()方法发送广播,即发送一个Intent 发送广播只能启动广播接收器Receiver对 ...
- 广播BroadcastReceiver使用
1️⃣动态广播(跟随组件的生命周期变化) ActivityOne 发送广播 sendBroadcast(new Intent(ActivityTwo.IS_FINISH)); ActivityTwo ...
- java eventbus 原理_事件总线,事件驱动(RxJava,EventBus)与广播(BroadcastReceiver)(2)
> 事件总线与广播 事件总线通过注解或者反射的方式自动维护和调用. otto和AndroidEventBus采用的是注解,EventBus则是规定方法名基础上采用反射. 从调度策略角度,Even ...
- android广播intent原理,Android中BroadcastReceiver详解
BroadcastReceiver是什么? Android app可以发送广播也可以接收系统或者其它app发送的广播,是发送/订阅的设计模式.这些广播被发送当重要的事件发生的时候.例如,安卓系统发送广 ...
- BroadcastReceiver 接收系统短信广播
BroadcastReceiver 接收系统短信广播 /* 注册广播: * |--静态注册 (在AndroidManifest .xml 中注册) * <receiver androi ...
- android 发送前台广播,使用IntentService与BroadcastReceiver实现后台服务(Android7.0可用)...
IntentService的优点 IntentService会创建单独的线程处理所有的Intent请求, 会处理onHandleIntent方法实现的代码, 隐藏开发者无须处理多线程问题, 当所有请求 ...
- android 静态注册wifi广播,Android中BroadcastReceiver详解
BroadcastReceiver是什么? Android app可以发送广播也可以接收系统或者其它app发送的广播,是发送/订阅的设计模式.这些广播被发送当重要的事件发生的时候.例如,安卓系统发送广 ...
- Android详细教程(基础篇):二十八、Android 广播机制BroadcastReceiver
广播机制(BroadcastReceiver) 11.1. BroadcastReceiver简介 在实际应用中,我们常需要等,等待系统抑或其他应用发出一道指令,为自己的应用擦亮明灯指明方向.而这种等 ...
最新文章
- 4dda在linux中的意思,Evvail | MaxQuant-蛋白质组DDA数据分析金标准 | Omics - Hunter
- ASP错误捕获的几种常规处理方式
- 可自动定时切换的选项卡/滑动门导航代码
- openssl 生成证书_使用证书和私钥导出P12格式个人证书!
- 08-图7 公路村村通 (30 分)
- 【HDU - 5884】Sort(k叉哈夫曼树,优化tricks,两个队列代替优先队列)
- 为什么linux远程一进入sudo su就卡住_Linux 实战(上)
- 计算机二级等级考试模拟试题,2016年计算机二级考试office模拟试题
- 我参与的一个项目总结
- Windows下打开Url 代码封装 C++
- Putty server refused our key的解决方法
- Oracle 树操作、递归查询(select…start with…connect by…prior)
- usb转rs232驱动无效,已经电脑自动扫描检测硬件改动.设备无法运行错误代码10
- 较全的正则字符、规则汇总
- python爬虫精进第5关前5页歌词爬-Python抓取歌词自制FreeStyle
- 开发板和电脑网线连接,电脑网线连接 ubuntu18.04 nfs搭建
- mfrc522 c语言程序,用mxgxw/MFRC522python库和MFRC522读卡器在raspi2上编写ntag213(Mifare ultralightc)...
- 虚假营销、口碑下滑、产品单一,小仙炖难圆“燕窝一姐”梦
- SIP–eyebeam配置手册
- GPU的并行运算与CUDA的简介
热门文章
- 传感器到底在“偷窥”什么
- 仅为代码实际运行资源付费 解构国内首个函数计算
- CentOS6.5下安装mfs分布式存储(转)
- nginx RTMP FFmpeg 视频直播
- Xcode 模拟器复制解决方案
- 停止预览时调用Camera.release(), 出现Method called after release()异常问题原因及解决办法...
- C语言读取文件大量数据到数组
- scala Basic 第三课
- 13幅逻辑图,领略杜克大学的经典思维
- 《管理3.0》读书笔记