Android Question IncomingNumber in the background ?

sigster

Active Member
Licensed User
Longtime User
Hi

How can I use PhoneStateChanged in the background
it work if the form is open

Regards
Sigster
 

Attachments

  • caller ID.zip
    8.2 KB · Views: 592

tunderin

Member
Licensed User
Longtime 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...
 
Upvote 0

tunderin

Member
Licensed User
Longtime 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.
 
Upvote 0

ilan

Expert
Licensed User
Longtime 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 :)
 
Upvote 0

ilan

Expert
Licensed User
Longtime 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.
 
Upvote 0

ilan

Expert
Licensed User
Longtime 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?
 
Upvote 0
Top