欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android检测网络连接状态示例讲解

程序员文章站 2022-06-10 19:12:38
android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(androidmanifest.xml):复制代码 代码如下:

android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(androidmanifest.xml):

复制代码 代码如下:

<uses-permission android:name="android.permission.access_network_state"/>
<uses-permission android:name="android.permission.internet"/>

然后,检测网络状态是否可用

复制代码 代码如下:

/**
 * 对网络连接状态进行判断
 * @return  true, 可用; false, 不可用
 */ 
private boolean isopennetwork() { 
    connectivitymanager connmanager = (connectivitymanager)getsystemservice(context.connectivity_service); 
    if(connmanager.getactivenetworkinfo() != null) { 
        return connmanager.getactivenetworkinfo().isavailable(); 
    } 

    return false; 

最后,不可用则打开网络设置

复制代码 代码如下:

/**
 * 访问百度主页,网络不可用则需设置
 */ 
private void initmoregames() { 
    string url_moregames = "http://www.baidu.com"; 
    mwebview = (webview) findviewbyid(r.id.view_gamesort); 

    if (mwebview != null) { 
        mwebview.requestfocus(); 
        websettings websettings = mwebview.getsettings(); 
        if (websettings != null) { 
            websettings.setjavascriptenabled(true); 
            websettings.setcachemode(mode_private); 
            websettings.setdefaulttextencodingname("utf-8"); 
        } 

        // 判断网络是否可用 
        if(isopennetwork() == true) { 
            mwebview.loadurl(url_moregames); 
        } else { 
            alertdialog.builder builder = new alertdialog.builder(moregamesactivity.this); 
            builder.settitle("没有可用的网络").setmessage("是否对网络进行设置?"); 

            builder.setpositivebutton("是", new dialoginterface.onclicklistener() { 
                @override 
                public void onclick(dialoginterface dialog, int which) { 
                    intent intent = null; 

                    try { 
                        string sdkversion = android.os.build.version.sdk; 
                        if(integer.valueof(sdkversion) > 10) { 
                            intent = new intent(android.provider.settings.action_wireless_settings); 
                        }else { 
                            intent = new intent(); 
                            componentname comp = new componentname("com.android.settings", "com.android.settings.wirelesssettings"); 
                            intent.setcomponent(comp); 
                            intent.setaction("android.intent.action.view"); 
                        } 
                        moregamesactivity.this.startactivity(intent); 
                    } catch (exception e) { 
                        log.w(tag, "open network settings failed, please check..."); 
                        e.printstacktrace(); 
                    } 
                } 
            }).setnegativebutton("否", new dialoginterface.onclicklistener() { 
                @override 
                public void onclick(dialoginterface dialog, int which) { 
                    dialog.cancel();         
                    finish(); 
                } 
            }).show(); 
        } 
    } else { 
        log.w(tag, "mwebview is null, please check..."); 
    } 
}