Prevent app from crash when network unavailable

拜拜、爱过 提交于 2019-12-08 07:22:44

问题


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


回答1:


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);



回答2:


you can handle the IOException & UnknownHostException

try
{

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

}
catch(IOException e)
{
 e.printStackTrace();
}
catch(UnknownHostException e)
{
 e.printStackTrace();
}


来源:https://stackoverflow.com/questions/27796001/prevent-app-from-crash-when-network-unavailable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!