Android 自动接听电话
16lz
2021-01-23
1. android 2.3以下版本(不包括2.3)
http://bbs.51cto.com/viewthread.php?tid=1078059&extra=&page=1
中的“二 android低版本自动接听/挂断实现”
或
没有2.3版本以下的手机,没有测试。
3. android 4.1, 4.2 版本
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)
更多相关文章
- 多个按钮左右相互挤压效果
- android历史版本
- Android后台保活,这里有你需要的所有姿势。2019,基于API26 Android
- Android Material Design按钮样式设计
- 利用productFlavors实现一套代码打包多版本
- Android 中英文切换(点击按钮切换语言)
- Android 获取系统信息(手机品牌、型号、安卓版本号等等)
- Android获取系统内核版本的方法