B4A Library AnswerCall Library

Hello everyone,

SoyEli wanted to answer a call automatically in this topic, so I created this library.

It works together with Erel's PhoneEvents PhoneStateChanged.

Note that you should respect the order of events of the sample in phonestateChanged and that it will only work in a service.

B4X:
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    Log("PhoneStateChanged, State = " & State & ", IncomingNumber = " & IncomingNumber)
    Log(Intent.ExtrasToString)
   AC.LetPhoneRing(3000)
   If AC.isRinging == True Then
      AC.AnswerPhone
   End If
   AC.enableSpeakerphone
End Sub


It may not work on all devices (if you can test it out?)
It worked for my Xperia PLAY.

Attachements include the library files and a sample

Tomas
 

Attachments

  • AnswerCall.zip
    9.5 KB · Views: 1,201
  • AnswerCall1.1.zip
    9.7 KB · Views: 1,752
Last edited:

Sgardy

Member
Licensed User
Longtime User
SOLVED!
Hi friends, I was getting crazy with the AnswerCall Library, it's was working fine but there were no ways to enable the speaker automatically; I tried with this first:

Sub PE2_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
ToastMessageShow(State & " " & IncomingNumber,True)
If State = "RINGING" Then
P.SetRingerMode(P.RINGER_SILENT)
If AC.isRinging == True Then
AC.AnswerPhone​
End If
AC.enableSpeakerphone
End If
End Sub

That does not works in my Samsung Galaxy S3 then I changed with:

Sub Process_Globals
Private OnCall = false As Boolean
Private rr As Reflector 'This is an alternative option​
End Sub



Sub PE2_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
ToastMessageShow(State & " " & IncomingNumber,True)
If State = "RINGING" Then
OnCall = True
P.SetRingerMode(P.RINGER_SILENT)
If AC.isRinging == True Then
AC.AnswerPhone​
End If​
End If
If State = "OFFHOOK" And OnCall Then
AC.enableSpeakerphone
' You can use reflector instead of AC method, maybe it works with AnswerPhone too, I'll try
' 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")
OnCall = False​
End If​
End Sub

In other words the speaker must be enabled after the effective answer.

Enjoy!
 

palmzac

Active Member
Licensed User
Longtime User
Hi XverhelstX ,

Thank you ! It's great library.

There is a problem that HTC device does not work. Do you have any solution ? Thank !


Palmzac

Hello everyone,

SoyEli wanted to answer a call automatically in this topic, so I created this library.

It works together with Erel's PhoneEvents PhoneStateChanged.

Note that you should respect the order of events of the sample in phonestateChanged and that it will only work in a service.

B4X:
Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    Log("PhoneStateChanged, State = " & State & ", IncomingNumber = " & IncomingNumber)
    Log(Intent.ExtrasToString)
   AC.LetPhoneRing(3000)
   If AC.isRinging == True Then
      AC.AnswerPhone
   End If
   AC.enableSpeakerphone
End Sub


It may not work on all devices (if you can test it out?)
It worked for my Xperia PLAY.

Attachements include the library files and a sample

Tomas
 

Mark Zraik

Member
Licensed User
Longtime User
I put this library to work on my phone due to an annoying telemarketer/proven scammer that was calling me. I added some other code that I found on this forum "KillCall" and together with an editable list in a plain text file, it really does the job of eliminating the problem.

I just use the HOME key to clear the app from the visible screen and keep the service running. Use the BACK key to kill the entire app and the service.

I changed the code in the main app and the service module.
Main code:
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: AnswerCall
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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)
    activity.AddMenuItem("Start Listener","mnuListen")
    Activity.AddMenuItem("Stop Listener","mnuStop")

    If FirstTime =  True Then
        If Not (File.Exists(File.DirDefaultExternal, "blacklist.txt")) Then
        File.Copy(File.DirAssets, "blacklist.txt", File.DirDefaultExternal, "blacklist.txt")
        End If
    End If
 
    mnuListen_Click
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If (UserClosed = True) Then
    mnuStop_Click
    ExitApplication
    End If

End Sub

Sub mnuListen_Click
    StartService("Listener")
End Sub

