在src文件夹下创建包com.Android.internal.telephony,在包下建一个文件ITelephony.aidl文件内容如下:
- package com.android.internal.telephony;
- interface ITelephony{
- boolean endCall();
- void answerRingingCall();
- }
创建了之后会在gen目录下生成相应的文件ITelephony.java
在你要调用的文件中:
- private static ITelephony getITelephony(Context context) {
- TelephonyManager mTelephonyManager = (TelephonyManager) context
- .getSystemService(TELEPHONY_SERVICE);
- Class<TelephonyManager> c = TelephonyManager.class;
- Method getITelephonyMethod = null;
- ITelephony iTelephony = null ;
- try {
- getITelephonyMethod = c.getDeclaredMethod("getITelephony",
- (Class[]) null); // 获取声明的方法
- getITelephonyMethod.setAccessible(true);
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
-
- try {
- iTelephony = (ITelephony) getITelephonyMethod.invoke(
- mTelephonyManager, (Object[]) null); // 获取实例
- return iTelephony;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return iTelephony;
- }
然后通过该方法获得
ITelephony 对象,然后调用它的answerRingingCall()方法实现接电话,调用它的endCall()方法实现挂电话。