今天接到一个之前的功能,现在要拿出来看一看能不能用。打电话接到挂断,在网上搜了半天,自己的代码看了半天也就两种。
第一种 try { Method method = Class.forName("android.os.ServiceManager") .getMethod("getService", String.class); IBinder binder = (IBinder) method.invoke(null, new Object[]{Context.TELEPHONY_SERVICE}); ITelephony telephony = ITelephony.Stub.asInterface(binder); telephony.endCall(); } catch (NoSuchMethodException e) { Log.d("", "", e); } catch (ClassNotFoundException e) { Log.d("", "", e); } catch (Exception e) { }
另一种使用TelePhonyManager去挂断
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); Class tmClazz = tm.getClass(); Method getITelephonyMethod = null; try { getITelephonyMethod = tmClazz.getDeclaredMethod("getITelephony", null); getITelephonyMethod.setAccessible(true); ITelephony iTelephony = (ITelephony) getITelephonyMethod.invoke(tm, null); iTelephony.endCall(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }
然后第一种不好使,报错
java.lang.NoSuchMethodError: No interface method endCall()Z in class Lcom/android/internal/telephony/ITelephony
看了半天是Aidl还有Framework啥的错误,改了一波也没整好,放弃了。
使用第二种,由与我是在一个工具类里写的TelephonyManager,当他.class的时候会报一个空指针的错误。在Acivity里使用时候完美运行,但是没有效果。
然后两种方法,都尝试过后不好使,研究了一上午发现了一个新的方式
TelecomManager tm = (TelecomManager) Context.getSystemService(Context.TELECOM_SERVICE); if(tm != null){ tm.endCall(); }
完美挂掉。权限9.0添加一下:
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/>