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