网上找到的,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
小果子 阅读(1993)
评论(2) 编辑 收藏 引用 所属分类:
开源 、
Android & Ios