Android Question Make Call with SPEAKER ON - Android 8

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends,

please, in Android 8, if I am using:

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

I get this error:
java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

Please, what does it mean?
 

Semen Matusovskiy

Well-Known Member
Licensed User
Did you add MODIFY_AUDIO_SETTINGS permission to manifest ?

Try this code (using JavaObject)

B4X:
    ' Add AddPermission (android.permission.MODIFY_AUDIO_SETTINGS) to manifest
    Dim jo As JavaObject
    jo.InitializeContext
    Dim am As JavaObject = jo.RunMethodJo ("getSystemService", Array ("audio"))
    Log (am.RunMethod ("isSpeakerphoneOn", Null))        ' Check state
    am.RunMethod ("setSpeakerphoneOn", Array (True))   ' Set True (if default is False)
    Log (am.RunMethod ("isSpeakerphoneOn", Null))        ' Check state again
 
Last edited:
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Semen,

thank you for your answer

In manifest I have.
B4X:
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)
And in code I add RP (run.permission).

If I use your code and phone state = is not in call, I get values:
TRUE
TRUE
(but I hear nothing - phone is not in call)


If phone state = is in call, I get values
FALSE
FALSE
It is not possible to set values = TRUE


Best regards,
p4ppc
 
Last edited:
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
SEMEN,

thank you very much for your example. It is still the same situation.

Only if phone is in state NOT IN CALL, I can set
B4X:
am.RunMethod ("setSpeakerphoneOn", Array (True))
I get TRUE

If the phone is in state "IN CALL", I can NOT set
B4X:
am.RunMethod ("setSpeakerphoneOn", Array (True))
I get only FALSE

RESULT:
It is not possible to SET
B4X:
am.RunMethod ("setSpeakerphoneOn", Array (True))
if the phone is IN CALL state

p4ppc
 
Last edited:
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I am not surprise that you can't change in call state.
Remember a camera. Many running apps are able to make a photo, but each app must release a camera interface in Activity_Pause. If any app will not release a interface, another apps will not be able to make a photo.
I am not a specialist in "telephone" interface. But it's possible to expect that in call a phone inforface is busy by system phone app.
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
SEMEN,

thank you for your answer. Before Android 8 it was possible.
Do somebody know, how to release phone interface, please? How to enable speakerphone?

Edit-if I set speaker ON=true before making call,then in thé state IN CALL it goes to FALSE automaticaly .
Thank you very much,
p4ppc
 
Last edited:
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
how to release phone interface

What is a purpose ?
If you need to control outgoing calls, then a phone should be free anyway.
If you want, for example, to record incoming calls, your app should pick up the phone. Guess, in this case "interface" belongs to your app and you are able to do "everything".
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Semen,

Thank You for answer-In the beginning I want only turn Speaker ON automaticaly when I receive call.

Edit: Do you think, please, that it is no possible to turn speaker ON? I want turn speakers ON automaticaly - I wish to hear call from speakers.

Best regards
p4ppc
 
Last edited:
Upvote 0

ronens

Member
Licensed User
hello
i tried to run the solution ( i red other posts)
my app is making a call .
when i run on debug and add break point it work. but when i run it in full mode the speaker not open.

Dim ctxt As JavaObject
ctxt.InitializeContext
Dim audioManager As JavaObject = ctxt.RunMethodJO("getSystemService", Array(ctxt.GetField("AUDIO_SERVICE")))
Dim audioManagerStatic As JavaObject
audioManagerStatic.InitializeStatic("android.media.AudioManager")

Dim originalMode As Int = audioManager.RunMethod("getMode",Null)

If value Then
audioManager.RunMethod("setMode", Array(audioManagerStatic.GetField("MODE_IN_CALL")))
Else
audioManager.RunMethod("setMode", Array(audioManagerStatic.GetField("MODE_NORMAL")))
End If

audioManager.RunMethod("setSpeakerphoneOn", Array(value))
' // restore original mode
audioManager.RunMethod("setMode", Array(originalMode))

Dim pc As PhoneCalls
StartActivity(pc.Call("01211111"))
 
Upvote 0
Top