问题描述
- 贴一段代码,关于自动接听的,请老司机帮帮忙
-
public synchronized void answerRingingCall(Context context) { try { Log.e("try to answer", "below 2.3"); //ITelephony itelephony = getITelephony(mTelephonyManager); Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class); IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE}); ITelephony itelephony = ITelephony.Stub.asInterface(binder); //itelephony.silenceRinger(); itelephony.answerRingingCall(); } catch (Exception e1) { Log.e("try to answer","2.3~4.1",e1); try { 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"); context.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); context.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); context.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"); context.sendOrderedBroadcast(localIntent4,"android.permission.CALL_PRIVILEGED"); } catch (Exception e2) { try{ Log.e("try to answer", "for 4.1 and above way1"); Intent intent = new Intent("android.intent.action.MEDIA_BUTTON"); KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK); intent.putExtra("android.intent.extra.KEY_EVENT",keyEvent); context.sendOrderedBroadcast(intent,"android.permission.CALL_PRIVILEGED"); } catch (Exception e3) { Log.d("try to answer", "for 4.1 and above way2", e3); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK); mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT,keyEvent); context.sendOrderedBroadcast(mediaButtonIntent, null); } } } }
最近在做一个关于通话的app,网上收集了些资料,但都比较老,整理出了这段代码,跑在模拟器上,用终端模拟电话呼入,模拟器只有5.0(API 21)的版本
结果代码跑到了"for 4.1 and above way1"这段里,也没有报错,但却无法接起电话。不知道是模拟器的原因,还是代码的问题?手边没有android的手机所以只能在模拟器折腾,请老司机帮帮忙~
时间: 2024-11-02 08:12:25