-3

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ňツ
  • 1
  • 12
  • 50
  • 89
neha
  • 107
  • 2
  • 11

2 Answers2

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ňツ
  • 1
  • 12
  • 50
  • 89
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
  • 260
  • 2
  • 17