简体   繁体   English

防止网络不可用时应用崩溃

[英]Prevent app from crash when network unavailable

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. 该服务每秒钟调用一个用作GPS连接的类。 The GPS class uses NETWORK_PROVIDER and GPS_PROVIDER . GPS类使用NETWORK_PROVIDERGPS_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". 有时,Android系统会显示一条消息,提示“除非您连接到wifi网络或移动网络,否则您将无法访问电子邮件和互联网”。 I don't remember well the message, but I had 2 buttons: "Cancel" and "Ok". 我不太记得该消息,但是我有2个按钮:“取消”和“确定”。 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 谢谢

You can use ConnectivityManager to access System services and then NetworkInfo class to get your network's connectivity status, 您可以使用ConnectivityManager来访问系统服务,然后使用NetworkInfo类来获取网络的连接状态,

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." “ GPS有一个网络状态侦听器,我认为应该使用它,但是我也不能对此进行测试。”

You can check your Network status from LocationManager this way, 您可以通过LocationManager检查网络状态,

// 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, 如果您想获取GPS状态,

// Getting GPS status
isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

you can handle the IOException & UnknownHostException 您可以处理IOExceptionUnknownHostException

try
{

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM