Android Question how can show unread sms in box?

khosrwb

Active Member
Licensed User
Longtime User
I want to show unread SMS in a box or listview.
I know its big ask.
 

eurojam

Well-Known Member
Licensed User
Longtime User
you can try this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim cr As ContentResolver
    cr.Initialize("")
    Dim u As Uri
    Dim pr() As String
    pr = Array As String ( "_id", "address", "date", "body" )
    u.Parse("content://sms/inbox")
    Dim crsr As Cursor = cr.Query( u, pr , "read = 0", Null, Null)
    Log("Rows " & crsr.RowCount)
    Log("Columns " & crsr.ColumnCount)
    For i=0 To crsr.RowCount-1
      crsr.Position = i
      Log(crsr.GetString("_id") & "-" & crsr.GetString("date"))
   
   Next
End Sub

this requires the READ_SMS permission: AddPermission(android.permission.READ_SMS) to the manifest editor
uses the SQL-Lib and ContentResolver-Lib

stefan
 
Last edited:
Upvote 0

khosrwb

Active Member
Licensed User
Longtime User
you can try this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim cr As ContentResolver
    cr.Initialize("")
    Dim u As Uri
    Dim pr() As String
    pr = Array As String ( "_id", "address", "date", "body" )
    u.Parse("content://sms/inbox")
    Dim crsr As Cursor = cr.Query( u, pr , "read = 0", Null, Null)
    Log("Rows " & crsr.RowCount)
    Log("Columns " & crsr.ColumnCount)
    For i=0 To crsr.RowCount-1
      crsr.Position = i
      Log(crsr.GetString("_id") & "-" & crsr.GetString("date"))
  
   Next
End Sub

this requires the READ_SMS permission: AddPermission(android.permission.READ_SMS) to the manifest editor
uses the SQL-Lib and ContentResolver-Lib

stefan

hi
I use them , but when I run app
no action occored:(
How should I use?
If possible please use one label or other objects to show results.
 
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
The example code I've posted does only write the results in the log. If you use the debug mode and set the log to filtered you will see what happens. Now you have to take my code snipplet and build your code around, for example fill a listview with it:
B4X:
Sub Globals
    Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim cr As ContentResolver
    cr.Initialize("")
    Dim u As Uri
    Dim pr() As String
    pr = Array As String ( "_id", "address", "date", "body" )
    u.Parse("content://sms/inbox")
    Dim crsr As Cursor = cr.Query( u, pr , "read = 0", Null, Null)
    Log("Rows " & crsr.RowCount)
    Log("Columns " & crsr.ColumnCount)
    ListView1.Initialize("ListView1")
    For i=0 To crsr.RowCount-1
       crsr.Position = i
       ListView1.AddSingleLine(crsr.GetString("_id") & "-" & crsr.GetString("date"))
     Next
    Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
  Activity.Title = Value
End Sub
 
Upvote 0

khosrwb

Active Member
Licensed User
Longtime User
very thanks


I used this code to count sms.
B4X:
Button5.Text=""&i

But , when run app and resive a new sms
Variable i is not change when a new sms (( when come on a new sms )).
What can be done to solve this problem?

excuse me for my bad english language
 
Last edited:
Upvote 0

khosrwb

Active Member
Licensed User
Longtime User
hi
I need to id sms , for show spesial sms in label , but when I use from this code :


B4X:
Sub Activity_Create(FirstTime As Boolean)
      ListView1.AddTwoLines(crsr.GetString("_id") ,crsr.GetString("body"))
      mysms=crsr.getstring("body")
      smsid=(crsr.getstring("_id"))
end sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
  label1.Text=mysms
End Sub


my app don't work truly , actually don't show sms truly:( (( my app show other sms ))
like:
I have 150 sms. but last id=161
I don't know what to do:(
 
Last edited:
Upvote 0

khosrwb

Active Member
Licensed User
Longtime User
how can I use them?
I used them but I can't use very good from them:(
I need to use under code from owne app

B4X:
sms_idkey=Sms.Id
threadid_dat=Sms.ThreadId
datelog_dat=DateUtils.TicksToString(Sms.Date)
readsms_dat=Sms.Read
bodysms_dat=Sms.Body
addresscp_dat=Sms.Address
person_dat=Sms.PersonId
smsType=Sms.Type
 
Upvote 0

khosrwb

Active Member
Licensed User
Longtime User
I can show sms but I will show sms by
B4X:
sms.id
I will use sms.id for show but I don't work with it ((work trully ))
my app don't work truly , actually don't show sms truly:( (( my app show other sms ))
like:
I have 150 sms. but last id=161 ((sms.id=161)):confused::confused:
I don't know what to do!?:(


actually I will load Special sms on label by click on Special listview item and I will to show sms time resive (( i use under code: ))
B4X:
smsTimeResive=DateUtils.TicksToString(Sms.Date)
 
Last edited:
Upvote 0
Top