How long will a software-registered broadcast receiver last?

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.

An example use case is as follows :

The user switches to activity, where he can see a list of supported countries. 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.

Some ideas :

1. When the user clicks the button, I register the broadcast status of broadcast broadcasts on the network using the registerReceiver () method. In the registered receiver, I will always check if the user is in WiFi, and if so, download a few bytes from the server to check if the user's country is now supported. And when they supported me, I unregistered the recipient. My doubt about this idea is how long a live broadcast receiver will be programmatically registered? Maybe the receiver will have to live for months. If the user does not delete my application, will the recipient receive events all the time?

2. Register the broadcast receiver in the Android manifest and, when the user request is informed, enter boolean shouldBeInformed in SharedPreferences and always check this value before continuing with the broadcast receiver. p>

Do you have any ideas how to deal with this problem elegantly and efficiently? C2DM is not suitable for solving this problem.

Thanks.

+4
source share
1 answer

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?

+2
source

All Articles