Android 10-音频设置权限拒绝:setSpeakerphoneOn() [英] Android 10 - Audio Settings Permission Denial: setSpeakerphoneOn()
问题描述
我在Android 10上运行,无法使用Kotlin打开扬声器电话.下面是我的代码,在调用过程中始终显示False.
I'm running on Android 10 and cannot turn on speaker phone using Kotlin. Below is my code and it will always show False during incall.
MainActivity.kt
MainActivity.kt
val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager.isSpeakerphoneOn = true
if (audioManager.isSpeakerphoneOn)
Toast.makeText(this, "True", Toast.LENGTH_SHORT).show()
else
Toast.makeText(this, "False", Toast.LENGTH_SHORT).show()
AndroidManifest.xml
AndroidManifest.xml
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Logcat
04-18 10:43:58.064 1313 3118 W AS.AudioService: Audio Settings Permission Denial: setSpeakerphoneOn() from pid=8073, uid=10213
推荐答案
我能够使用InCallService解决此问题,我正在使用它来将应用程序开发为默认拨号程序.您所要做的就是获取扩展InCallService的CallService类的实例.由于在Android 10上您不能直接使用setSpeakerphoneOn(),因此在Android 9上效果很好.InCallService具有setAudioRoute()方法,可用于路由音频.到目前为止,我已经在Android 10和9上测试了以下代码.还测试了在通话过程中扬声器和听筒之间交替显示的Wouter Vanhauwaert问题,该方法也很好用.我的代码如下.
I was able to resolve the issue using InCallService which I am using to develop an app as Default Dialer. All you have to do is get the instance of the CallService class which extends InCallService. Since on Android 10 you cannot directly use setSpeakerphoneOn(), which works great on Android 9. InCallService have a method setAudioRoute() which you can use to route your audio. So far I have tested the below code on Android 10 and 9. Also tested Wouter Vanhauwaert issue of alternating between speaker and earpiece during a call which also works fine. My Code is as below.
CallService.java
public class CallService extends InCallService {
private static CallService sInstance;
……
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
}
public static CallService getInstance(){
return sInstance;
}
}
AnotherActivity.java
public void toggleSpeaker() {
AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
boolean isSpeakerOn = audioManager.isSpeakerphoneOn();
int earpiece = CallAudioState.ROUTE_WIRED_OR_EARPIECE;
int speaker = CallAudioState.ROUTE_SPEAKER;
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.P){
CallService.getInstance().setAudioRoute(isSpeakerOn ? earpiece : speaker);
} else {
am.setSpeakerphoneOn(!isSpeakerOn);
}
}
这篇关于Android 10-音频设置权限拒绝:setSpeakerphoneOn()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!