How to detect Phone State..

abner69

Member
Licensed User
Longtime User
I am looking for a way to be able to tell if:

1. a device is a phone
2. if it is a phone, whether or not it has service through its provider

Using the phone library, I was able to check for the type of service, the data state, and whether or not the phone is provisioned.

To my amazement, all of my devices, including the tablets, reported as having either CDMA or GSM service...

All show that the Phones (y tablets) are provisioned for service.

finally all show that Data Service is disconnected.

So, after several hours of looking I found the PhoneStateListener Library...

Using the PSL library, I can now tell that my tablets do not have service available.

However, All phones (including two without service activation) are showing as IN SERVICE.

After reading the info on the PhoneStateListener on the Android Developer page (Located Here), I can not figure out why these inactivated phones are showing IN SERVICE. I would think that they would at least show a state of EMERGENCY ONLY or POWER OFF.

it is critical to be able to detect this due to the fact that I use this information to decide whether to send SMS Messages through the native phone or through the web.

Any thoughts or help would be greatly appreciated!

...Pablo
 

abner69

Member
Licensed User
Longtime User
Hi Erel, thanks for the quick reply!

Unfortunately, I am using the Multi-part SMS send using the reflector lib, and to the best of my knowledge, the phone lib doesnt actually fire an event that the msg has been sent...

because of the message size (norm 2-3 txts total) I need to keep the multipart send, if at all possible.


B4X:
If Ind.CheckIfPhone=True AND Ind.CheckIfPhoneService=True Then
   Dim MsgSender As Reflector
    MsgSender.Target = MsgSender.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
    Dim parts As Object
    parts = MsgSender.RunMethod2("divideMessage", tempString, "java.lang.String")
    MsgSender.RunMethod4("sendMultipartTextMessage", _
        Array As Object(tSendNum, Null, parts, Null, Null), _
        Array As String("java.lang.String", "java.lang.String", _
            "java.util.ArrayList", "java.util.ArrayList", "java.util.ArrayList"))
end if


any ideas on a work around for this?

...Pablo
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this code:
B4X:
Dim MsgSender As Reflector
MsgSender.Target = MsgSender.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
Dim parts As Object
parts = MsgSender.RunMethod2("divideMessage", tempString, "java.lang.String")
Dim i1 As Intent
i1.Initialize("b4a.smsdelivered")

Dim pi As Reflector
pi.Target = pi.RunStaticMethod("android.app.PendingIntent", "getBroadcast", _
   Array As Object(pi.GetContext, 0, i1, 134217728), _
   Array As String("android.content.Context", "java.lang.int", "android.content.Context", "java.lang.int"))

Dim list1 As List
list1.Initialize
list1.Add(pi.Target)

MsgSender.RunMethod4("sendMultipartTextMessage", _
    Array As Object(tSendNum, Null, parts, Null, list1), _
    Array As String("java.lang.String", "java.lang.String", _
        "java.util.ArrayList", "java.util.ArrayList", "java.util.ArrayList"))
It should raise the SmsDelivered when the first part is delivered.
 
Upvote 0

abner69

Member
Licensed User
Longtime User
Erel,

After adding the code to the service, i got a compiler error for:
B4X:
i1.Initialize("b4a.smsdelivered")
so, i replaced it with:
B4X:
i1.Initialize("b4a.smsdelivered","")

it compiled without a hitch, however upon run, when reaching the
B4X:
pi.Target = pi.RunStaticMethod("android.app.PendingIntent", "getBroadcast", _
    Array As Object(pi.GetContext, 0, i1, 134217728), _
    Array As String("android.content.Context", "java.lang.int", "android.content.Context", "java.lang.int"))

