0

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

Deepa MG
  • 188
  • 1
  • 15
  • 1
    you can use broadcast receiver if you are connected with net it will be run. – D.J Oct 19 '16 at 04:41
  • try this also. – D.J Oct 19 '16 at 04:43
  • Yeah my application have used broadcast receiver already.But broadcast received is limited to only my application,so data will be uploaded only when i open my application.It will not autosync as like WHATSAPP. – Deepa MG Oct 19 '16 at 04:50
  • post your receiver code and menifest related to this. – D.J Oct 19 '16 at 04:51
  • See about sync adapter. It will help u. https://developer.android.com/training/sync-adapters/creating-sync-adapter.html – Prakash Bala Oct 19 '16 at 05:03
  • Simple you can use network broadcast receiver. – Piyush Oct 19 '16 at 06:44
  • How can i set time duration for broadcast receiver?I mean i want to call it again after delay of 5minutes.Because it is calling often and not let me to complete my synchronization in that time of delay. – Deepa MG Oct 20 '16 at 07:02
  • How to test your working code @DeepaMG? We tried it but no luck. When that Toast message displays? – mpsbhat Sep 25 '17 at 09:50

2 Answers2

1
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;
        }
    }
}
anonymous
  • 401
  • 7
  • 15
vyshak
  • 26
  • 2
  • ,Thanks for reply.But i used broadcast receiver already in my code.what should i do further to sync my data from local database to webserver automatically even when i am not using application? – Deepa MG Oct 19 '16 at 07:53
  • do your webservice code inside the if(), it will work even if the app is not open.. – vyshak Oct 19 '16 at 09:05
0

if your app needs to run in background,then create a broadcast reciever and also specify the intent-filter and permission in manifest file. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><receiver android:name=".CustomBroadCastReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>

vyshak
  • 26
  • 2
  • I have used intent-filter and permission in my manifest file.I dint got what you mean by " then create a service and attach a broadcast reciever ". – Deepa MG Oct 19 '16 at 06:08
  • I am very new to android.Please explain me about it clearly.please – Deepa MG Oct 19 '16 at 06:09