权限:

<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. EasyUI - NumberBox组件

    效果: html代码: <input type ="text" id ="box"/> JS代码: $(function () { $('#box' ...

  2. Git flow 的流程

    Git flow 的流程与参考   Git flow 出自 A successful Git branching model,这里使用了一个前端项目配合本文稿实施了 git flow 并记录流程作出示 ...

  3. Spring MVC 的json问题(406 Not Acceptable)

    原因 : 就是程序转换JSON失败. 在pom.xml 加上 <dependency> <groupId>com.fasterxml.jackson.core</grou ...

  4. 基于visual Studio2013解决面试题之0510连续数之和

     题目

  5. 配置VS2008下的Qt开发环境有感

    写一篇小小的日志为了在VS2008中安装Qt的插件,花了我很多的时间.1.vs2008在win7中破解问题我的VS2008已经安装好了,不知道为何,当初没有破解,现在只剩下15天限制了.于是为了破解, ...

  6. JS - 提示是否删除

    1. OnClientClick="return confirm('确定要删除吗?') 2.自定义函数: 函数: <script type ="text/javascript ...

  7. ASP.NET、HTML+CSS - 弹出提示窗体

    刷新数据,提示之后,CSS样式改变: 解决方案: 在ASP.NET中,如果是添加信息成功之后出现提示信息,那么只能用  ClientScript.RegisterStartupScript(this. ...

  8. ALV 数值列负号前置 (EDIT_MASK应用)

    1.建立自定义函数 浮点数显示FUNCTION conversion_exit_zsign_output.*"---------------------------------------- ...

  9. Java 多线程 (并发)总结

    一.概念 1. 维基百科解释 进程是什么? http://zh.wikipedia.org/wiki/%E8%BF%9B%E7%A8%8B 线程是什么? http://zh.wikipedia.org ...

  10. TComponent,TControl,TWinControl,TGraphic的DefineProperties赏析与说明(不懂)

    先观赏一下最后的实现效果: object Form1: TForm1 Left = Top = Width = Height = Caption = 'Form1' Color = clBtnFace ...