網上找到的,HTC One X,三星i9000 cm9 4.0.4測試通過,在此記錄一下,大伙需要的話就拿去吧..
1. Android 2.3(不包括)以下,通過獲取aidl遠程服務接口TelephoneyManager來調用它的answerRingingCall方法(ps: 關于此方法具體做法,由于時間關系,我這里就不詳細說啦,大家Google一下吧)
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService;
telephonyService = (ITelephony) m.invoke(tm);
// Silence the ringer and answer the call!
telephonyService.silenceRinger();
telephonyService.answerRingingCall();
2. Android 2.3(包括)以上,如果照樣使用TelephoneyManager獲取到的answerRingingCall方法的話,就會拋沒有 android.permission.MODIFY_PHONE_STATE權限異常,其實你已經配了這個權限的了,但是不好意思,你的App不是系統 軟件,沒有系統簽名,所以還是不能調用,除非,你root了你的手機,把你的app裝到系統軟件里面去,所以這里使用另外一種方法實現自動接聽這個行為 了,詳細如下:
剛開始我用這段代碼的,發現三星機型可以,但HTC(如: G10 , One X)不行,
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);
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);
sendOrderedBroadcast(intent,"android.permission.CALL_PRIVILEGED");
后來又google到了這段代碼,經過測試,完全好使..
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");
CallingActivity.this.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);
CallingActivity.this.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);
CallingActivity.this.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");
CallingActivity.this.sendOrderedBroadcast(localIntent4,"android.permission.CALL_PRIVILEGED");
posted on 2012-06-07 17:20
小果子 閱讀(2012)
評論(2) 編輯 收藏 引用 所屬分類:
開源 、
Android & Ios