Sub mnuStop_Click
    StopService("Listener")
End Sub
Service Module:
B4X:
#Region Module Attributes
    #StartAtBoot: true
#End Region

'Service module
Sub Process_Globals
    Dim PE As PhoneEvents 'gives you the phone events as they are happening - lib Phone ver. 2.25
    Dim AC As AnswerCall 'used for answering the call - lib AnswerCall ver. 1.00
    Dim PhoneId As PhoneId 'needed for PE Initialization
    Dim sNotif As Notification 'used for foreground services - required by android
'    Dim pnum As String 'used for writing number to file - uses lib randomaccessfile ver. 2.00
    Dim blklist As List 'used for checking numbers as they come in
'    Dim r As Reflector ' uses lib Reflection ver. 2.40
    Dim bnmatched As Boolean 'used for a match of a blacklisted/bad number
End Sub
Sub Service_Create
'Log("PID " & PhoneId)
    PE.InitializeWithPhoneState("PE",PhoneId)
    AC.Initialize("AnswerCall")
    'Log("Service created")
    sNotif.Initialize
    sNotif.Icon = "icon"
    sNotif.Vibrate = False
    sNotif.SetInfo("AnswerCall","Service Running",Main)
    sNotif.Sound = False
    sNotif.Notify(1)
    Service.StartForeground(1,sNotif)

End Sub


Sub Service_Start (StartingIntent As Intent)
    'Log("Service started.")
    ReadMeInfo
End Sub

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    'Log("PhoneStateChanged, State = " & State & ", IncomingNumber = " & IncomingNumber)
    'Log(Intent.ExtrasToString)
    bnmatched = False
    'Log("State1: " & State)
'    pnum = IncomingNumber
    'ToastMessageShow(IncomingNumber,False)
'    Dim bnmatched As Boolean

    'If AC.isRinging == True Then

    For Each badnum As String In blklist 
        If IncomingNumber = badnum Then
        bnmatched = True
        'Log("Number: " & badnum)
        ToastMessageShow("BlackListed Number: " & badnum,True)
        'Return
        End If
    Next

    If bnmatched = True Then
        AC.LetPhoneRing(1000)     
        'answer the call to stop the call from going to voicemail.
        AC.AnswerPhone
        DoEvents
        AC.enableSpeakerphone 'just for kicks
        DoEvents
    End If

        'Log("State2: " & State)
        'ToastMessageShow("Killing1: " & badnum,False)
    If (bnmatched = True) Then
            'Log("Killing2: " & badnum)
            Try
                Dim r As Reflector ' uses lib Reflection ver. 2.40
                r.Target = r.GetContext
                Dim TelephonyManager, TelephonyInterface As Object
                TelephonyManager = r.RunMethod2("getSystemService", "phone", "java.lang.String")
                r.Target = TelephonyManager
                TelephonyInterface = r.RunMethod("getITelephony")
                r.Target = TelephonyInterface
                r.RunMethod("endCall")
            Catch
            Log("Kill error: " & LastException)
            End Try
 
        'Log("Finished killing " & badnum)
    End If
    'End If
    'Log("State3: " & State)
End Sub
Sub Service_Destroy

End Sub

'Sub WriteMeInfo
'Dim TextWriter1 As TextWriter
'Log("Number " & pnum)
'            Try
'                If File.Exists(File.DirDefaultExternal, "blacklist.txt") Then
'                TextWriter1.Initialize(File.openOutput(File.DirDefaultExternal, "blacklist.txt", True))
'                TextWriter1.WriteLine(pnum)
'                TextWriter1.Close
'                Else
'                TextWriter1.Initialize(File.openOutput(File.DirDefaultExternal, "blacklist.txt", False))
'                TextWriter1.WriteLine(pnum)
'                TextWriter1.Close
'                End If
'            Catch
'            Log("Making new list: " &LastException.Message)
'            End Try
'End Sub

