Aidl挂接电话
ITelephony.aidl
package com.android.internal.telephony; interface ITelephony { void answerRingCall(); boolean endCall(); }
MyPhoneService
public class MyPhoneService extends Service { TelephonyManager manager; ITelephony telephony; @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { run(); return super.onStartCommand(intent, flags, startId); } private void run() { manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // 获取类类型 Class<TelephonyManager> clazz = TelephonyManager.class; try { // 通过方法名和参数找到方法 Method method = clazz.getDeclaredMethod("getITelephony", null); // 将此方法设为可用 method.setAccessible(true); // 执行该方法 telephony = (ITelephony) method.invoke(manager, null); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } // 设置电话状态监听 PhoneStateListener listener = new MyPhoneStateListener(); manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); } class MyPhoneStateListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_OFFHOOK:// 当摘机时 Log.i("onCallStateChanged", "CALL_STATE_OFFHOOK" + incomingNumber); try { telephony.endCall(); } catch (RemoteException e) { e.printStackTrace(); } break; case TelephonyManager.CALL_STATE_RINGING:// 当响铃时 Log.i("onCallStateChanged", "CALL_STATE_RINGING" + incomingNumber); try { // 接电话 telephony.answerRingCall(); } catch (RemoteException e) { e.printStackTrace(); } break; case TelephonyManager.CALL_STATE_IDLE: // 当空闲时 Log.i("onCallStateChanged", "CALL_STATE_IDLE" + incomingNumber); break; default: break; } super.onCallStateChanged(state, incomingNumber); } } }