在Oreo / Nougat中在拨出呼叫上启用扬声器时出错

问题描述 投票:3回答:1

我需要在拨出电话期间打开扬声器,但它的抛出异常下面是我使用过的代码片段:

audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                    audioManager.setMode(AudioManager.MODE_IN_CALL);
                    if (!audioManager.isSpeakerphoneOn())
                        audioManager.setSpeakerphoneOn(true);
                    audioManager.setMode(AudioManager.MODE_NORMAL);

它适用于6.0以下的设备,但不适用于8.0和7.0。

logcat的:

01-22 13:38:19.887 25014-25014/com.tool.autoredialer D/AudioManager: PPD setMode mode = 2
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: dalvik.system.VMStack.getThreadStackTrace(Native Method)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: java.lang.Thread.getStackTrace(Thread.java:1536)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.media.AudioManager.setMode(AudioManager.java:1630)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: com.tool.autoredialer.services.CallVibratorAccessibilityService.handlVibrate(CallVibratorAccessibilityService.java:114)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: com.tool.autoredialer.services.CallVibratorAccessibilityService.onAccessibilityEvent(CallVibratorAccessibilityService.java:49)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.accessibilityservice.AccessibilityService$2.onAccessibilityEvent(AccessibilityService.java:1527)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage(AccessibilityService.java:1712)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.os.Handler.dispatchMessage(Handler.java:105)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.os.Looper.loop(Looper.java:164)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.app.ActivityThread.main(ActivityThread.java:6809)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: java.lang.reflect.Method.invoke(Native Method)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
01-22 13:38:19.888 25014-25014/com.tool.autoredialer V/AudioManager: Elem: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
01-22 13:38:19.892 25014-25014/com.tool.autoredialer I/AudioManager: setSpeakerphoneOn true java.lang.Throwable
                                                                         at android.media.AudioManager.setSpeakerphoneOn(AudioManager.java:1235)
                                                                         at com.tool.autoredialer.services.CallVibratorAccessibilityService.handlVibrate(CallVibratorAccessibilityService.java:116)
                                                                         at com.tool.autoredialer.services.CallVibratorAccessibilityService.onAccessibilityEvent(CallVibratorAccessibilityService.java:49)
                                                                         at android.accessibilityservice.AccessibilityService$2.onAccessibilityEvent(AccessibilityService.java:1527)
                                                                         at android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage(AccessibilityService.java:1712)
                                                                         at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                         at android.os.Looper.loop(Looper.java:164)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6809)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer D/AudioManager: com.tool.autoredialerdo not have using speaker authority in call
01-22 13:38:19.894 25014-25014/com.tool.autoredialer D/AudioManager: PPD setMode mode = 0
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: dalvik.system.VMStack.getThreadStackTrace(Native Method)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: java.lang.Thread.getStackTrace(Thread.java:1536)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.media.AudioManager.setMode(AudioManager.java:1630)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: com.tool.autoredialer.services.CallVibratorAccessibilityService.handlVibrate(CallVibratorAccessibilityService.java:117)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: com.tool.autoredialer.services.CallVibratorAccessibilityService.onAccessibilityEvent(CallVibratorAccessibilityService.java:49)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.accessibilityservice.AccessibilityService$2.onAccessibilityEvent(AccessibilityService.java:1527)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.accessibilityservice.AccessibilityService$IAccessibilityServiceClientWrapper.executeMessage(AccessibilityService.java:1712)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.os.Handler.dispatchMessage(Handler.java:105)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.os.Looper.loop(Looper.java:164)
01-22 13:38:19.894 25014-25014/com.tool.autoredialer V/AudioManager: Elem: android.app.ActivityThread.main(ActivityThread.java:6809)
01-22 13:38:19.895 25014-25014/com.tool.autoredialer V/AudioManager: Elem: java.lang.reflect.Method.invoke(Native Method)
01-22 13:38:19.895 25014-25014/com.tool.autoredialer V/AudioManager: Elem: com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
01-22 13:38:19.895 25014-25014/com.tool.autoredialer V/AudioManager: Elem: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
android android-audiomanager android-8.0-oreo android-7.1-nougat
1个回答
1
投票

找到了答案。

您正在尝试在通话期间使用AudioManager,Android系统禁止使用它。在扬声器上实现设置的方法是替换Phone行为并实现InCallService。

然后你可以打电话给setAudioRoute(CallAudioState.ROUTE_SPEAKER),并能够处理不同的设备(如蓝牙或有线耳机)。

您可以在此处查看API定义:https://developer.android.com/reference/android/telecom/InCallService#setAudioRoute(int)

© www.soinside.com 2019 - 2024. All rights reserved.