I am trying to receive a Broadcast Intent to check which app is being launched currently.
(In order to send a Notification/Popup, reminding the user)
in my Manifest file:
<receiver
android:name=".domain.background.receiver.OpenedApplicationBroadcastReceiver"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
And do something in the receiver:
class OpenedApplicationBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// do something if it is application xyz
}
}
But so far this isn’t working.
But I am also quite clueless what to put in my <intent-filter>
in order to receive such broadcast.
What do I need to change so that my code works?
I am trying to receive a Broadcast Intent to check which app is being launched currently
There is no such broadcast.
4
you can register a broadcast receiver for the ACTION_PACKAGE_ADDED and ACTION_PACKAGE_REPLACED intents. These intents are broadcasted when a new package is installed or replaced, which will tell that an app is being launched.
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && (action.equals(Intent.ACTION_PACKAGE_ADDED) ||action.equals(Intent.ACTION_PACKAGE_REPLACED))) {
String packageName = intent.getDataString();
Log.d("PackageChangeReceiver", "Package added/replaced: " + packageName);
// You can check the package name to find which app is being launched
}
}
this should be done in Manifest file.
<receiver android: name=".dBroadcastReceiver">
<intent-filter>
<action android: name="android.intent.action.PACKAGE_ADDED" />
<action android: name="android.intent.action.PACKAGE_REPLACED" />
<data android: scheme="package" />
</intent-filter>
</receiver>
1