Android的定期检查Internet连接 [英] Android regular check for internet connection

查看:103
本文介绍了Android的定期检查Internet连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行一个Android应用程序,在特定的URL显示的WebView,我要检查,如果应用服务器我访问是活的,我可以查看HTML,如果失败,我应终止web视图,这个检查应是每隔10秒(或其他),究竟是什么,最好的方法?

I'm running an android app that displays a webview on specific URL, I want to check if the application server I'm accessing is alive and that I can view HTML, if failed I should terminate the webview, this check should be every 10 seconds (or whatever), what is the best approach for this ?

推荐答案

创建定时器 (在这里你有资料),当计时器滴答,平任何主机:

Create Timer (here you have information) and when timer tick, ping any host :

 URL url = new URL("http://microsoft.com");

    HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
    urlc.setRequestProperty("User-Agent", "Android Application:"+Z.APP_VERSION);
    urlc.setRequestProperty("Connection", "close");
    urlc.setConnectTimeout(1000 * 30); // mTimeout is in seconds
            urlc.connect();
    if (urlc.getResponseCode() == 200) {
        Main.Log("getResponseCode == 200");
            return new Boolean(true);
    }
    } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
} catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    } 

如果还真:连接处于活动状态。否则无效。

If return true : connection is active. Else isn't active.

这篇关于Android的定期检查Internet连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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