android检测网络连接状态示例讲解
Android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(AndroidManifest.xml):
<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连接网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置. 首先,要判断网络状态,需要有相应的权限,下面为权限代码(Androi ...
- iOS开发 - Swift实现检测网络连接状态及网络类型
一.前言 在移动开发中,检测网络的连接状态尤其检测网络的类型尤为重要.本文将介绍在iOS开发中,如何使用Swift检测网络连接状态及网络类型(移动网络.Wifi). 二.如何实现 Reachabili ...
- Android检测网络连接
Android检测网络连接 import android.app.AlertDialog; import android.content.Context; import android.content ...
- Delphi检测网络连接状态
有时候,我们做一些小软件就需要检测网络连接状态,比如想给你的软件加上类似QQ那样的系统消息,可是像我这样的穷人肯定是买不起服务器了,那我们只好另想办法,可以读取网页然后用浏览器显示,这个时候就需要判断 ...
- [Swift通天遁地]四、网络和线程-(6)检测网络连接状态
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- android 检查网络连接状态实现步骤
获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission android:name="android.permission.AC ...
- c#判断网络连接状态示例代码
使用c#判断网络连接状态的代码. 代码: public partial class Form1 : Form { [DllImport() == true) { label1.Text = " ...
- 我的Android进阶之旅------>Android检测wifi连接状态
今天要实现监听系统Wifi连接状态,下面代码简化后提取出来的,以备后用. step1. 编写BroadcastReceiver import android.content.BroadcastRece ...
- iOS检测网络连接状态
官方Demo下载地址:https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 将Reachab ...
随机推荐
- Cesium随笔(3)随鼠标实时显示经纬度坐标以及高度【转】
在网页三维地球上进行可视化开发与经纬度坐标以及高度是分不开的,能够实时获取鼠标位置的经纬度对可视化效果有很好的帮助,Cesium当然能做到: (1)首先在里创建显示坐标的容器 样式自己调整的合适即可 ...
- 前端要给力之:URL应该有多长?
URL到底应该有多长?我为什么要提这个问题呢?有许多优化指南里都写着:要尽量减小COOKIE.缩短URL,以及尽可能地使用GET请求等等,以便优化WEB页面的请求和装载.但是,这种所谓“尽可能”.“尽 ...
- Linux经常使用命令(三) - pwd
Linux中用 pwd 命令来查看"当前工作文件夹"的完整路径. 简单得说,每当你在终端进行操作时.你都会有一个当前工作文件夹. 在不太确定当前位置时.就会使用pwd来判定当前文件 ...
- lua接收图片并进行md5处理
需要luacurl(http://luacurl.luaforge.net/)和MD5两个库函数 curl = require("luacurl") require("m ...
- PHP经典项目案例-(一)博客管理系统5
本篇实现发表博客. 八.发表博客 (1).界面实现file.php <tr> <td colSpan=3 valign="baseline" style ...
- 安装apache+php记录
安装apache yum install httpd 修改apache配置文件,可以修改apache的默认端口号,根目录等 /etc/httpd/conf/httpd.conf 启动/重启apache ...
- MVC 之 属性详解
一.System [AttributeUsage]:指定另一特性类的用法.无法继承此类. [CLSCompliant]:指示程序元素是否符合公共语言规范 (CLS).无法继承此类. [ContextS ...
- vim配置 高亮+自动缩进+行号+折叠+优化
一:修改 .vimrc即可 二: set nocompatible " 关闭 vi 兼容模式syntax on " 自动语法高亮colorscheme molokai " ...
- Menubar
A menubar is a common part of a GUI application. It is a group of commands located in various menus. ...
- eclipse no java machine vitual was found
eclipse no java machine vitual was found CreateTime--2018年4月27日10:41:20 Author:Marydon 1.错误提示 2.问题 ...