Chandru
Chandru

Reputation: 5962

How to make android app work in offline mode

I want to know how I can make my app work in offline when user lost the network connectivity.

I need to post data to the server using JSON post method when the user is not connected with wifi or mobile data.

I am using following code to detect the network state using broadcast receiver and don't know how to proceed after this.
Please assist me to proceed further.

public class UpdateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean isConnected = activeNetInfo != null
                && activeNetInfo.isConnectedOrConnecting();
        if (isConnected) {
            Log.e("NET", "connected" + isConnected);

        } else {
            Log.e("NET", "not connected to internet" + isConnected);
        }
    }
}

Upvotes: 1

Views: 1291

Answers (1)

Diffy
Diffy

Reputation: 2359

In this post, its given how to fetch data from sqlite database. And then you can post the data to server using this method. You can write all this code in your

if(isConnected){


}

Upvotes: 1

Related Questions