Created
July 2, 2018 07:36
-
-
Save Bringoff/11c76d2fbf9de717f567c3e53acb4db9 to your computer and use it in GitHub Desktop.
Implicit broadcast dynamic registration solution for target sdk >= 26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| private Set<ImplicitBroadcastRegistrationLifecycleObserver> getRequiredRegistrators() { | |
| Set<ImplicitBroadcastRegistrationLifecycleObserver> registrators = new HashSet<>(); | |
| registrators.add(new AppInstallationObserver(this)); | |
| ... | |
| return registrators; | |
| } | |
| @Override | |
| public void onCreate() { | |
| ... | |
| Set<ImplicitBroadcastRegistrationLifecycleObserver> broadcastRegistrators = getRequiredRegistrators(); | |
| for (ImplicitBroadcastRegistrationLifecycleObserver registrator : broadcastRegistrators) { | |
| ProcessLifecycleOwner.get().getLifecycle().addObserver(registrator); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AppInstallationObserver | |
| extends ImplicitBroadcastRegistrationLifecycleObserver<AppInstallationBroadcastReceiver> { | |
| public AppInstallationObserver(@NonNull Context context) { | |
| super(context); | |
| } | |
| @Override | |
| protected AppInstallationBroadcastReceiver createReceiver() { | |
| return new AppInstallationBroadcastReceiver(); | |
| } | |
| @Override | |
| protected IntentFilter createBroadcastFilter() { | |
| IntentFilter filter = new IntentFilter(); | |
| filter.addAction(Intent.ACTION_PACKAGE_ADDED); | |
| filter.addAction(Intent.ACTION_PACKAGE_REMOVED); | |
| filter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED); | |
| filter.addAction(Intent.ACTION_PACKAGE_REPLACED); | |
| filter.addDataScheme("package"); | |
| return filter; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Starting from Android API 26 most of implicit receivers are not allowed to be registered in manifest, so register it | |
| * dynamically according to app lifecycle. This Observer should be registered in app class | |
| * {@link Application#onCreate()} method like this: | |
| * <code>ProcessLifecycleOwner.get().getLifecycle().addObserver(thisObserverInstance)</code> | |
| */ | |
| public abstract class ImplicitBroadcastRegistrationLifecycleObserver<R extends BroadcastReceiver> | |
| implements LifecycleObserver { | |
| @NonNull | |
| private final Context context; | |
| private final R receiver; | |
| private final IntentFilter receiverFilter; | |
| public ImplicitBroadcastRegistrationLifecycleObserver(@NonNull Context context) { | |
| this.context = context; | |
| receiver = createReceiver(); | |
| receiverFilter = createBroadcastFilter(); | |
| } | |
| protected abstract R createReceiver(); | |
| protected abstract IntentFilter createBroadcastFilter(); | |
| @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) | |
| void onAppCreated() { | |
| context.registerReceiver(receiver, receiverFilter); | |
| } | |
| @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) | |
| void onAppDestroyed() { | |
| context.unregisterReceiver(receiver); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment