I want to implement a function in my Android application that will allow the user to receive notifications when their country is supported in my application, and I have some doubts about how to implement this function.
Usually "supported in my application" means updating this application.
The user's country is always the first, and if it is not supported, there is a button where he can click and request information when his country receives support.
Save the country in SharedPreferences . When you first start after each application update, see if the country is on the support list and pops up "hey, now it is supported!". Dialogue. Or, as a last resort, register for ACTION_PACKAGE_REPLACED , and if it was your package that was replaced, see if you support the country and put Notification on the status bar.
When the user clicks the button, I register the broadcast broadcast status on the network through the registerReceiver () method.
Why?
In the registered receiver, I will always check if the user is WiFi, and if so, download a few bytes from the server to check if the user's country is now supported.
You do not need BroadcastReceiver . Whenever you decide to check for updates (for example, once a day using the AlarmManager ), you can check if the device is on the WiFi network. I donβt quite understand why you care about Wi-Fi or not, and I donβt know how it relates to updating your application (usually using the Android Market).
And when they support me, I will unregister the recipient.
He will be unregistered for a long time.
My doubt about this idea is how long the software-registered broadcast receiver will be broadcast? Maybe the receiver will need to live for months.
He will live as long as the user will work on the screen. You need to unregister the recipient before your activity is paused. Even if you intentionally leak the receiver after the action is destroyed - it is a bad idea - it will work for several minutes until Android completes the process.
Register the broadcast receiver in the Android manifest, and when the user request is informed, you need to enter boolean shouldBeInformed in SharedPreferences and always check this value before continuing with the broadcast receiver.
Why?