Sub ReadMeInfo
Dim rdr As TextReader

    '1. Read the file
    Try
        If File.Exists(File.DirDefaultExternal, "blacklist.txt") Then
            rdr.Initialize(File.openInput(File.DirDefaultExternal,"blacklist.txt"))
            blklist = rdr.ReadList
            rdr.Close
            Else
            ToastMessageShow("Black List File Not Found", True)
            DoEvents
            Return
        End If
    Catch
    Log("Getting List error: " &LastException.Message)
    End Try
    Log("Getting List Finished")
End Sub
'Sub KillCall
'       Dim r As Reflector ' uses lib Reflection ver. 2.40
'       r.Target = r.GetContext
'       Dim TelephonyManager, TelephonyInterface As Object
'       TelephonyManager = r.RunMethod2("getSystemService", "phone", "java.lang.String")
'       r.Target = TelephonyManager
'       TelephonyInterface = r.RunMethod("getITelephony")
'       r.Target = TelephonyInterface
'       r.RunMethod("endCall")
'
'End Sub

There is also some commented out code that I plan on adding for the future, maybe.

The manifest file looks like the following:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.basic4ppc.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddPermission(android.permission.READ_PHONE_STATE)
AddPermission(android.permission.CALL_PHONE)
AddPermission(android.permission.MODIFY_AUDIO_SETTINGS)
'End of default text.

Android Gingerbread 2.3.5 rooted, custom rom, sim unlocked on a Samsung i927 Glide.

If an incoming number matches a number in the text file of numbers (Manually edited), then it answers the phone, then hangs up the phone (Preventing my voicemail from being clogged with nonsense).

I must thank all of those who have unknowingly contributed to this hack I put together. Thank You All!

This is a great library, it just needed the hangup step, which i found else where on this forum.

