核心代码:
Class<TelephonyManager> c = TelephonyManager.class; try { Method getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null); getITelephonyMethod.setAccessible(true); ITelephony iTelephony = null; iTelephony = (ITelephony) getITelephonyMethod.invoke(telephonyManager, (Object[]) null); iTelephony.endCall(); } catch (Exception e) { Log.e(TAG, "Fail to accept call.", e); }
Class<TelephonyManager> c = TelephonyManager.class; try { Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class); IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE}); ITelephony telephony = ITelephony.Stub.asInterface(binder); telephony.answerRingingCall(); }catch (Exception e) { Log.e(TAG, "Fail to accept call.", e); }
2. android 2.3版本
(1)不使用耳机
private void answerRingingCall() { //插耳机 Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG); localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); localIntent1.putExtra("state", 1); localIntent1.putExtra("microphone", 1); localIntent1.putExtra("name", "Headset"); sendOrderedBroadcast(localIntent1,"android.permission.CALL_PRIVILEGED"); //按下耳机按钮 Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HEADSETHOOK); localIntent2.putExtra("android.intent.extra.KEY_EVENT", localKeyEvent1); sendOrderedBroadcast(localIntent2,"android.permission.CALL_PRIVILEGED"); //放开耳机按钮 Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK); localIntent3.putExtra("android.intent.extra.KEY_EVENT", localKeyEvent2); sendOrderedBroadcast(localIntent3, "android.permission.CALL_PRIVILEGED"); //拔出耳机 Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG); localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); localIntent4.putExtra("state", 0); localIntent4.putExtra("microphone", 1); localIntent4.putExtra("name", "Headset"); sendOrderedBroadcast(localIntent4,"android.permission.CALL_PRIVILEGED"); }(2)需使用耳机(在网上很多人提供了这个方案,所以在这里贴出来)
private void answerRingingCall() { //按下耳机按钮 Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HEADSETHOOK); localIntent2.putExtra("android.intent.extra.KEY_EVENT",localKeyEvent1); sendOrderedBroadcast(localIntent2,"android.permission.CALL_PRIVILEGED"); //放开耳机按钮 Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK); localIntent3.putExtra("android.intent.extra.KEY_EVENT",localKeyEvent2); sendOrderedBroadcast(localIntent3,"android.permission.CALL_PRIVILEGED"); }
更高版本的没有相关机器测试。
从http://blog.sina.com.cn/s/blog_6dc1188a010176cy.html获得的方案
(1) 不使用耳机
private void answerRingingCall() { //放开耳机按钮 Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK); localIntent3.putExtra("android.intent.extra.KEY_EVENT",localKeyEvent2); sendOrderedBroadcast(localIntent3,"android.permission.CALL_PRIVILEGED"); //插耳机 Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG); localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); localIntent1.putExtra("state", 1); localIntent1.putExtra("microphone", 1); localIntent1.putExtra("name", "Headset"); sendOrderedBroadcast(localIntent1,"android.permission.CALL_PRIVILEGED"); //按下耳机按钮 Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_HEADSETHOOK); localIntent2.putExtra("android.intent.extra.KEY_EVENT",localKeyEvent1); sendOrderedBroadcast(localIntent2,"android.permission.CALL_PRIVILEGED"); //放开耳机按钮 localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON); localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK); localIntent3.putExtra("android.intent.extra.KEY_EVENT",localKeyEvent2); sendOrderedBroadcast(localIntent3,"android.permission.CALL_PRIVILEGED"); //拔出耳机 Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG); localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); localIntent4.putExtra("state", 0); localIntent4.putExtra("microphone", 1); localIntent4.putExtra("name", "Headset"); sendOrderedBroadcast(localIntent4,"android.permission.CALL_PRIVILEGED"); }(2)使用耳机同上面的2(2)