lundi 25 mai 2015

Correct and most accurate way to check the internet Status Background service or reciever

    public class InternetChecker extends BroadcastReceiver {
     @Override
        public void onReceive(Context context, Intent intent) {
    ConnectivityManager cm = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
            if (cm == null)
                return;
            if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
    //execute my async activity 

    }else{
    //store the values in temp file
    }
  }
}

This is what in my manifest file

<receiver android:name="com.company.InternetChecker" android:exported="false">
            <intent-filter>
                <action android:name="android.net.wifi.STATE_CHANGE" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

Problem is it is only getting detected when the INTERNET is made ON from OFF state When the data connection goes and comes back it dont react to it. For instance wifi connection is instable and then again stable the background service doesnt work accordingly

Physically when the internet/wifi is turned ON from OFF state this background service

I hope i am clear

Aucun commentaire:

Enregistrer un commentaire