java - what is the best way to check Internet connection continuously in android -


i developing application. in 1 screen check internet connection, after oncreate() method. if network connection calling 1 asynctask class load countries list , show on screen in spinnerview. if there no network connection showing toast message user , call check_network(asynctask). in class protected long doinbackground(url... params) method i'm checking network connected or not if connected call countries asynctask otherwise again calling check_network(asynctask). process repeat until network connected. problem is correct way check network repeatedly. please suggested me. sorry poor in english please understand.blow showing code

if (checknetwork.isonline(this)) {             try {                 new countryprocess().execute();             } catch (exception e) {                 e.printstacktrace();             }         } else {               toast.maketext(                     getapplicationcontext(),                     getstring(r.string.network_connection_fail)                             + "!", toast.length_long).show();             new network_connectivity().execute(); }  //.......................//  class network_connectivity extends asynctask<url, integer,long>     {         @override         protected long doinbackground(url... params)         {             if (checknetwork.isonline(mainactivity.this)) {                  new countryprocess().execute();              }else             {                 new network_connectivity().execute();             }              return null;         }     } 

add below code in manifest, adding receiver connectivity change intent

<receiver android:name=".networkstatereceiver">    <intent-filter>       <action android:name="android.net.conn.connectivity_change" />    </intent-filter> </receiver> <uses-permission android:name="android.permission.access_network_state" /> 

and @ receiver side, extras associated intent , check status. whenever there change in network status, notified perform task accordingly.

public class networkstatereceiver extends broadcastreceiver { public void onreceive(context context, intent intent) {  super.onreceive(context, intent);  if(intent.getextras()!=null) {     networkinfo ni=(networkinfo) intent.getextras().get(connectivitymanager.extra_network_info);     if(ni!=null && ni.getstate()==networkinfo.state.connected) {         //connected     }  }  if(intent.getextras().getboolean(connectivitymanager.extra_no_connectivity,boolean.false)) {         //not connected  } } } 

for case, add permission in manifest , register receiver in activity.

intentfilter filter = new intentfilter(connectivitymanager.connectivity_action);     registerreceiver(networkreceiver, filter); 

make sure unregister before leaving activity

unregisterreceiver(networkreceiver);  private broadcastreceiver networkreceiver = new broadcastreceiver {     @override     public void onreceive(context context, intent intent) {      super.onreceive(context, intent);      if(intent.getextras()!=null) {         networkinfo ni=(networkinfo) intent.getextras().get(connectivitymanager.extra_network_info);         if(ni!=null && ni.getstate()==networkinfo.state.connected) {             //connected         }      }      //not connected     } } 

and based upon requirement requires connected status 1 time. first check connectivity , if not connected register receiver.

public boolean isnetworkconnected() {         connectivitymanager cm =             (connectivitymanager) getsystemservice(context.connectivity_service);         networkinfo netinfo = cm.getactivenetworkinfo();         if (netinfo != null && netinfo.isconnectedorconnecting()) {             return true;         }         return false;     } 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -