Download Data When Internet Connectivity Is Detected
Solution 1:
Which is the best practice to do so?
Best way is to use IntentService.
Start this service from your BroadcastReceiver. Via IntentService, all requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), so no worry about how long your task is.
Is broadcast receiver stays around even when application is not on?
Yes, if they are registered via manifest.
Helpful resources are
Solution 2:
in the android developer guide mentioned you can Monitor for Changes in Connectivity
1st - you create a NetworkChangeReceiver class that is a BroadCastReceive , where you implement your code to download what you want :
publicclassNetworkChangeReceiverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(final Context context, final Intent intent) {
finalConnectivityManagerconnMgr= (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
finalNetworkInfowifi= connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfoactiveNetwork= connMgr.getActiveNetworkInfo();
booleanisConnected= wifi != null &&
wifi.isConnected() ;
if (isConnected ) {
// Do something
Log.d("Netowk Available ", "Flag No 1");
}
}
}
2nd - you register your BroadCastReceiver
in the manifest file like the following :
<receiverandroid:name=".NetworkChangeReceiver" ><intent-filter><actionandroid:name="android.net.conn.CONNECTIVITY_CHANGE" /></intent-filter></receiver>
Post a Comment for "Download Data When Internet Connectivity Is Detected"