Search code examples
androidbroadcastreceiver

Autosync android app data when mobile connected to network


I have developed an application, which has a registration form. The data from registration form will be uploaded to WebService if candidates mobile is connected to the network. Else that data will be stored in local MySQL database.

What I want now is, when mobile is connected to a network, then the locally stored data should upload to WebService, even though he don't open that application. I am expecting functionality same as of WHATSAPP.

Edited Solved

Finally i found solution to this problem.Thank you so much everyone for participation.

package com.example.detapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

    public class InternetConnector_Receiver extends BroadcastReceiver {
        boolean isVisible;
        public static int flag1=0;
        public InternetConnector_Receiver() {

                                             }

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

                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkInfo networkInfo = connectivityManager
                            .getActiveNetworkInfo();

                    // Check internet connection and accrding to state change the
                    // text of activity by calling method
                    if ((networkInfo.isConnected()==true)) {
                        Log.i("*************DEEPA* insideif", "*********network : " +networkInfo.isConnected() );
                        flag1=1;
                        Toast.makeText(context, "*INTERNET CONNECTED*", Toast.LENGTH_SHORT).show();

                        new MainActivity().Synchronization(flag1);


                    } else {
                        flag1=0;

                        Toast.makeText(context, "*NO INTERNET*", Toast.LENGTH_SHORT).show();

                        new MainActivity().Synchronization(flag1);
                    }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

`

Added this broadcast receiver class InternetConnector_Receiver.java.This will work even when you are off to application.

And in MainActivity.java i added following code

protected void Synchronization(int flag)
    {
    if(flag==1)
    {
        //call webservice or sync Adapters here to synch data
    }
    }

Thank you all once again


Solution

  • import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    
    
    public class CustomBroadCastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(isNetworkAvail(context)){
                //TODO:
            }
        }
        public boolean isNetworkAvail(Context mContext) {
            try {
                ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connectivity != null) {
                    NetworkInfo[] info = connectivity.getAllNetworkInfo();
                    if (info != null)
                        for (int i = 0; i < info.length; i++)
                            if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                                return true;
                            }
                }
                return false;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    }