Working with SMS

sktanmoy

Active Member
Licensed User
Longtime User
I'm trying to create an app which will delete the sms received from specific name/number.

I'll use SMSpluslib to delete sms. Now I need your help.

My code is
B4X:
#
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim si As SmsInterceptor
   Dim dl As SmsMessages
   
   Dim Froms() As String 
   Forms = Array As String ("Offer", "Cricket")
   
   
End Sub
Sub Service_Create
   si.Initialize2("si", 999)
End Sub

Sub Service_Start (StartingIntent As Intent)
   StartService("")
End Sub

Sub Service_Destroy
   StopService("")
End Sub

Sub si_MessageReceived (From As String, Body As String) As Boolean
   
   If forms.Contains(From) Then
      'Code of delete the message
   End If
   
End Sub

How the delete message snippet should be?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can return True from si_MessageReceived. The SMS message will not reach the SMS client.

However it is more complicated as the SmsInterceptor will only work while the service is running.
See this tutorial: Intent Filters - Intercepting SMS messages in the background

Try the following solution:
- Use an intent filter to intercept messages in the background.
- If the message needs to be blocked then call another service with StartServiceAt 10 seconds from now.
- Use SMSplus library to get the latest SMS messages. Make sure that the phone number is correct and delete it.
 
Upvote 0

sktanmoy

Active Member
Licensed User
Longtime User
- Use SMSplus library to get the latest SMS messages. Make sure that the phone number is correct and delete it.

The difficulty I'm facing is to get sms id. If I could get id of the sms I was working with, dl.deletesms(id) would work for me.
 
Upvote 0

sktanmoy

Active Member
Licensed User
Longtime User
Okay, I could able to get detail of a message, ex. id=1, threadid=5, ...... Address=Offer
I don't know how to get value of a specific parameter from the above string, ex. value of id or value Address in b4a.
I need your help in this point.

Thanks.
 
Upvote 0

sktanmoy

Active Member
Licensed User
Longtime User
Got it, Regix.split helped me to get message id and sender and now I will be able to do my job.
Thanks a lot.
 
Upvote 0

masterleous

Member
Licensed User
Longtime User
Okay, I could able to get detail of a message, ex. id=1, threadid=5, ...... Address=Offer
I don't know how to get value of a specific parameter from the above string, ex. value of id or value Address in b4a.
I need your help in this point.

Thanks.

Getting speciific value of id or threadid and for other is quite easy

You may use like this
B4X:
Dim SmsMessages1 As SmsMessages
Dim List1 As List
List1 = SmsMessages1.GetAllSince(DateTime.Add(DateTime.now, 0, 0, -1))

Dim smsID as String
Dim smsBody as String

Dim Sms1 As Sms

For i = 0 To List1.Size - 1

Sms1 = List1.Get(i)
smsBody= Sms1.Body   'Storing Specific Body
smsID= Sms1.ID   'Storing Specific ID

Next

Above one is only an example for you, Similarly you may use ThreadId, Read, Date/Time and more specific data of SMS
 
Last edited:
Upvote 0

zekigultekin

Member
Licensed User
Longtime User
SmsInterceptor

when i received new sms i have this error. how can i solve it ??


java.lang.RuntimeException: Error receiving broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x30 (has extras) } in anywheresoftware.b4a.phone.PhoneEvents$SMSInterceptor$1@426deee8


:sign0085:
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
when i received new sms i have this error. how can i solve it ??


java.lang.RuntimeException: Error receiving broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x30 (has extras) } in anywheresoftware.b4a.phone.PhoneEvents$SMSInterceptor$1@426deee8


:sign0085:

I guess you forgot to add permission in the Manifest:

B4X:
AddPermission(android.permission.RECEIVE_SMS)

You may need to start a new thread for your question as this thread is about how to delete an SMS.
 
Last edited:
Upvote 0
Top