处理与PhoneGap的Andr​​oid上没有互联网连接 [英] handling no internet connection with PhoneGap on Android

查看:158
本文介绍了处理与PhoneGap的Andr​​oid上没有互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来检测用户是否拥有自己的Andr​​oid设备上的网络连接。如果他们没有互联网,我希望能够证明上有一个按钮,上面写着重试的景色,如一个谷歌Play应用商店。我检测使用的是原生Android的WebView使用下面的code之前没有信号,但是我现在想找到一种方法,这样做同样的事情的PhoneGap。此外,如果任何人都可以提供关于如何重新加载网页一些示例code,这将是极好的!

  webview.setWebViewClient(新WebViewClient(){
        公共无效onReceivedError(的WebView观点,诠释错误code,描述字符串,字符串failingUrl){
            意图myIntent =新意图(view.getContext(),LoadScreen.class);            startActivity(myIntent);
            完();
        }
    });


解决方案

通过以下code解决了这个:

 公共布尔isOnline(){
    ConnectivityManager厘米=
        (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetInfo的的NetworkInfo = cm.getActiveNetworkInfo();
    如果(NetInfo的= NULL&放大器;!&安培; netInfo.isConnectedOrConnecting()){
        返回true;
    }
    返回false;
}

I'm trying to detect whether or not a user has internet connection on their android device. If they don't have internet, I want to be able to show a view that has a button on it that said "retry", like that of the Google Play app store. I was detecting no signal using a native android webview before using the code below, but I'd now like to find a way to do this same thing with PhoneGap. Also, if anybody could provide some sample code on how to re-load the webpage, that would be excellent!

    webview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Intent myIntent = new Intent(view.getContext(), LoadScreen.class);

            startActivity(myIntent);
            finish();
        }
    });

解决方案

Solved this by using the following code:

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

这篇关于处理与PhoneGap的Andr​​oid上没有互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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