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 ...
随机推荐
- Magento在IE下登陆不了后台,在Firefox下正常
目前的解决办法如下: 方法一,用FF登陆后台,在 System—Configuration-Web-Session Cookie management....timeout 改为:86400 方法二: ...
- Http的请求的全过程
http请求的详细过程 HTTP是一个应用层的协议,在这个层的协议,是一种网络交互需要遵守的一种协议规范. 1.连接:当输入一个请求时,首先建立一个socket连接,因为socket是通过ip和端口建 ...
- wireshark使用详解
编号:1009时间:2016年4月29日15:52:44功能:wireshark使用详解URl:http://blog.jobbole.com/70907/URL:http://www.9upk.co ...
- 【Sublime Text 3】插件
TrailingSpacer 高亮显示多余的空格和Tab HTML-CSS-JS Prettify
- mark资料-selenium断言的分类
操作(action).辅助(accessors)和断言(assertion): 操作action: 模拟用户与 Web 应用程序的交互. 辅助accessors: 这是辅助工具.用于检查应用程序的状态 ...
- MySQL数据库恢复的经历。
蛋疼,定时任务设置错误.把数据给删除了.还有一次是服务器时间不对,也把数据给删除了. 还好,开启了二进制日志,才算把数据找回,但是速度效率也太低. 痛定思变.在把一切交由电脑工作的时候,也要做好一定的 ...
- Linux 线程--那一年, 我们一起忽视的pthread_join
前言: 通过linux的pthread库, 相信大家对创建/销毁线程肯定很熟悉, 不过对pthread_join是否知道的更多呢?实验: 先编写一个常规的程序 #include <pthread ...
- Codeforces Round #303 (Div. 2) C dp 贪心
C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- spring-AOP-添加日志
1 把一个类声明为一个切面:①需要把该类放入到IOC中,②再声明为一个切面(@Aspect @Component)@Order(1):指定顺序 2 在配置文件中添加如下配置:<aop:aspec ...
- scala言语基础学习八