Android Question Call and set loudspeaker on

slowtime

Active Member
Licensed User
Longtime User
Hi,
can someone help me ?

The code initialize a call and set on the loudspeaker.

This code works on android 2.3.3. in main activity but doesn't work in a service.
The same code doesn't work in android 4.1.2 .

Dim pc As PhoneCalls
StartActivity(pc.Call(cellnumber))

'open speakerphone
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")

Is there a way to run the code in a service and in all android os ?

Thank you in advance.

Ciao

p.s. sorry for my english
 

bsnqt

Active Member
Licensed User
Longtime User
What error does the code give you? Or it runs without error but does not switch on the loudspeaker?
 
Upvote 0

slowtime

Active Member
Licensed User
Longtime User
Hi
It runs without error but doesn't switch the speaker in 4.1.2
In 2.3.3 run only in main activity not in service module
Ciao
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Sorry I don't have time enough to dig into your issue...
If you have time you may want to search around for at least a solution, even if it is in Java
Then we can have you to implement it using Reflector or building up a library..
But so far I tried but also get same thing (loud speaker is not activated...), I am using Xperia S Android 4.0.4
You need to investigate first (by searching internet) to see if your code works for other Android versions or any alternative solution
If I have time this week-end I will try to help you.
 
Upvote 0

slowtime

Active Member
Licensed User
Longtime User
Hi,
I solved my problem but I don't like this solution. Suggestions are wecome.

From Stackoverflow:

on android 4.1 PhoneUtils checkin speaker state before call and turn it off(logcat):

B4X:
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)

MAIN

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim flag As Boolean
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    flag=False
StartService("serv")
Dim pc As PhoneCalls
StartActivity(pc.Call(yourcellnumber))

End Sub

Sub Activity_Resume
Log (flag)

If flag=True Then
    StopService("serv")
    Activity.Finish
    ExitApplication 
End If
     
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

SERVICE (serv)

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
Dim Timer1 As Timer '
    Dim PE As PhoneEvents
End Sub
Sub Service_Create
Timer1.Initialize("Timer1",1000) ' 1 secondi

End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub
Sub Timer1_Tick
Timer1.Enabled=False
Main.flag=True

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") 

End Sub

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
 
    Log("PhoneStateChanged, State = " & State & ", IncomingNumber = " & IncomingNumber)
    If State == "OFFHOOK" Then
 
'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")
 
Timer1.Enabled=True
    End If 
End Sub

Ciao.
 
Upvote 0
Top