In my app i am doing offline caching and i want to schedule offline tasks to be executed when user connect to internet.I found JobScheduler API for that but it only supports API level 21.Is there any alternative of JobScheduler for API less than 21 which help me schedule tasks to be executed when user connect to internet?
You can use GcmNetworkManager, it fits the need you described, here is a working sample. It uses JobScheduler internally above 21, and below 21 it uses some Google propriety
Make a broadcast receiver to receive connectivity change
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() || mobile.isAvailable()) {
// Do something
Log.d("Netowk Available ", "Flag No 1");
}
}
}
And in your manifest, add permission
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With