Android连接网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置。

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

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

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

  1. /**
  2. * 对网络连接状态进行判断
  3. * @return  true, 可用; false, 不可用
  4. */
  5. private boolean isOpenNetwork() {
  6. ConnectivityManager connManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  7. if(connManager.getActiveNetworkInfo() != null) {
  8. return connManager.getActiveNetworkInfo().isAvailable();
  9. }
  10. return false;
  11. }

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

  1. /**
  2. * 访问百度主页,网络不可用则需设置
  3. */
  4. private void initMoreGames() {
  5. String URL_MOREGAMES = "http://www.baidu.com";
  6. mWebView = (WebView) findViewById(R.id.view_gamesort);
  7. if (mWebView != null) {
  8. mWebView.requestFocus();
  9. WebSettings webSettings = mWebView.getSettings();
  10. if (webSettings != null) {
  11. webSettings.setJavaScriptEnabled(true);
  12. webSettings.setCacheMode(MODE_PRIVATE);
  13. webSettings.setDefaultTextEncodingName("utf-8");
  14. }
  15. // 判断网络是否可用
  16. if(isOpenNetwork() == true) {
  17. mWebView.loadUrl(URL_MOREGAMES);
  18. } else {
  19. AlertDialog.Builder builder = new AlertDialog.Builder(MoreGamesActivity.this);
  20. builder.setTitle("没有可用的网络").setMessage("是否对网络进行设置?");
  21. builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
  22. @Override
  23. public void onClick(DialogInterface dialog, int which) {
  24. Intent intent = null;
  25. try {
  26. String sdkVersion = android.os.Build.VERSION.SDK;
  27. if(Integer.valueOf(sdkVersion) > 10) {
  28. intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
  29. }else {
  30. intent = new Intent();
  31. ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
  32. intent.setComponent(comp);
  33. intent.setAction("android.intent.action.VIEW");
  34. }
  35. MoreGamesActivity.this.startActivity(intent);
  36. } catch (Exception e) {
  37. Log.w(TAG, "open network settings failed, please check...");
  38. e.printStackTrace();
  39. }
  40. }
  41. }).setNegativeButton("否", new DialogInterface.OnClickListener() {
  42. @Override
  43. public void onClick(DialogInterface dialog, int which) {
  44. dialog.cancel();
  45. finish();
  46. }
  47. }).show();
  48. }
  49. } else {
  50. Log.w(TAG, "mWebView is null, please check...");
  51. }
  52. }

运行界面:

Android 检测网络连接状态的更多相关文章

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

    网络的时候,并不是每次都能连接到网络,因此在程序启动中需要对网络的状态进行判断,如果没有网络则提醒用户进行设置   Android连接首先,要判断网络状态,需要有相应的权限,下面为权限代码(Andro ...

  2. Android检测网络连接

    Android检测网络连接 import android.app.AlertDialog; import android.content.Context; import android.content ...

  3. Delphi检测网络连接状态

    有时候,我们做一些小软件就需要检测网络连接状态,比如想给你的软件加上类似QQ那样的系统消息,可是像我这样的穷人肯定是买不起服务器了,那我们只好另想办法,可以读取网页然后用浏览器显示,这个时候就需要判断 ...

  4. iOS开发 - Swift实现检测网络连接状态及网络类型

    一.前言 在移动开发中,检测网络的连接状态尤其检测网络的类型尤为重要.本文将介绍在iOS开发中,如何使用Swift检测网络连接状态及网络类型(移动网络.Wifi). 二.如何实现 Reachabili ...

  5. [Swift通天遁地]四、网络和线程-(6)检测网络连接状态

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. android 检查网络连接状态实现步骤

    获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission android:name="android.permission.AC ...

  7. 我的Android进阶之旅------>Android检测wifi连接状态

    今天要实现监听系统Wifi连接状态,下面代码简化后提取出来的,以备后用. step1. 编写BroadcastReceiver import android.content.BroadcastRece ...

  8. iOS检测网络连接状态

    官方Demo下载地址:https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip 将Reachab ...

  9. android检查网络连接状态的变化,无网络时跳转到设置界面

    在AndroidManifest.xml中加一个声明<receiver android:name="NetCheckReceiver"> <intent-filt ...

随机推荐

  1. Magento在IE下登陆不了后台,在Firefox下正常

    目前的解决办法如下: 方法一,用FF登陆后台,在 System—Configuration-Web-Session Cookie management....timeout 改为:86400 方法二: ...

  2. Http的请求的全过程

    http请求的详细过程 HTTP是一个应用层的协议,在这个层的协议,是一种网络交互需要遵守的一种协议规范. 1.连接:当输入一个请求时,首先建立一个socket连接,因为socket是通过ip和端口建 ...

  3. wireshark使用详解

    编号:1009时间:2016年4月29日15:52:44功能:wireshark使用详解URl:http://blog.jobbole.com/70907/URL:http://www.9upk.co ...

  4. 【Sublime Text 3】插件

    TrailingSpacer 高亮显示多余的空格和Tab HTML-CSS-JS Prettify

  5. mark资料-selenium断言的分类

    操作(action).辅助(accessors)和断言(assertion): 操作action: 模拟用户与 Web 应用程序的交互. 辅助accessors: 这是辅助工具.用于检查应用程序的状态 ...

  6. MySQL数据库恢复的经历。

    蛋疼,定时任务设置错误.把数据给删除了.还有一次是服务器时间不对,也把数据给删除了. 还好,开启了二进制日志,才算把数据找回,但是速度效率也太低. 痛定思变.在把一切交由电脑工作的时候,也要做好一定的 ...

  7. Linux 线程--那一年, 我们一起忽视的pthread_join

    前言: 通过linux的pthread库, 相信大家对创建/销毁线程肯定很熟悉, 不过对pthread_join是否知道的更多呢?实验: 先编写一个常规的程序 #include <pthread ...

  8. Codeforces Round #303 (Div. 2) C dp 贪心

    C. Woodcutters time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. spring-AOP-添加日志

    1 把一个类声明为一个切面:①需要把该类放入到IOC中,②再声明为一个切面(@Aspect @Component)@Order(1):指定顺序 2 在配置文件中添加如下配置:<aop:aspec ...

  10. scala言语基础学习八