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