标签:
有序广播的优先级:在接收到呼出电话的广播后,修改setResult()传递的电话号码,即可实现对呼出电话的管理。
对呼出的电话进行操作的实例:
Activity不用修改,默认状态即可,
这里只给出广播:
package com.example.chargecall; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class OutReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // 修改呼出的电话号码: /*可以对电话号码进行多种操作,如果想要禁止呼出的电话号码,可以直接设置为空值 * * */ String number = getResultData(); number = "12345" + number; setResultData(number); } }
<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>
方法即可获得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; @Override public 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{ @Override public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub switch (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中,还要把写好的广播添加到这里边。
标签:
原文地址:http://blog.csdn.net/wojiaohuangyu/article/details/50455363