Android Question Intercept SMS messages without notification Android 4.4

gawie007

Member
Licensed User
Longtime User
Hi,

It appears that the ability to intercept and consume SMS messages will not be allowed from Android 4.4 unless the program that we write is the default SMS App.

I came across this on my almost fruitless search to try to delete an SMS from the store.
http://android-developers.blogspot.co.uk/2013/10/getting-your-sms-apps-ready-for-kitkat.html

My programs will be filling up the SMS stores with meaningless information (to the user) because of this change if my program is not the main receiver OR unless someone has a solution - besides installing older versions of Android.

What are Google thinking?????
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1) The standard SMS message can get the message if it listens to the SMS_RECEIVED_ACTION. It does make sense however that it will not listen to this action and only listen to the DELIVERY action as it isn't expected to do anything if there is a different app (Hangout for example) that handles incoming messages. I don't think that there is anything you can do about it.

2) You cannot do it programmatically. You can check whether your app is the default app and if not show the dialog asking the user to change the default app:
B4X:
  Dim context As JavaObject = Activity
   context = context.RunMethod("getContext", Null)
   Dim so As JavaObject
   Dim package As String = so.InitializeStatic("android.provider.Telephony.Sms").RunMethod( _
     "getDefaultSmsPackage", Array As Object(context))
   If package <> "b4a.example" Then
     Dim i As Intent
     i.Initialize("android.provider.Telephony.ACTION_CHANGE_DEFAULT", "")
     i.PutExtra("package", "b4a.example")
     StartActivity(i)
   End If
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Erel, first of all thanks again for your incredible support. How can you manage b4a development and have time to reply to everybody ?
Do you have 48h days ? :D

Second, I'm so sad to be in this situation with KitKat that is arriving as update on many devices.
My app will not work properly and I can't see actually a good solution.
Perhaps let my app be the default SMS app and implement code and graphics interface for sending/receiving SMS, but could be a complex task ...
Marco
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Erel has a minimum of 16 cores i suppose :D
 
Upvote 0

pxpto

Member
Licensed User
Longtime User
Turns out you need to manage all the options or it will not be listed. You need to have three services and add this code:
B4X:
SetReceiverAttribute(service1, android:permission, "android.permission.BROADCAST_SMS")
AddReceiverText(service1, <intent-filter>
  <action android:name="android.provider.Telephony.SMS_DELIVER" />
  </intent-filter>
)
SetReceiverAttribute(service2, android:permission, "android.permission.BROADCAST_WAP_PUSH")
AddReceiverText(service2,  <intent-filter>
  <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
  <data android:mimeType="application/vnd.wap.mms-message" />
  </intent-filter>
)
SetServiceAttribute(service3, android:permission, "android.permission.SEND_RESPOND_VIA_MESSAGE")
SetServiceAttribute(service3, android:exported, "true")
AddServiceText(service3,  <intent-filter>
  <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:scheme="sms" />
  <data android:scheme="smsto" />
  <data android:scheme="mms" />
  <data android:scheme="mmsto" />
  </intent-filter>
)
AddActivityText(main,
<intent-filter>
  <action android:name="android.intent.action.SEND" />  
  <action android:name="android.intent.action.SENDTO" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="sms" />
  <data android:scheme="smsto" />
  <data android:scheme="mms" />
  <data android:scheme="mmsto" />
  </intent-filter>
)

I added this code to my manifest and I have the following compilation error:

B4X:
Parsing code.                          0.01
Compiling code.                        Error
Module: service1_br not found.

My app has just the Main module and 1 Service. Do I need to create a "service1", "service2" and "service3"? With what code?
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
B4X:
     If package <> "b4a.example" Then
     Dim i As Intent
     i.Initialize("android.provider.Telephony.ACTION_CHANGE_DEFAULT", "")
     i.PutExtra("package", "b4a.example")
     StartActivity(i)
   End If

Hi Erel, I have successfully listed my app in the Settings dialog for the user to choose as "Default SMS app" (as per your post, by adding 3 Services and 4 required code snippets in the manifest). It seems that if an app - after it is installed - has 3 Services and has the 4 required codes in manifest file, it will be automatically listed in the Settings.

