-4

enter image description here

I am making an app in which the launch activity fetches the location of user and when it fetched the location it redirects the user to another activity.But when there is no internet available then it must not redirect the user to another activity

This is the part of my main activity code:

protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkReceiver, filter);  //here i have registerd the receiver
// create class object
final GPSTracker gps = new GPSTracker(WelcomeActivity.this);
// check if GPS enabled
if (gps.canGetLocation()) {

    latitude = gps.getLatitude();
    longitude = gps.getLongitude();
    if (latitude != 0.0 && longitude != 0.0) {
        if (Utils.isNetworkAvailable(WelcomeActivity.this)) {
            fetchlocation.setText("Fetching your Location...");
            sendGetAddressRequest(latitude, longitude);
        }
        else
            fetchlocation.setText("No Internet Connectivity");
    } else {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                latitude = gps.getLatitude();
                longitude = gps.getLongitude();
                if (latitude != 0.0 && longitude != 0.0) {
                    if (Utils.isNetworkAvailable(WelcomeActivity.this)) {
                        fetchlocation.setText("Fetching your Location...");
                        sendGetAddressRequest(latitude, longitude);
                    } else
                        fetchlocation.setText("No Internet Connectivity");
                } else {
                    Toast.makeText(WelcomeActivity.this, "Couldn't detect location", Toast.LENGTH_LONG).show();
                }
            }
        }, 5000);
    }


} else {
   // fetchlocation.setText("Enable Location First");
    gps.showSettingsAlert();
}

}

This is in onPause:

@Override
protected void onPause() {
    unregisterReceiver(networkReceiver); // here i have unregistered the receiver
    super.onPause();
}
public boolean isNetworkConnected() {
    ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

And this is my NetwrokConnectivity code:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getExtras() != null) {
        NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
            // we're connected
        }
    }
}

What happens is when there is no internet available the textview shows text as Np Internet connectvity..And when i turned on the internet it still displayys No Internet Connectivity..I want is when Internet is coming or I turned on the Wifi or the network data,the textview must display text as Fetching your location and redirects user to another activity

Maveňツ
  • 9,147
  • 14
  • 53
  • 94
neha
  • 101
  • 2
  • 11

3 Answers3

0
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

If this boolean is true that means u have net connection and then perform ur operation like this:

if(isNetworkAvailable()){
     // perform your operation here 
}

You will also need:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Maveňツ
  • 9,147
  • 14
  • 53
  • 94
0

Call this way from your activity:

getNetworkType(activity)

For getting wifi or sim internet:

public static String getNetworkType(final Context activity) {
        String networkStatus = "";

        final ConnectivityManager connMgr = (ConnectivityManager)activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        // check for wifi
        final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        // check for mobile data
        final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isConnected() && wifi.isAvailable()) {
            networkStatus = "Wifi";
        } else if (mobile.isAvailable()&& mobile.isConnected()) {
            networkStatus = getDataType(activity);
        } else {
            networkStatus = "noNetwork";
        }
        return networkStatus;
}

getDataType() Method:

For more precise details(2G ,3G or 4G).

public static String getDataType(Context activity) {
    String type = "Mobile Data";
    TelephonyManager tm = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
    System.out.println("tm.getNetworkType(): "+tm.getNetworkType());
    switch (tm.getNetworkType()) {
        case TelephonyManager.NETWORK_TYPE_HSDPA:
            type = "Mobile Data 3G";
            Log.d("Type", "3g");
            // for 3g HSDPA networktype will be return as
            // per testing(real) in device with 3g enable
            // data
            // and speed will also matters to decide 3g network type
            break;
        case TelephonyManager.NETWORK_TYPE_HSPAP:
            type = "Mobile Data 4G";
            Log.d("Type", "4g");
            // No specification for the 4g but from wiki
            // i found(HSPAP used in 4g)
            break;
        case TelephonyManager.NETWORK_TYPE_CDMA:
            type = "Mobile Data CDMA";
            Log.d("Type", "CDMA");
            // No specification for the CDMA but from wiki
            break;

        case TelephonyManager.NETWORK_TYPE_LTE:
            type = "Mobile Data 4G LTE";
            Log.d("Type", "LTE");
            break;

        case TelephonyManager.NETWORK_TYPE_GPRS:
            type = "Mobile Data GPRS";
            Log.d("Type", "GPRS");
            break;

        case TelephonyManager.NETWORK_TYPE_EDGE:
            type = "Mobile Data EDGE 2G";
            Log.d("Type", "EDGE 2g");
            break;

        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            type = "Mobile Data UNKNOWN";
            Log.d("Type", "unknown");
            break;

    }
    return type;
}
Maveňツ
  • 9,147
  • 14
  • 53
  • 94
0

You must check if you are connected to the internet and not just to a wifi network. you might wanna try this:

try {
        HttpURLConnection urlc = (HttpURLConnection) (new URL("http://google.com").openConnection());
        urlc.setRequestProperty("User-Agent", "Test");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1500);
        urlc.connect();

        isConnected = true;

}catch (IOException e) {

        isConnected = false;

}
dranreb dino
  • 250
  • 2
  • 16