广播BroadcastReceiver(2)

来源:互联网 发布:wind数据库一年多少钱 编辑:程序博客网 时间:2024/05/06 22:12
  有序广播的优先级:
       发送有序广播的方法有:
   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中,还要把写好的广播添加到这里边。

0 0
原创粉丝点击