这个也是在别的地方看的,具体不记得了,自己只是做个备份,以免以后找不到。
android2.3之前的版本可以使用ITelephony.answerRingingCall()方法来接通电话,可惜后来被认为是危险的api,那个PHONE_CHANGE_STATE仅限于system用户使用,应用级的程序不能再访问这个。前些天偶然搜索到了一个帖子,能够解决使用代码接通。忘了在哪儿找的了,也没法引用转载地址了。
public void autoAnswerPhone() { try { TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); Class<TelephonyManager> c = TelephonyManager.class; Method mthEndCall = null; mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null); mthEndCall.setAccessible(true); ITelephony itelephony = (ITelephony) mthEndCall.invoke(telMag, (Object[]) null); //itelephony.silenceRinger(); itelephony.answerRingingCall(); } catch (Exception e) { e.printStackTrace(); Application app = getApplication(); try { Intent intent = new Intent("android.intent.action.MEDIA_BUTTON"); KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK); intent.putExtra("android.intent.extra.KEY_EVENT",keyEvent); app.sendOrderedBroadcast(intent,"android.permission.CALL_PRIVILEGED"); //TApplication.nowApplication.sendOrderedBroadcast(intent,"android.permission.CALL_PRIVILEGED"); intent = new Intent("android.intent.action.MEDIA_BUTTON"); keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK); intent.putExtra("android.intent.extra.KEY_EVENT",keyEvent); getApplication().sendOrderedBroadcast(intent,"android.permission.CALL_PRIVILEGED"); //TApplication.nowApplication.sendOrderedBroadcast(intent,"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"); getApplication().sendOrderedBroadcast(localIntent1,"android.permission.CALL_PRIVILEGED"); //TApplication.nowApplication.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); app.sendOrderedBroadcast(localIntent2,"android.permission.CALL_PRIVILEGED"); //TApplication.nowApplication.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); app.sendOrderedBroadcast(localIntent3,"android.permission.CALL_PRIVILEGED"); //TApplication.nowApplication.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"); app.sendOrderedBroadcast(localIntent4,"android.permission.CALL_PRIVILEGED"); //TApplication.nowApplication.sendOrderedBroadcast(localIntent4,"android.permission.CALL_PRIVILEGED"); } catch (Exception e2) { e2.printStackTrace(); Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK); meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT,keyEvent); app.sendOrderedBroadcast(meidaButtonIntent, null); //TApplication.nowApplication.sendOrderedBroadcast(meidaButtonIntent, null); } } }