Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long will programmatically registered broadcast receiver live?

I want to implement feature in my Android application which will allow user to receive notification when their country is supported in my application and I have some doubts how to implement this feature.

Use case scenario looks like this:

User goes to Activity where he can see the list of supported countries. User's country is always first and if it's not supported there is a button where he can click and request to be informed when his country become supported.

Some of ideas:

1. When user click on a button I'll register network state changed broadcast receiver via registerReceiver() method. In registered receiver I'll always check if user is on WiFi and if it is, download some bytes from server to check if user's country is now supported. And when became supported I'll unregister the receiver. My doubt in this idea is how long will programmatically registered broadcast receiver live? Maybe receiver will need to live for months. If user doesn't uninstall my application will receiver be receiving events all the time?

2. Register broadcast receiver in Android Manifest and when user request to be informed put boolean shouldBeInformed in SharedPreferences and always check this value before proceeding in broadcast receiver.

Do you have any ideas how to handle this problem elegantly and efficiently? C2DM is not suitable solution at this momemnt.

Thanks.

like image 398
Aleksandar Ilic Avatar asked Sep 05 '11 00:09

Aleksandar Ilic


People also ask

What is the time limit for completion of broadcast receiver session?

In general, you want onReceive() to return in under a millisecond, in case your UI is in the foreground, so you do not freeze the UI (a.k.a., have "jank"). There is also a 5-10 second limit, after which Android will basically crash your app.

What is the life cycle of broadcast receivers in Android?

Latest Android Aptitude Question SOLUTION: What is the life cycle of broadcast receivers in android? Options 1) send intent() 2) onRecieve() 3) implicitBroadcast() 4) sendBroadcast(), sendOrderBroadcast(), and sendStickyB.

How do you check if broadcast receiver is registered or not?

A simple solution to this problem is to call the registerReceiver() in your Custom Application Class. This will ensure that your Broadcast receiver will be called only one in your entire Application lifecycle.

Does broadcast receiver run in background?

A broadcast receiver will always get notified of a broadcast, regardless of the status of your application. It doesn't matter if your application is currently running, in the background or not running at all.


1 Answers

I want to implement feature in my Android application which will allow user to receive notification when their country is supported in my application and I have some doubts how to implement this feature.

Usually, "supported in my application" would imply an update to that application.

User's country is always first and if it's not supported there is a button where he can click and request to be informed when his country become supported.

Save the country in SharedPreferences. On first launch after each update to the application, see if the country is on the supported list, and pop up a "hey! it's now supported!" dialog. Or, in a pinch, register for ACTION_PACKAGE_REPLACED, and if it was your package that was replaced, see if you support the country and put a Notification in the status bar.

When user click on a button I'll register network state changed broadcast receiver via registerReceiver() method.

Why?

In registered receiver I'll always check if user is on WiFi and if it is, download some bytes from server to check if user's country is now supported.

You do not need a BroadcastReceiver for this. Whenever you decide to check for updates (e.g., once per day via AlarmManager), you can check to see if the device is on WiFi. I am not quite certain why you care about WiFi or not, and I have no idea what this has to do with updating your application (typically handled by the Android Market).

And when became supported I'll unregister the receiver.

It will have been unregistered a long time ago.

My doubt in this idea is how long will programmatically registered broadcast receiver live? Maybe receiver will need to live for months.

It will live for however long the user has the activity on the screen. You need to unregister the receiver before your activity is paused. Even if you intentionally leak the receiver after the activity is destroyed -- which is a bad idea -- it will live for minutes, until Android terminates the process.

Register broadcast receiver in Android Manifest and when user request to be informed put boolean shouldBeInformed in SharedPreferences and always check this value before proceeding in broadcast receiver.

Why?

like image 146
CommonsWare Avatar answered Oct 26 '22 17:10

CommonsWare