权限:

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

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

代码例如以下:

package com.example.nettest;



import android.net.ConnectivityManager;

import android.net.NetworkInfo.State;

import android.os.Bundle;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.ComponentName;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.view.Menu;

import android.widget.TextView;

import android.widget.Toast;





public class MainActivity extends Activity {





private ConnectivityManager manager;

private TextView tv;

StringBuffer sb = new StringBuffer(256);





@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);





tv = (TextView) findViewById(R.id.textView1);

checkNetworkState();

}





/**

* 检測网络是否连接



* @return

*/

private boolean checkNetworkState() {

boolean flag = false;

// 得到网络连接信息

manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

// 去进行推断网络是否连接

if (manager.getActiveNetworkInfo() != null) {

flag = manager.getActiveNetworkInfo().isAvailable();

}

if (!flag) {

setNetwork();

} else {

isNetworkAvailable();

}

tv.setText(sb.toString());

return flag;

}





/**

* 网络未连接时。调用设置方法

*/

private void setNetwork() {

Toast.makeText(this, "wifi is closed!", Toast.LENGTH_SHORT).show();

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle("网络提示信息");

builder.setMessage("网络不可用。假设继续,请先设置网络!

");

builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Intent intent = null;

/**

* 推断手机系统的版本号!

假设API大于10 就是3.0+ 由于3.0以上的版本号的设置和3.0下面的设置不一样。调用的方法不同

*/

if (android.os.Build.VERSION.SDK_INT > 10) {

intent = new Intent(

android.provider.Settings.ACTION_SETTINGS);

} else {

intent = new Intent();

ComponentName component = new ComponentName(

"com.android.settings",

"com.android.settings.WirelessSettings");

intent.setComponent(component);

intent.setAction("android.intent.action.VIEW");

}

startActivity(intent);

}

});





builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

});

builder.create();

builder.show();

}





/**

* 网络已经连接,然后去推断是wifi连接还是GPRS连接 设置一些自己的逻辑调用

*/

private void isNetworkAvailable() {





State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)

.getState();

State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)

.getState();

if (gprs == State.CONNECTED || gprs == State.CONNECTING) {

Toast.makeText(this, "gprs is open! ", Toast.LENGTH_SHORT).show();

sb.append("\ngprs is open! ");

} else {

sb.append("\ngprs is closed! ");

}





// 推断为wifi状态下才载入广告,假设是GPRS手机网络则不载入。

if (wifi == State.CONNECTED || wifi == State.CONNECTING) {

Toast.makeText(this, "wifi is open! ", Toast.LENGTH_SHORT).show();

loadAdmob();

sb.append("\nwifi is open! ");

} else {

sb.append("\nwifi is closed! ");

}





}





/**

* 在wifi状态下 载入admob广告

*/

private void loadAdmob() {

Toast.makeText(getApplicationContext(), "ad is loding..", 1).show();

sb.append("\nad is loding...");

}

}

Android 检測网络是否连接的更多相关文章

  1. javaScript 检測 能否够连接指定server

    本文主要讲述怎样在js里面从多个服务端servlet中找到可用的地址 一般项目部署到生产环境之后会有一个生产网段(就是你能够在家訪问)和一个办公网段(仅仅能在公司才干訪问). 我们的项目部署好了之后, ...

  2. android 检測右滑的WebView

    今天产品出新花样非得要右滑....检測到右滑手势后事件不做处理放在Activity中做对应的处理即可了. import android.app.Activity; import android.con ...

  3. Android中判断网络是否连接并提示设置

    /** * 判断网络是否连通 * @param context * @return */ public static boolean isNetworkConnected(Context contex ...

  4. AFN检測网络情况

    问: I'm a bit lost on AFNetorking's Reachability and haven't found a lot of good information out ther ...

  5. Android实践--监測网络状态

    Android 监測网络状态      我们在使用Android手机时候,一些APP须要网络环境才干执行,所以手机须要可用的网络,无论是2G.3G或者WIFI.甚至有一些比較耗流量的APP仅仅能在WI ...

  6. iOS开发实践之网络检測Reachability

    在网络应用开发中.有时须要对用户设备的网络状态进行实时监控.以至于对用户进行友好提示 或者依据不同网络状态处理不一样的逻辑(如视频播放app,依据当前的网络情况自己主动切换视频清晰度等等).用Reac ...

  7. 检測wifi是否须要portal验证 公共场所wifi验证

    何为wifi portal验证? 平时在商场,咖啡厅,银行等公共场所.我们手机提示:有可用WLAN.这些WIFI能够直接连接,不须要password,但须要我们手动在手机网页上进行验证,通常是输入一个 ...

  8. android之检測是否有网络

    主要是用来检測是否有网络,假设没有,就去wifi里面去进行设置网络... 以下贴一下主要代码: private void checkNetWorkInfo() { if (!Tools.isNetwo ...

  9. android 检测网络是否连接,或者GPS是否可用

    很多android程序在打开时,检测网络是否连接,或者GPS是否可用: 1.网络是否连接(包括Wifi和移动网络) // 是否有可用网络 private boolean isNetworkConnec ...

随机推荐

  1. svn 标示提示

    原来没有遇到过, 突然发现这次写的项目有几个文件时 这个"表示的" ,死活找不到原因,并且提交,改动 都好烦人,还要锁定什么嘛的. 最后最终知道, 这个意思是  文件的状态为 &q ...

  2. 编写高质量代码改善java程序的151个建议——[52-57]String !about String How to use them?

    原创地址:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),须要转载的,保留下! Thanks Although the world is full of s ...

  3. [cocos2d-x]HelloWorldDemo

    实现一个demo,具备以下功能: 1.让几个字分别位于中间和四个角落. 2.中间的字体改变,并且带有闪烁功能. 3.单点触摸和多点触摸,并且能够实现滑动效果,滑动的话必须使用带有bool返回值的单点触 ...

  4. VHDL TestBench 测试终止时自动结束仿真——assert方法

    可在结束仿真位置添加如下代码: assert false report "Simulation is finished!" severity Failure; 则在Modelsim ...

  5. 杭电OJ_DIY_YTW2_1001 A Mathematical Curiosity

    Problem Description Given two integers n and m, count the number of pairs of integers (a,b) such tha ...

  6. 电驴 emule 源代码分析 (1)

    关于电驴emule 的源代码,网上有一个  叫刘刚的人 分析的 非常多,可是假设你仅仅是看别人的分析,自己没有亲身去阅读代码的话,恐怕非常难  剖析整个系统. 关于emule  主要就是 连接 kad ...

  7. Ubuntu 环境安装整理

    Ubuntu11.04下Java开发环境搭建和配置 转自:http://guoyunsky.iteye.com/blog/1175861 类似的搭建,网上一搜一大把,但每次去搜索比较麻烦.我这里就整理 ...

  8. 批处理运行python

    @echo off cd C:\test start python test.py start python test2.py exit

  9. 自绘XP风格菜单

    这是以前写的代码,自绘XP风格的菜单,硬盘坏了后以为没了,最后写的一个软件要自定义风格,“翻箱倒柜”的终于在我可爱的古董机^_^上找到了一个应用的例子.还是把它放到Blog上来,即可共享又可作为备用 ...

  10. SilkTest Q&A 7

    Q61.有一个用Dotnet开发的应用,有1000个为测它而录制的case,一直都运行的很正常,直到有一天… 有人改变了该应用命名空间,由于现在有一个新的window或是panel出现,所以测试脚本一 ...