Android Question Turn loudspeaker on android 4.4 KitKat and above

hookshy

Well-Known Member
Licensed User
Longtime User
Can you turn the loudspeaker on this adroid version ?

For previous versions the code below works perfect :
I use it together with phone events to rise the following code .

B4X:
        Dim rr As Reflector
        rr.Target = rr.GetContext
        rr.Target = rr.RunMethod2("getSystemService", "audio", "java.lang.String")            
        rr.RunMethod2("setMode", "2", "java.lang.int")
        rr.RunMethod2("setSpeakerphoneOn", True, "java.lang.boolean")

Thank you .
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
This worked for me on lollipop http://stackoverflow.com/questions/...ough-the-speakers-when-headphones-are-plugged

B4X:
Dim java As JavaObject

    java.InitializeContext
    java.RunMethod("setSpeakerOn",Null)

B4X:
#If JAVA
import android.media.AudioManager;
public void setSpeakerOn() {
   
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    audioManager.setMode(AudioManager.MODE_IN_CALL);
    audioManager.setSpeakerphoneOn(true);
    }
   

#End if

And add the permission in manifest
B4X:
AddPermission("android.permission.MODIFY_AUDIO_SETTINGS")
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm getting an error using that code

B4X:
#If JAVA
    import android.media.AudioManager;
    public void setSpeakerOn(boolean Value) {
       AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
       audioManager.setMode(AudioManager.MODE_IN_CALL);
       audioManager.setSpeakerphoneOn(Value);
    }
#End if

I changed it to take the value as a parameter

It says:
error: cannot find symbol
symbol: variable AUDIO_SERVICE
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I got it "working" by moving the code to the activity instead of the service

However it still doesn't actually activate the speakerphone on Android 5
 
Upvote 0
Top