Reputation: 5679
I want to show a notification when a new package is added. and I found the code that the manifest file need! what I can't figure out is how to catch the broadcast inside my service. How can I do that?
Upvotes: 3
Views: 2814
Reputation: 3268
Though the answer is already posted and will work till Android 29 or Android 10, but from Android 11 or Android 30 onwards we just need to add <queries>
or need to add the below permission in AndroidManifest.xml file -
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES" />
It's because of there are few behavior changes in Android 11 - https://developer.android.com/training/package-visibility
Upvotes: 0
Reputation: 132982
You can Register an PACKAGE_INSTALL
and PACKAGE_ADDED
Receiver for Receiving package install and uninstall events and then Start your Service( i.e IntentService)
from onReceive
of Broadcast Receiver for Showing notification when a new package is added.
In Manifest.xml:
<receiver android:name=".PackageReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
in PackageReceiver :
public class PackageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.PACKAGE_INSTALL)) {
//START YOUR SERVICE HERE
}
}
}
OR you can also register an receiver dynamically in your service
br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//SHOW notification here or Start Notification Service
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL);
intentFilter.addDataScheme("package");
registerReceiver(br, intentFilter);
Upvotes: 8