How to have Firebase running in the background?

3

I have a main activity where I see that if I do not have internet in the onCreate , the Firebase ( which has its own AsyncTask ) does not do its work, then the problem is that if I do not obtain information of the Firebase , I can not do the setter of the adapter with the information and the activity would be empty. My question is, how to carry out the task of obtaining continuous Firebase if I do not have internet? How can I get the FB database once connected to the internet?

I have tried the following methods to do a boolean if I am connected or not and they do not help me at all ... even if I had the Wi-Fi, my warning that I was not connected ...

public boolean isOnline() 
{
    ConnectivityManager connMgr = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnected());
}  

and

public boolean isNetworkAvailable(Context context) {
     ConnectivityManager connectivity =(ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);


    if (connectivity == null) {
        return false;
    } else {
        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;
    }

If the Firebase has its own "doInBackground" due to its Asynctask , because when connecting to the internet (wifi or 3g) it does not give me the information to adapt it?

Thank you very much

    
asked by M. Mariscal 05.03.2016 в 08:55
source

2 answers

1

Perfect! RESOLVED!

Firebase having integrated AsyncTask implement within a method (The first parameter is Context where I want to execute the Firebase , and the second parameter the GridView where I want to add the setAdapter ).

When executing it in the onCreate of the main activity: If I am not connected I have set a Toast , and instead I do onDataChange (Method of the FB) + setAdapter in GridView what happened to him as a second parameter. It works perfectly, without any problem!

I hope the explanation has helped you!

    
answered by 07.03.2016 / 00:37
source
2

This method is more useful, which checks if there is connectivity which can be Wifi ( ConnectivityManager.TYPE_WIFI ) or GPrS ( ConnectivityManager.TYPE_MOBILE ):

public Boolean isNetAvailable(Context context)  {      
        try{
           ConnectivityManager connectivityManager = (ConnectivityManager)                                                                               context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
          NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
                    return true;
                }
            }
            catch(Exception e){
                Log.e("Exception Connectivity", e.getMessage());
            }
            return false;
        }

With this you can perform your validation.

Remember to add permission in Manifest.xml :

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

If you did not have it, this may be the reason why your method could not work properly:

    
answered by 05.03.2016 в 21:13