Android 检测网络连接状态
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...");
- }
- }
运行界面:

Android 检测网络连接状态的更多相关文章
- android检测网络连接状态示例讲解
网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置 Android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(Andro ...
- Android检测网络连接
Android检测网络连接 import android.app.AlertDialog; import android.content.Context; import android.content ...
- Delphi检测网络连接状态
有时候,我们做一些小软件就需要检测网络连接状态,比如想给你的软件加上类似QQ那样的系统消息,可是像我这样的穷人肯定是买不起服务器了,那我们只好另想办法,可以读取网页然后用浏览器显示,这个时候就需要判断 ...
- iOS开发 - Swift实现检测网络连接状态及网络类型
一.前言 在移动开发中,检测网络的连接状态尤其检测网络的类型尤为重要.本文将介绍在iOS开发中,如何使用Swift检测网络连接状态及网络类型(移动网络.Wifi). 二.如何实现 Reachabili ...
- [Swift通天遁地]四、网络和线程-(6)检测网络连接状态
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- android 检查网络连接状态实现步骤
获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission android:name="android.permission.AC ...
- 我的Android进阶之旅------>Android检测wifi连接状态
今天要实现监听系统Wifi连接状态,下面代码简化后提取出来的,以备后用. step1. 编写BroadcastReceiver import android.content.BroadcastRece ...
- iOS检测网络连接状态
官方Demo下载地址:https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 将Reachab ...
- android检查网络连接状态的变化,无网络时跳转到设置界面
在AndroidManifest.xml中加一个声明<receiver android:name="NetCheckReceiver"> <intent-filt ...
随机推荐
- echarts在360中以及IE8浏览器不兼容:解决方案
参考:http://blog.csdn.net/www3300300/article/details/12992489 添加: <head> <meta http-equiv=&qu ...
- nginx php-cgi php
/*************************************************************************** * nginx php-cgi php * 说 ...
- 非常基础的css注意点
排版了很多页面,才发现自己忽视了一个很基本且重要的知识点: 一个div在一般的浏览器中,算它的width,要计算其content,padding,border. 但是在CSS中定义一个div的widt ...
- 38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- name值与id值在Js获取元素时的区别
1.适用范围 除base.head.html.script.meta.title标签外,id都可以用:name只适用于select.form.frame.iframe.img.a.input等中. H ...
- HTML5 3D动画效果
对以前来讲,3D动画拿到网页上展示是一件非常奢侈的事情,第一是浏览器不够先进,第二是大部分只能用flash实现伪3D.HTML5的出现,让实现网页3D动画变得非常简单,当然前提是你不要再使用像IE67 ...
- Mybatis学习 —— 包括所有 mybatis官网
http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases
- html5表单新特性
type=range 值区域范围 默认值(0-100) type=data 选择日期 type=color value='初始值' 颜色选择器控件 type=search 搜索框效果 type=im ...
- apache开启url rewrite模块
在把服务器数据转移到本地服务器之后,本地打开首页出现排版紊乱等问题,经过大神指点说是url rewrite的问题. 本篇文章主要写怎样开启apache的url rewrite功能. 打开Apache2 ...
- IntelliJ IDEA 开发前的设置
1.IntelliJ IDEA 显示行号方法 设置方法:File->Settings->Editor->General->Appearance->Show line nu ...