However, I could not make your code above work. It shows no error but the Settings select dialog does not appear after StartActivity(i) (nothing happened). So if I want to turn on my app as default, I have to go to Settings and select it manually...

Any advice or thought? Thank you very much.

EDIT: Solved. I wrongly put word package instead of "package". Thank you.
 

Attachments

  • TestSMSdefaultapp.zip
    9.1 KB · Views: 873
Last edited:
Upvote 0

lymey

Active Member
Licensed User
Longtime User
This may be a silly question, is it possible to amend this part of the code to run in a Service? If not, how can you get the default package?
Thanks!
B4X:
  Dim context As JavaObject = Activity
   context = context.RunMethod("getContext", Null)
   Dim so As JavaObject
   Dim package As String = so.InitializeStatic("android.provider.Telephony.Sms").RunMethod( _
     "getDefaultSmsPackage", Array As Object(context))
 
Upvote 0

KY Leng

Member
Licensed User
Longtime User
Erel, could you give us a small project that make an sms application as default? I could not make it by follow your block of code
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is no simple way to make your application a default application. You need to add the manifest code I posted here: http://www.b4x.com/android/forum/th...t-notification-android-4-4.37171/#post-227969

The user then needs to set your app as the default app: http://www.cnet.com/how-to/manage-default-sms-app-on-android-4-4-kitkat/
However the user will only agree to set your app as the default app if you actually implement all of the features of a messaging app. This will require a large project...
 
Upvote 0

Humberto

Active Member
Licensed User
Longtime User
Hi
The code to verify if my program is set as SMS option only work in Android 4.4
I downloaded the project in post #28


B4X:
Dim context As JavaObject = Activity
   context = context.RunMethod("getContext", Null)
   Dim so As JavaObject
   Dim package As String = so.InitializeStatic("android.provider.Telephony.Sms").RunMethod( _
     "getDefaultSmsPackage", Array As Object(context))
   If package <> "b4a.example" Then
     Dim i As Intent
     i.Initialize("android.provider.Telephony.ACTION_CHANGE_DEFAULT", "")
     i.PutExtra("package", "b4a.example")
     StartActivity(i)
   End If


I get this error with a Samsung S3 with android 4.3

===========================================================
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: Method: getDefaultSmsPackage not found in: android.provider.Telephony$Sms
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:346)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:97)
at b4a.example.main._button1_click(main.java:326)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:163)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:77)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Method: getDefaultSmsPackage not found in: android.provider.Telephony$Sms


Sorry I found that exist just from API 19

Thanks
 
Last edited:
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
If I don't say something of stupid, the "defaut sms provider" is related to KitKat and later and does not exist before.
You can check if the user is running Kitkat (and behave accordingly in your program) with this code

B4X:
Dim p1 As Phone
If p1.SdkVersion>18 Then

Marco
 
Upvote 0

PABLO2013

Well-Known Member
Licensed User
Longtime User
Hi I have an application that basically a message is sent to change the color of a panel.
When sending the message from my phone: not notification of Messages and the application works; but when sending the message from a module (SIM900:AT+CMGS="+ 506 ... "), a notification appears of the Recent Messages and blocks the application, when running debug the application service module works reading the message but the execution of the color change does not react because notification Messages blocks the app , how can I do so that does not present the notification . thanks

android 4.1.2
 

Attachments

  • Phone1.png
    Phone1.png
    21.7 KB · Views: 593
  • sim900.png
    sim900.png
    27.7 KB · Views: 666
  • Sample.zip
    9.3 KB · Views: 527
Last edited:
Upvote 0

DaOel

Member
Licensed User
Longtime User
I was not able to add it as standard sms app with the proposed manifest changes.

However manifest change worked for me:

manifest:
AddApplicationText(
        <!-- BroadcastReceiver that listens for incoming SMS messages https://stackoverflow.com/questions/30127564/how-do-i-set-my-app-as-the-default-sms-app -->
        <receiver
           android:name=".SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>

        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver
            android:name=".MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>

        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name=".ComposeSmsActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </activity>

        <!-- Service that delivers messages from the phone "quick response" -->
        <service
            android:name=".HeadlessSmsSendService"
            android:exported="true"
            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE">
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>
)
 
Upvote 0
Top