0

I am trying to write an Android application that can receive specific broadcast messages even though the application has not started and then start to execute some code (call a service, etc). How can that be done?

It seems possible because when a device boots up, the OS can send broadcast messages to applications (which have registered to receive the event) even though they are not started yet and can proceed to execute some code.

I have tried implementing a background service but it is triggered only when the application is launched and stopped as soon as the application is stopped.

Do I have to implement a foreground service instead but how do I make sure it can receive broadcast messages and trigger execution of code even though the application is not running yet?

Can this be done or is it something specific for device bootup only?

Edit 22 Mar 2023

I tried to modify my manifest file to add the receiver there but still my receiver does not get called. As far as I know, since I want to trigger my receiver when a package is added, I need the QUERY_ALL_PACKAGE permission and possibly the corresponding query as well.

AndroidManifest.xml

....
    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
        tools:ignore="QueryAllPackagesPermission" />

    <queries>
        <intent>
            <action android:name="android.intent.action.MAIN" />
        </intent>
    </queries>
....

<application>
....
        <receiver android:name=".receiveInstalled" android:exported="true" android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <data android:scheme="package" />
            </intent-filter>
        </receiver>
....
</application>

My class as follows

class receiveInstalled : BroadcastReceiver()
{
    override fun onReceive(p0: Context?, p1: Intent?)
    {
        if(Intent.ACTION_PACKAGE_ADDED == p1?.action)
        {
            val retcomponent = p0?.startService(Intent(p0, GetService2::class.java))
            Log.v("check_services", "startService " + retcomponent)

            val getname = p1?.data?.schemeSpecificPart

            Log.v("check_services", "intent received " + getname.toString())
            Log.v("check_services", "query " + p1?.data?.scheme)
        }
    }
}

If I tried to add registerReceiver inside my code, my receiver gets called but in this case, the receiver is not triggered if my application is closed. I include the code here for comparison and completeness.

        val intentFilter = IntentFilter()
        intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
        intentFilter.addDataScheme("package")
        registerReceiver(receiveInstalled(), intentFilter)

Edit 22 Mar 2023 Part 2

Somehow when I used adb to manually send a broadcast, my receiver will be triggered if I specify my receiver in the command line, otherwise it will not get triggered.

Receiver triggered

am broadcast -a "android.intent.action.PACKAGE_ADDED" -n com.temp.getservice2/.receiveInstalled

Broadcasting: Intent { act=android.intent.action.PACKAGE_ADDED flg=0x400000 cmp=com.temp.getservice2/.receiveInstalled }

Broadcast completed: result=0

Receiver not triggered

am broadcast -a "android.intent.action.PACKAGE_ADDED"

Broadcasting: Intent { act=android.intent.action.PACKAGE_ADDED flg=0x400000 }

Broadcast completed: result=0
localacct
  • 611
  • 5
  • 13
  • Does this answer your question? [Android BroadcastReceiver on startup - keep running when Activity is in Background](https://stackoverflow.com/questions/5290141/android-broadcastreceiver-on-startup-keep-running-when-activity-is-in-backgrou) – dominicoder Mar 21 '23 at 13:59
  • @dominicoder Unfortunately no. That article mentioned about using AndroidManifest.xml to add receivers and I tried but somehow my receiver does not get called. I will update my post to include the snippets. – localacct Mar 22 '23 at 07:26
  • OK. How about this? https://stackoverflow.com/questions/45996338/how-to-register-for-action-package-added-and-action-package-removed-on-android-o – dominicoder Mar 22 '23 at 14:48
  • Does your app have an `Activity`? Have you launched that `Activity`? – David Wasser Mar 23 '23 at 11:59

1 Answers1

0

Looks like this is probably the cause of your problem:

https://developer.android.com/about/versions/oreo/background.html#broadcasts

You cannot register a receiver to be triggered on implicit broadcasts (PACKAGE_ADDED is implicit) in the manifest. So you can't build a component that would be launched by Android in this case. See also

How to register for ACTION_PACKAGE_ADDED and ACTION_PACKAGE_REMOVED on Android Oreo?

for more info

David Wasser
  • 93,459
  • 16
  • 209
  • 274