這個也是在別的地方看的,具體不記得了,自己只是做個備份,以免以後找不到。
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);
}
}
}