3

So I have followed this guideline to show a simple toast when SMS is received. While it works ok when app is running, when I go to settings and force-close the app, it stops working.

I checked many answers here on StackOverflow for simmilar questions, but none actually answers whether (and how) it is possible to make a piece of code execute EVERY time SMS is received, without the app being set as the default SMS app on device (Android 4.4+). Is it?

Consider that even service can be stopped, and when that happens, service is not a solution anymore.

I am interested in API level 19+

Thanks

michnovka
  • 2,880
  • 3
  • 26
  • 58

2 Answers2

5

Unfortunately, no, this isn't really possible without your app being the default SMS app.

When the user forcibly closes your app, it is put back into the stopped state, and a statically registered Receiver for the implicit SMS_RECEIVED broadcast won't work until your app has been explicitly started again; e.g., by the user launching your app from an explicit launcher shortcut.

The default SMS app, on the other hand, will be delivered the SMS_DELIVER broadcast, and that is explicit. Even if the default has been forcibly stopped, that broadcast will act like any other explicit starting Intent to bring it out of the stopped state.

If timeliness isn't a major concern, you could just query the SMS Provider as needed – e.g., at each startup – and determine if you've missed any new messages since last checked.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
0

I was making an SMS app. The broadcast would not work when was closed. Tried many things but it didn't work out. Then I decided to change it to the default SMS app by the old code, then the app splash keeps restarting. Then, when I used the following code but it was not working. The dialog box would not show. Then, I found a answer on StackOverflow. Basically you need one activity, TWO receivers and one service. when I added all that to manifest. The default SMS dialog was shown in the app. Which was a success. then I closed the app. my app was receiving the broadcast even when closed. So everything works out pretty well in the end. I hope my experience will help you.

Here is the link How do I set my app as the default SMS app?

    void defaultSmsApp(){
    RoleManager roleManager;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        roleManager = getApplicationContext().getSystemService(RoleManager.class);
        if (roleManager.isRoleAvailable(RoleManager.ROLE_SMS)) {
            Log.e(TAG, "defaultSmsApp: " );
            if (roleManager.isRoleHeld(RoleManager.ROLE_SMS)) {
                Toast.makeText(getApplicationContext(), "PrismApp set as default.", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
                startActivity(i);
            } else {
                Toast.makeText(getApplicationContext(), "NOT DEFAULT.", Toast.LENGTH_SHORT).show();
                Intent roleRequestIntent = roleManager . createRequestRoleIntent (
                        RoleManager.ROLE_SMS);
                 someActivityResultLauncher.launch(roleRequestIntent);
            }
        }
    }
}


ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    // There are no request codes
                    Intent data = result.getData();

                }
            }
        });
Rohaitas Tanoli
  • 624
  • 4
  • 16