Query for Phone State

MrKim

Well-Known Member
Licensed User
Longtime User
Is there a way to query for the phone state.
Java has: getCallState()
 

MrKim

Well-Known Member
Licensed User
Longtime User
Thanks for the response.

I know, and I am doing that for other things but I have a service that starts periodically based on certain results of that. Android appears to have a getCallState() function that returns the current status of the phone and that seems to me to be the cleaner way to do what I need to do. I just need the call state at a moment in time when my service runs. I hate to listen constantly and turn a variable on and off depending on whether the phone is OFF_HOOK or not.

Perhaps it is time for me to read the tutorial and write a lib. Really like using B4A.

Kim
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
Perhaps it is time for me to read the tutorial and write a lib

You don't need to write a lib for this purpose. Just write a B4A function with the help of the Reflection Library:

B4X:
'returns the current device call state
'0 = Device call state: No activity.
'1 = Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.
'2 = Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
Sub GetCallState() As Int
    Dim r As Reflector
   Dim TelephonyManager As Object
   r.Target = r.GetContext
    TelephonyManager = r.RunMethod2("getSystemService", "phone", "java.lang.String")
    r.Target = TelephonyManager
   Return r.RunMethod( "getCallState") 
End Sub
 
Last edited:
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Well, this is where my newbeeness comes in to play. When I click on the referenced libraries tab there is no Reflections library. I am aware that there seem to be other libs available but am leary as I don't know if they are tested/reliable, nor have I run across any info on exactly how to find/download/install them. I also wonder how much they will increase the size of my app. when you add a lib to a project does the entire lib get installed or only those functions that are used. All questions for another topic.

Kim
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
Well, this is where my newbeeness comes in to play. When I click on the referenced libraries tab there is no Reflections library.
I am aware that there seem to be other libs available but am leary as I don't know if they are tested/reliable, nor have I run across any info on exactly how to find/download/install them. I also wonder how much they will increase the size of my app.

You can find a list of all available libraries here:

Libraries - Basic4android Wiki

More about Agrahams Reflection library here:

Reflection Library

Reflection is very reliable and one of the most useful libraries in my opinion.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Djembefola

Djembefola,

Thanks for your help. I downloaded it and will give it a try. The only other question, the text file said to put the jar and xml files in my additional libraries folder. I didn't have one so I put it with all of the others and it showed up OK. I would like to keep the add-ons seperate, where do I put the "additional libraries folder and exactly what is its name?

Thanks again
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
It Works .. but

In case anyone else trys this excelent code you have 1 and 2 reversed:
'returns the current device call state
'0 = Device call state: No activity.
'1 = Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.
'2 = Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
Djembefola

Is it possible to use this for Bluetooth connection ?
Thank you:


'returns the current device call state
'0 = Device call state: No activity.
'1 = Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.
'2 = Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
Sub GetCallState() As Int
Dim r As Reflector
Dim TelephonyManager As Object
r.Target = r.GetContext
TelephonyManager = r.RunMethod2("getSystemService", "phone", "java.lang.String")
r.Target = TelephonyManager
Return r.RunMethod( "getCallState")
End Sub
 
Upvote 0

Geezer

Active Member
Licensed User
Longtime User
Djembefola,

I would like to keep the add-ons seperate, where do I put the "additional libraries folder and exactly what is its name?

Thanks again

The folder name and location can be anything you like. Just add the information in tools/configure paths.
 
Upvote 0
Top