xxxxManager通过getsystemservice获得,管理xxxxService
ServiceManager管理xxxxService的Binder通讯
xxxxManager设置了hide api、system api
telephoneManager hide call dial endcall
listen(PhoneStateListener listener, int events):注册监听事件,监听指定的状态变化
例如:TelephonyManager#CALL_STATE_IDLE 没有通话
TelephonyManager#CALL_STATE_RINGING 一个新的通话进来,正在响铃或等待
TelephonyManager#CALL_STATE_OFFHOOK 至少一个通话存在,并且通话正在进行,没有新的通话进来
TelephonyManager#DATA_UNKNOWN 初始值,在状态获取前使用
TelephonyManager#DATA_DISCONNECED 网络连接失败
TelephonyManager#DATA_CONNECTIN 正在启动数据连接
TelephonyManager#DATA_CONNECTED 数据连接成功
TelephonyManager#DATA_SUSPENDED 数据连接暂停,如,一个通话到达,网络切换到2G
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener=new PhoneStateListener(){
@Override
//监测电话状态的变化。
public void onCallStateChanged(int state, String incomingNumber) { // state 电话的状态 incomingNumber 打进来的号码
switch(state){
//无状态
case TelephonyManager.CALL_STATE_IDLE:
break;
//通话状态
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
//响铃状态
case TelephonyManager.CALL_STATE_RINGING:
int mode= Blackmdb.getInstance(getApplicationContext()).queuenumber(incomingNumber);
if(mode==2||mode==3){
Log.d("挂断电话","挂断");
endcall();
}
break;
default:
break;
}
super.onCallStateChanged(state,incomingNumber);
}
};
tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
getSystemService(String name)
public int getCallState() {
try {
ITelecomService telecom = getTelecomService();
if (telecom != null) {
return telecom.getCallState();
}
} catch (RemoteException e) {
Log.e(TAG, "Error calling ITelecomService#getCallState", e);
}
return CALL_STATE_IDLE;
}
private ITelephony getITelephony() {
return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
}
private ITelecomService getTelecomService() {
return ITelecomService.Stub.asInterface(ServiceManager.getService(Context.TELECOM_SERVICE));
}
ServiceMangager是隐藏类,只能通过反射调用。
private void endcall() {
//利用反射机制挂断电话
try {
//获取类对象
Class clazz= Class.forName("android.os.ServiceManager");
//获取类中方法,这个方法是非静态的
Method mothod = clazz.getDeclaredMethod("getService",String.class);
//执行这个方法
IBinder binder= (IBinder) mothod.invoke(null, Context.TELEPHONY_SERVICE);
ITelephony iTelephony = ITelephony.Stub.asInterface(binder);
iTelephony.endCall();//挂断电话
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}