Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduling offline tasks to be executed when user connect to internet [closed]

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?

like image 348
user3684678 Avatar asked Mar 16 '23 19:03

user3684678


2 Answers

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

like image 81
sanket vetkoli Avatar answered Apr 27 '23 05:04

sanket vetkoli


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" />
like image 24
Randyka Yudhistira Avatar answered Apr 27 '23 05:04

Randyka Yudhistira