1

I have an app which has a foreground notification started from a service. The service calls at each second a class which serves as a GPS connection. The GPS class uses NETWORK_PROVIDER and GPS_PROVIDER.

All worked correct so far, except for today until something happened.

At some point a message was displayed by Android system saying something like "Unless you connect to wifi network or mobile network, you will not be able to access email and internet". I don't remember well the message, but I had 2 buttons: "Cancel" and "Ok". I might have clicked on something when pulling the phone out of my pocket.

On top of this message was another message saying that my app unfortunately crashed. So the order of events seem to be this: network disabled (?), app crashed.

I think that I need to handle the network error somehow, but I'm clueless right now, because I couldn't get to much info from the error.

Any thoughts?

Thanks

Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
George I.
  • 560
  • 2
  • 7
  • 26
  • check for network before you go on – Elltz Jan 06 '15 at 09:56
  • @sufiyan thanks for answer. I didn't had time to test the code, but I didn't forgot, don't worry. I'm still concerned where to use the code you suggested, since I use a location event listener. – George I. Jan 06 '15 at 14:55
  • This answer has a similar code to the one I use: http://stackoverflow.com/a/10917500/702196 I don't know if I can place your code anywhere there. – George I. Jan 06 '15 at 14:59
  • could you please elaborate ? – Sufiyan Ghori Jan 07 '15 at 10:44
  • 1
    `locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);` Where `5000` is an interval when `locationListener` function is executed. I think your answer is good, but it doesn't solve the problem. GPS has a network state listener and I think that should be used, but I can't test that either. – George I. Jan 07 '15 at 14:15

2 Answers2

2

You can use ConnectivityManager to access System services and then NetworkInfo class to get your network's connectivity status,

method to check if Network is available,

private boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()) {
            isAvailable = true;
        }
        return isAvailable;
    }

If network is available and is connected, then only continue the execution of app. Otherwise exit the app, showing proper error message.

    // Check if Internet is connected
    if (isNetworkAvailable()) {
                  // Execution here
        }

    else {
        // Error message here if network is unavailable.
        Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
    }

don't forget to give permission to your app to access network state,

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Update:

"GPS has a network state listener and I think that should be used, but I can't test that either."

You can check your Network status from LocationManager this way,

// Declaring a Location Manager
protected LocationManager mLocationManager;
boolean isNetworkEnabled = false;
boolean isGPSEnabled = false;

// Getting network status
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

If you want to get the GPS status,

// Getting GPS status
isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
  • The issue is raised **after** the event listener is initiated. It happens like this: the GPS starts checking device's location and works fine. After some point, the network fails - no mobile network, no GPS, no wi-fi. – George I. Jan 09 '15 at 10:07
  • what are you using `GPS` or `Network` for `LocationManager` ? you may try using if/else block to check if `GPS` is connected and if `GPS` is not connected then try to connect with the `Network`, if both fails then show some error dialog. Whats the problem with that ? – Sufiyan Ghori Jan 09 '15 at 10:10
0

you can handle the IOException & UnknownHostException

try
{

 ------------code------------

}
catch(IOException e)
{
 e.printStackTrace();
}
catch(UnknownHostException e)
{
 e.printStackTrace();
}
Elango
  • 412
  • 4
  • 24