android - How to raise an Exception while inserting to firebase if the data is not synced to server immediately? -


i inserting values firebase. want mark insert operation failed if data not synced server after few second delay. tried oncompletelistener , onfailurelisteneron task, onfailurelistener doesn't fire if device offline then,oncompletelistener fires after successful sync firebase server (the device comes online). want fire onfailurelistener if device offline. how can achieve that?

you can check availability of network before inserting values using following function

public static boolean isnetworkavailable(context context){     connectivitymanager manager = (connectivitymanager) context.getsystemservice(context.connectivity_service);     networkinfo info = manager.getactivenetworkinfo();     return info != null; }  

update

you can check if internet working or not if it's connected.

public static boolean hasactiveinternetconnection(context context){     if(isnetworkavailable(context)) {         try {             httpurlconnection connection = (httpurlconnection) (new url("http://clients3.google.com/generate_204")).openconnection();             connection.setrequestproperty("user-agent", "test");             connection.setrequestproperty("connection", "close");             connection.setreadtimeout(1500);             connection.connect();             return (connection.getresponsecode() == 204 && connection.getcontentlength() == 0);         } catch (ioexception e){             log.e("error", "error checking internet connection");         }     } else log.e("error", "no network available");     return false; } 

Comments

Popular posts from this blog

cookies - Yii2 Advanced - Share session between frontend and mainsite (duplicate of frontend for www) -

angular - password and confirm password field validation angular2 reactive forms -

php - Permission denied. Laravel linux server -