标签:
在Android平台中,通过TelephonyManager可以访问与手机通讯相关的信息,比如设备信息、网络信息及SIM卡信息,同时还可以监听电话的相关状态。下面我们通过几个方面来说明Android平台中如何使用电话技术。
1、获取TelephonyManager对象
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
2、通过TelephonyManager获取设备信息
String phoneTypeStr = "未知"; int phoneType = telephonyManager.getPhoneType(); switch (phoneType){ case TelephonyManager.PHONE_TYPE_CDMA: phoneTypeStr="CDMA"; break; case TelephonyManager.PHONE_TYPE_GSM: phoneTypeStr="GSM"; break; case TelephonyManager.PHONE_TYPE_SIP: phoneTypeStr="SIP"; break; case TelephonyManager.PHONE_TYPE_NONE: phoneTypeStr="None"; break; } addTelephoneView("电话类型",phoneTypeStr); //GSM--IMEI CDMA--MEID String deviceId = telephonyManager.getDeviceId(); addTelephoneView("设备编号",deviceId); //软件版本编号 String softVersion = telephonyManager.getDeviceSoftwareVersion(); addTelephoneView("软件版本",softVersion); //手机号码(不一定能获取到) String phoneNumber = telephonyManager.getLine1Number(); addTelephoneView("手机号码",phoneNumber);
3、通过TelephonyManager获取网络信息
//网络信息:连接到一个网络时有效,如果是CDMA则不一定有效 //国家ISO代码 String networkCountry = telephonyManager.getNetworkCountryIso(); addTelephoneView("国家ISO代码",networkCountry); //运营商信息 String networkOperatorId = telephonyManager.getNetworkOperator(); addTelephoneView("运营商编号",networkOperatorId); String networkOperatorName=telephonyManager.getNetworkOperatorName(); addTelephoneView("运营商名称",networkOperatorName); //网络连接状态 int networktype = telephonyManager.getNetworkType(); addTelephoneView("网络类型",networktype+"");
4、通过TelephonyManager获取SIM卡信息
//SIM卡的状态 int simState = telephonyManager.getSimState(); switch (simState){ case TelephonyManager.SIM_STATE_READY: //SIM卡的ISO国家代码 String simISO = telephonyManager.getSimCountryIso(); addTelephoneView("SIM国家ISO",simISO); //SIM卡的运营商代码 String simOperator = telephonyManager.getSimOperator(); addTelephoneView("SIM运营商编号",simOperator); //SIM的运营商名称 String simOperatorName = telephonyManager.getSimOperatorName(); addTelephoneView("SIM运营商名称",simOperatorName); //SIM的序列号‘ String number = telephonyManager.getSimSerialNumber(); addTelephoneView("SIM序列号",number); break; }
注意获取信息的时候需要添加如下权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
运行结果如下:
5、通过继承PhoneStateListener,使用TelephonyManager注册后进行监听电话状态。
PhoneStateListener可以通过重写方法onCallStateChanged、onDataConnectionStateChanged、onDataActivity等方法对来电状态、数据连接状态、数据传输状态进行监听。 通过TelephonyManager.listen方法进行注册,该方法有两个参数,一个参数为PhoneStateListener,第二个参数为监听标志,决定了监听哪些状态。 PhoneStateListener的之类代码如下:
//定义电话状态监听器 private PhoneStateListener listener = new PhoneStateListener(){ @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); String msg = "未知"; switch (state){ case TelephonyManager.CALL_STATE_IDLE: msg="电话空闲"; break; case TelephonyManager.CALL_STATE_RINGING: msg = incomingNumber+"来电话了,响铃中……"; break; case TelephonyManager.CALL_STATE_OFFHOOK: msg = "电话中……"; break; } Toast.makeText(TelephonyActivity.this,msg,Toast.LENGTH_LONG).show(); } @Override public void onDataConnectionStateChanged(int state) { super.onDataConnectionStateChanged(state); String msg = "未知"; switch (state){ case TelephonyManager.DATA_CONNECTED: msg="数据已连接"; break; case TelephonyManager.DATA_CONNECTING: msg = "数据正在连接中……"; break; case TelephonyManager.DATA_DISCONNECTED: msg = "数据已关闭!"; break; } show.setText(msg); } @Override public void onDataActivity(int direction) { super.onDataActivity(direction); String msg = "未知"; switch (direction){ case TelephonyManager.DATA_ACTIVITY_IN: msg = "下载"; break; case TelephonyManager.DATA_ACTIVITY_OUT: msg = "上传"; break; case TelephonyManager.DATA_ACTIVITY_INOUT: msg = "下载/上传"; break; case TelephonyManager.DATA_ACTIVITY_NONE: msg = "未知"; break; } show.setText(msg); } };
注册代码:
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE |PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |PhoneStateListener.LISTEN_DATA_ACTIVITY);
注销代码:
telephonyManager.listen(listener,PhoneStateListener.LISTEN_NONE);
6、通过BroadcastReceiver接收广播监听电话的状态。
当拨打电话时系统会发送Intent.ACTION_NEW_OUTGOING_CALL广播,当电话状态发生变化的时候,系统会发送TelephonyManager.ACTION_PHONE_STATE_CHANGED广播,我们通过接收这两个广播来监听电话的相关状态
BroadcastReceiver的关键代码如下:
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){ //去电 String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); }else if(intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){ //来电 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){ //有来电,响铃中 //TelephonyManager.EXTRA_STATE_IDLE; //TelephonyManager.EXTRA_STATE_OFFHOOK String phoneNumber= intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); //比对电话号码 if(null !=backNumber && null != phoneNumber && backNumber.endsWith(phoneNumber)){ //挂掉电话 //AIDL Android接口定义语言 //IPC ITelephony iTelephony = getITelephony(context); try { iTelephony.endCall(); } catch (RemoteException e) { e.printStackTrace(); } finally { } } }else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){ // }else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){ } } }
在清单文件中注册:
<receiver android:name=".receiver.PhoneStateChanagedReceiver"> <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> <action android:name="android.intent.action.PHONE_STATE"/> </intent-filter> </receiver>
注意此处需要如下权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
7、电话拦截,在6的基础上,完成电话拦截操作,这里需要使用到ITelephony.aidl。
首先在Android Studio的对应的Module中创建aidl目录,和java目录同级,在该目录下创建com.android.internal.telephony包和android.telephony包,然后拷贝ITelephony.aidl和NeighboringCellInfo.aidl到对应的包下,然后从新构建。通过反射拿到ITelephony对象并调用endCall方法挂断电话。
关键代码如下:
private static ITelephony getITelephony(Context context){ ITelephony iTelephony = null; TelephonyManager telephonyManager=(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); Class<TelephonyManager> c = TelephonyManager.class; Method getITelephoneyMethod = null; try { getITelephoneyMethod = c.getDeclaredMethod("getITelephony",(Class[])null); getITelephoneyMethod.setAccessible(true); iTelephony = (ITelephony)getITelephoneyMethod.invoke(telephonyManager,(Object[])null); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return iTelephony; }
标签:
原文地址:http://www.cnblogs.com/jerehedu/p/4877863.html