防止网络不可用时应用程序崩溃 [英] Prevent app from crash when network unavailable

查看:146
本文介绍了防止网络不可用时应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它具有从服务开始的前台通知。
服务在每秒钟呼叫一个充当GPS连接的类。
GPS类使用 NETWORK_PROVIDER GPS_PROVIDER



到目前为止,所有的工作都是正确的,除了今天之外,直到发生了一些事情。



在某些时候,Android系统显示一条消息,表示除非连接到wifi网络或移动网络,您将无法访问电子邮件和互联网。
我不太记得信息,但我有两个按钮:取消和确定。
我可能在将电话从口袋里拿出时点击了某些东西。



此消息的另一个消息是,我的应用程序不幸崩溃。
所以事件的顺序似乎是这样的:网络禁用(?),应用程序崩溃。

我认为我需要以某种方式处理网络错误,但我现在无言以对,因为我无法从错误中得到很多信息。



有什么想法?



感谢您可以使用 ConnectivityManager 访问系统服务,然后 NetworkInfo 类来获得您的网络连接状态,

方法检查网络是否可用, p>

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

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





$ b如果网络可用且已连接,则只能继续执行应用程序。

  //检查互联网是否已连接
if(isNetworkAvailable() ){
//在此处执行
}

else {
//如果网络不可用,则会显示错误消息。
Toast.makeText(这是网络不可用!,Toast.LENGTH_LONG).show();
}

不要忘记授权您的应用访问网络状态,

 <使用权限android:name =android.permission.ACCESS_NETWORK_STATE/> 

更新:

GPS有一个网络状态监听器,我认为应该使用,但我无法测试。

您可以通过 LocationManager 这种方式检查您的网络状态,

  //声明位置管理器
受保护的LocationManager mLocationManager;
boolean isNetworkEnabled = false;
boolean isGPSEnabled = false;

//获取网络状态
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

如果您想获取GPS状态,

  //获取GPS状态
isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


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

解决方案

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

这篇关于防止网络不可用时应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