I am getting the following error in my logs:
B4X:
Loaded 0 Personal Note Flag Records.
** Activity (reader_b) Resume **
CheckIfPhone Reports:CDMA
CheckifPhone=true
Phone Type: CDMA
Data State: DISCONNECTED
Provisioning Status: 1
Service Reports: IN SERVICE
Have Service=true
Sending SMS via SVC...
Got the parts of the Message
Created intent for tracking
ebsystem_sendsmsmessage (java line: 341)
java.lang.NoSuchMethodException: getBroadcast
   at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
   at java.lang.Class.getDeclaredMethod(Class.java:731)
   at anywheresoftware.b4a.agraham.reflection.Reflection.RunStaticMethod(Reflection.java:900)
   at biblia.electronica.ebsystem._sendsmsmessage(ebsystem.java:341)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:165)
   at anywheresoftware.b4a.keywords.Common$4.run(Common.java:884)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at anywheresoftware.b4a.Msgbox.waitForMessage(Msgbox.java:211)
   at anywheresoftware.b4a.Msgbox.waitForMessage(Msgbox.java:180)
   at anywheresoftware.b4a.keywords.Common.DoEvents(Common.java:370)
   at biblia.electronica.reader_b._btnmailsend_click(reader_b.java:1544)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:165)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:153)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:149)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:55)
   at android.view.View.performClick(View.java:2485)
   at android.view.View$PerformClick.run(View.java:9080)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3694)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
   at dalvik.system.NativeStart.main(Native Method)
** Activity (reader_b) Pause, UserClosed = false **

Here is my complete code:
B4X:
Sub SendSMSMessage()As Boolean 
Log("Sending SMS via SVC...")
    Dim SmsManager As PhoneSms, parts As Object
    'Try
        If SMSMessage.Length <= 160 Then 
            SmsManager.Send(SMSPhoneNumber, SMSMessage)
         Log("Simple Single SMS Sent")
        Else
Dim MsgSender As Reflector
MsgSender.Target = MsgSender.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
parts = MsgSender.RunMethod2("divideMessage", SMSMessage, "java.lang.String")
Log("Got the parts of the Message")
Dim i1 As Intent
i1.Initialize("b4a.smsdelivered","")
Log("Created intent for tracking")
Dim pi As Reflector
pi.Target = pi.RunStaticMethod("android.app.PendingIntent", "getBroadcast", _
    Array As Object(pi.GetContext, 0, i1, 134217728), _
    Array As String("android.content.Context", "java.lang.int", "android.content.Context", "java.lang.int"))
Log("Ran the impending Intent")

Dim list1 As List
list1.Initialize
Log(pi.target)
list1.Add("pi target=" & pi.Target)

Log("Sending Rest of Message")
MsgSender.RunMethod4("sendMultipartTextMessage", _
    Array As Object(SMSPhoneNumber, Null, parts, Null, list1), _
    Array As String("java.lang.String", "java.lang.String", _
        "java.util.ArrayList", "java.util.ArrayList", "java.util.ArrayList"))
      
        End If
      Log("message sent")
        Return True
    'Catch
      Log("message failed.")
        Return False
    'End Try
End Sub

am i missing something here??

...Pablo
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code works:
B4X:
Log("Sending SMS via SVC...")
Dim SmsManager As PhoneSms, parts As Object
Dim MsgSender As Reflector
MsgSender.Target = MsgSender.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
Dim m As String
For i = 1 To 100
   m = m & " test"
Next
parts = MsgSender.RunMethod2("divideMessage",m, "java.lang.String")
Log(parts)
Log("Got the parts of the Message")
Dim i1 As Intent
i1.Initialize("b4a.smsdelivered","")
Log("Created intent for tracking")
Dim pi As Reflector
pi.Target = pi.RunStaticMethod("android.app.PendingIntent", "getBroadcast", _
    Array As Object(pi.GetContext, 0, i1, 134217728), _
    Array As String("android.content.Context", "java.lang.int", "android.content.Intent", "java.lang.int"))
Log("Ran the impending Intent")

Dim list1 As List
list1.Initialize
list1.Add(pi.Target)
MsgSender.RunMethod4("sendMultipartTextMessage", _
    Array As Object("PHONE NUMBERS", Null, parts, Null, list1), _
    Array As String("java.lang.String", "java.lang.String", _
        "java.util.ArrayList", "java.util.ArrayList", "java.util.ArrayList")
 
Upvote 0

abner69

Member
Licensed User
Longtime User
thank you very much for the help Erel...

This works perfectly with a phone that is provisioned, and has actual service activated.

With these CDMA phones (1 provisioned for Verizon, 1 for StraightTalk) that have no actual service activation, the routine still sends the sms msg "Successfully." A look at the unfiltered logs shows both phones switching frequencies to prohibit connections.

I guess the quick way of fixing it is have the user set a flag to send via Wi-Fi only since they know whether or not they have paid for their service :D

Again, thanks for everything! :sign0188:

...Pablo
 
Upvote 0
Top