Android 实现挂电话和接电话


在src文件夹下创建包com.Android.internal.telephony,在包下建一个文件ITelephony.aidl

文件内容如下:

  1. package com.android.internal.telephony;  
  2. interface ITelephony{  
  3.     boolean endCall();  
  4.     void answerRingingCall();  
  5. }  
创建了之后会在gen目录下生成相应的文件ITelephony.java


在你要调用的文件中:

  1. private static ITelephony getITelephony(Context context) {  
  2.       TelephonyManager mTelephonyManager = (TelephonyManager) context  
  3.               .getSystemService(TELEPHONY_SERVICE);  
  4.       Class<TelephonyManager> c = TelephonyManager.class;  
  5.       Method getITelephonyMethod = null;  
  6.       ITelephony iTelephony = null ;  
  7.       try {  
  8.           getITelephonyMethod = c.getDeclaredMethod("getITelephony",  
  9.                   (Class[]) null); // 获取声明的方法   
  10.           getITelephonyMethod.setAccessible(true);  
  11.       } catch (SecurityException e) {  
  12.           e.printStackTrace();  
  13.       } catch (NoSuchMethodException e) {  
  14.           e.printStackTrace();  
  15.       }  
  16.   
  17.       try {  
  18.           iTelephony = (ITelephony) getITelephonyMethod.invoke(  
  19.                   mTelephonyManager, (Object[]) null); // 获取实例   
  20.           return iTelephony;  
  21.       } catch (Exception e) {  
  22.           e.printStackTrace();  
  23.       }  
  24.       return iTelephony;  
  25.   }  
然后通过该方法获得ITelephony 对象,然后调用它的answerRingingCall()方法实现接电话,调用它的endCall()方法实现挂电话。

相关内容