Android Question IncomingNumber in the background ?

tunderin

Member
Licensed User
Or you could do it with a intent filter - it has the advantage of starting your service for you when PhoneStateChanged changes.

I did it by adding to the manifest:

B4X:
AddPermission(android.permission.READ_PHONE_STATE)
AddReceiverText(yourServiceName,
<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>)

To the yourServiceName Service_Start sub, I added:

B4X:
    'call processing
    If StartingIntent.Action = "android.intent.action.PHONE_STATE" Then
        Select StartingIntent.GetExtra("state")
           
            'onhook - call ended or phone idle
            Case "IDLE"
           
            'outgoing call processing
            Case "OFFHOOK"
           
            'incoming call processing
            Case "RINGING"
                Log("CallerID="&StartingIntent.GetExtra("incoming_number"))
       
        End Select


Hope this helps...
 

tunderin

Member
Licensed User
Have a look at Margaret's Custom Toast class - it allows more control over the toastmessage display duration.

In my app, I'm using an activity to display data about the caller if they are in my db and trying to implement answer & reject buttons in the activity. There seems to be no reliable way to answer an incoming call from an app - Android security.
 

ilan

Expert
Licensed User
Or you could do it with a intent filter - it has the advantage of starting your service for you when PhoneStateChanged changes.

I did it by adding to the manifest:

B4X:
AddPermission(android.permission.READ_PHONE_STATE)
AddReceiverText(yourServiceName,
<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>)

To the yourServiceName Service_Start sub, I added:

B4X:
    'call processing
    If StartingIntent.Action = "android.intent.action.PHONE_STATE" Then
        Select StartingIntent.GetExtra("state")
         
            'onhook - call ended or phone idle
            Case "IDLE"
         
            'outgoing call processing
            Case "OFFHOOK"
         
            'incoming call processing
            Case "RINGING"
                Log("CallerID="&StartingIntent.GetExtra("incoming_number"))
     
        End Select


Hope this helps...
Thank you this very helpful.

Can i also detect if a call was accepted?

(So after ringing if the call is active)

Thanx :)
 

ilan

Expert
Licensed User
Add log messages. I expect OFFHOOK to appear if the call was answered.
thank you i will try it out, so i could put a variable (boolean) in "RINGING" and ask in "OFFHOOK" if variable x = true then ringing was before call went active and like this know if an incoming call was answered.
 

ilan

Expert
Licensed User
Use a Long variable instead and store the value of DateTime.Now when it is ringing. You can then see how many milliseconds have passed when OFFHOOK fired.
also a good idea, so if < 10.000 ms then = answer after incoming call ?? or maybe more then 10sec?
 
Top