1. android 2.3以下版本(不包括2.3)
http://bbs.51cto.com/viewthread.php?tid=1078059&extra=&page=1
中的“二 android低版本自动接听/挂断实现”

核心代码:

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.3版本以下的手机,没有测试。

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");    }

3. android 4.1, 4.2 版本

更高版本的没有相关机器测试。

从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)


更多相关文章

  1. 多个按钮左右相互挤压效果
  2. android历史版本
  3. Android后台保活,这里有你需要的所有姿势。2019,基于API26 Android
  4. Android Material Design按钮样式设计
  5. 利用productFlavors实现一套代码打包多版本
  6. Android 中英文切换(点击按钮切换语言)
  7. Android 获取系统信息(手机品牌、型号、安卓版本号等等)
  8. Android获取系统内核版本的方法

随机推荐

  1. 使用Android NDK中的独立toolchain来开发
  2. Android 网络请求详解
  3. COCOS2D-X跨ANDROID&IOS平台开发入门教程
  4. Android计算优化解析
  5. Android课堂笔记(九)——消息条、对话框、
  6. Handler机制分析
  7. Android性能优化典范
  8. android 工程里缺少 R.java 文件原因和解
  9. Android(安卓)logcat 使用
  10. Android深入浅出之Surface[1]