Monday, 9 September 2013

BroadcastReceiver working for IntentService in background

BroadcastReceiver working for IntentService in background

In my application I want IntentService to upload data from application. I
want this data to be uploaded only through wifi connection. When user put
data to upload my app is sending intent to service and service is handling
this intent.
When service receives intent to handle there could be no wifi connection.
Furthermore user could turn wifi off when my service is uploading data.
For these cases service is registering BroadcastReceiver to handle
connection changes:
Lost wifi connection, to stop uploading data.
Gain wifi connection, to start uploading. This should work even if user
exited from app.
The problem is that I'm not getting intents when connection is changed if
service is not working. Example scenario:
First taks (intent) for service, service is creating
BroadcastReceiver is registered on service create.
onHandleIntent is executed with task.
Before handling is completed I'm changing connection, I'm getting
information about this in my receiver.
All handling is done (all uploaded or there was no wifi connection).
Changing network state doesn't cause getting event on BroadcastReceiver.
My code samples:
public final class MyService extends RoboIntentService {
@Override
public void onCreate() {
super.onCreate();
...
IntentFilter filter = new
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mReceiver, filter);
}
...
private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Ln.v("Network changed"); //Log that network is changed
...
}
};

No comments:

Post a Comment