I hope this helps someone out besides me. Enjoy the hack...
NOTE: If you add or remove a number from the blacklist, stop and restart the service and it will re-read the file. I just use a basic text editor to do this (10 digit numbers, I don't include the "1")

Changed the service module to play the 3 tones to indicate a number that is no longer in service, I have the same on my voicemail at the beginning of the greeting.

So here is the new service module code:
B4X:
#Region Module Attributes
    #StartAtBoot: true
#End Region

'Service module
Sub Process_Globals
    Dim PE As PhoneEvents 'gives you the phone events as they are happening - lib Phone ver. 2.25
    Dim AC As AnswerCall 'used for answering the call - lib AnswerCall ver. 1.00
    Dim PhoneId As PhoneId 'needed for PE Initialization
    Dim sNotif As Notification 'used for foreground services - required by android
'    Dim pnum As String 'used for writing number to file - uses lib randomaccessfile ver. 2.00
    Dim blklist As List 'used for checking numbers as they come in
'    Dim r As Reflector ' uses lib Reflection ver. 2.40
    Dim bnmatched As Boolean 'used for a match of a blacklisted/bad number
    Dim tones As MediaPlayer
End Sub
Sub Service_Create
'Log("PID " & PhoneId)
    PE.InitializeWithPhoneState("PE",PhoneId)
    AC.Initialize("AnswerCall")
    'Log("Service created")
    sNotif.Initialize
    sNotif.Icon = "icon"
    sNotif.Vibrate = False
    sNotif.SetInfo("AnswerCall","Service Running",Main)
    sNotif.Sound = False
    sNotif.Notify(1)
    Service.StartForeground(1,sNotif)

End Sub


Sub Service_Start (StartingIntent As Intent)
    'Log("Service started.")
    ReadMeInfo
'    tones.Initialize2("killIt")
End Sub

Sub PE_PhoneStateChanged (State As String, IncomingNumber As String, Intent As Intent)
    'Log("PhoneStateChanged, State = " & State & ", IncomingNumber = " & IncomingNumber)
    'Log(Intent.ExtrasToString)
    bnmatched = False
    'Log("State1: " & State)
'    pnum = IncomingNumber
    'ToastMessageShow(IncomingNumber,False)
'    Dim bnmatched As Boolean

    'If AC.isRinging == True Then

    For Each badnum As String In blklist    
        If IncomingNumber = badnum Then
        bnmatched = True
        'Log("Number: " & badnum)
        'ToastMessageShow("BlackListed Number: " & badnum,True)
        'Return
        End If
    Next

    If bnmatched = True Then
        AC.LetPhoneRing(1000)        
        'answer the call to stop the call from going to voicemail.
        AC.AnswerPhone
        DoEvents
        AC.enableSpeakerphone 'just for kicks
        DoEvents
    End If

        'Log("State2: " & State)
        'ToastMessageShow("Killing1: " & badnum,False)
    If (bnmatched = True) Then
            'Log("Killing2: " & badnum)
'            Try
'                Dim r As Reflector ' uses lib Reflection ver. 2.40
'                r.Target = r.GetContext
'                Dim TelephonyManager, TelephonyInterface As Object
'                TelephonyManager = r.RunMethod2("getSystemService", "phone", "java.lang.String")
'                r.Target = TelephonyManager
'                TelephonyInterface = r.RunMethod("getITelephony")
'                r.Target = TelephonyInterface
'                r.RunMethod("endCall")
'            Catch
'            Log("Kill error: " & LastException)
'            End Try
    
        'Log("Finished killing " & badnum)
        playIt
    End If
    'End If
    'Log("State3: " & State)
End Sub
Sub Service_Destroy

End Sub

'Sub WriteMeInfo
'Dim TextWriter1 As TextWriter
'Log("Number " & pnum)
'            Try
'                If File.Exists(File.DirDefaultExternal, "blacklist.txt") Then
'                TextWriter1.Initialize(File.openOutput(File.DirDefaultExternal, "blacklist.txt", True))
'                TextWriter1.WriteLine(pnum)
'                TextWriter1.Close
'                Else
'                TextWriter1.Initialize(File.openOutput(File.DirDefaultExternal, "blacklist.txt", False))
'                TextWriter1.WriteLine(pnum)
'                TextWriter1.Close
'                End If
'            Catch
'            Log("Making new list: " &LastException.Message)
'            End Try
'End Sub

Sub ReadMeInfo
Dim rdr As TextReader

    '1. Read the file
    Try
        If File.Exists(File.DirDefaultExternal, "blacklist.txt") Then
            rdr.Initialize(File.openInput(File.DirDefaultExternal,"blacklist.txt"))
            blklist = rdr.ReadList
            rdr.Close
            Else
            ToastMessageShow("Black List File Not Found", True)
            DoEvents
            Return
        End If
    Catch
    Log("Getting List error: " &LastException.Message)
    End Try
    Log("Getting List Finished")
End Sub

Sub playIt
Log("Starting Tones")
Try
tones.Initialize'2("killIt")
If File.Exists(File.DirDefaultExternal, "tone.mp3") Then
    tones.Load(File.DirDefaultExternal, "tone.mp3")
    tones.Looping = False
    tones.Play

    End If
'ToastMessageShow("Playing tones", False)
'

    DoEvents
    DoEvents
    DoEvents
    Log("Stopping Tones")
'
Catch
Log("Player error: " &LastException.Message)
End Try


Do While tones.IsPlaying
'Log("Playing now")
Loop
killIt
tones.Stop
tones.Release
End Sub
Sub killIt

Log("Killing call")
            Try
                Dim r As Reflector ' uses lib Reflection ver. 2.40
                r.Target = r.GetContext
                Dim TelephonyManager, TelephonyInterface As Object
                TelephonyManager = r.RunMethod2("getSystemService", "phone", "java.lang.String")
                r.Target = TelephonyManager
                TelephonyInterface = r.RunMethod("getITelephony")
                r.Target = TelephonyInterface
                r.RunMethod("endCall")
            Catch
            Log("Kill error: " & LastException)
            End Try
        
End Sub
'Sub KillCall
'       Dim r As Reflector ' uses lib Reflection ver. 2.40
'       r.Target = r.GetContext
'       Dim TelephonyManager, TelephonyInterface As Object
'       TelephonyManager = r.RunMethod2("getSystemService", "phone", "java.lang.String")
'       r.Target = TelephonyManager
'       TelephonyInterface = r.RunMethod("getITelephony")
'       r.Target = TelephonyInterface
'       r.RunMethod("endCall")
'
'End Sub
 

Attachments

  • tone.zip
    11 KB · Views: 193
Last edited:
Top