Search code examples
androidandroid-intentbluetooth-lowenergyforeground-service

Calling the action from ForegroundService


I'm writing an emergency application that, when a button is pressed on an bluetooth SOS device, performs several actions (calling and sending sms to several contacts). Bluetooth is listened to in ForegroundService so that the user can work on the phone without interference. The idea behind this application is to display the emergency screen unconditionally and perform the appropriate actions after reading that the button has been pressed. And here's where the problem arises - since Android 10, you can't call intentions from background activity. Looking for solutions, I have not found much. The only code I came across is this:

Uri uri = new Uri.Builder().scheme("rating").authority("call").build();
                            Intent i = new Intent(Intent.ACTION_VIEW);
                            i.setData(uri);
                            //i.putExtra("call_ratings_for", call_id);
                            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(i);

In the manifest file in the called activity item added:

<intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="rating" />
</intent-filter>

Unfortunately, this only works when the application is in the foreground.

Is there any way - official or unofficial - to implement this? I am already losing hope that such an application that is needed can be created at all.


Solution

  • I have 2 ideas that you can look at:

    • The first is CompanionDeviceManager. If your app is associated with a companion device (and this sounds exactly like your situation), the app should be able to launch an Activity in response to some action on the companion device.

    • The second idea is giving your app SYSTEM_ALERT_WINDOW permission. If your app has this permission, your Service should be able to launch an Activity even if it is in the background.