<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
private boolean NetWorkStatus() { boolean netSataus = false; ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); cwjManager.getActiveNetworkInfo(); if (cwjManager.getActiveNetworkInfo() != null) { netSataus = cwjManager.getActiveNetworkInfo().isAvailable(); } if (netSataus) { Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络") .setMessage("是否对网络进行设置?"); b.setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Intent mIntent = new Intent("/"); ComponentName comp = new ComponentName( "com.android.settings", "com.android.settings.WirelessSettings"); mIntent.setComponent(comp); mIntent.setAction("android.intent.action.VIEW"); startActivityForResult(mIntent,0); // 如果在设置完成后需要再次进行操作,可以重写操作代码,在这里不再重写 } }).setNeutralButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }).show(); } return netSataus; }
通过上面的代码即可完成对网络状态的判断!
该日志标签: 互联网, Android, 检查网络
上一篇: Android Wifi简单管理与操作
下一篇: Android 使用三种方式获取网页(通过Post,Get进行表单的提交)
仅有一条评论 »
pan
November 15th, 2011 at 01:36 pm
以上代码在2.X系统下运行没问题,但是在3.X或者4.0下运行崩溃
添加新评论 »
称呼
网站(选填)
- package com.dx;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.NetworkInfo.State;
- import android.os.Bundle;
- import android.widget.TextView;
- public class Main extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // setContentView(R.layout.main);
- TextView textView = new TextView(this);
- textView.setText("网络检测");
- setContentView(textView);
- if(checkNetWorkInfo()){
- goToNetWork();
- };
- }
- private boolean goToNetWork() {
- // TODO Auto-generated method stub
- ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = connectivityManager.getActiveNetworkInfo();
- if(info == null || !info.isAvailable()){
- new AlertDialog.Builder(this).setMessage("没有可以使用的网络").setPositiveButton("Ok", null).show();
- return false;
- }
- else{
- new AlertDialog.Builder(this).setMessage("网络正常可以使用").setPositiveButton("Ok", null).show();
- return true;
- }
- }
- private boolean checkNetWorkInfo() {
- // TODO Auto-generated method stub
- ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
- if(wifi != null){
- new AlertDialog.Builder(this).setMessage(wifi.toString()).setPositiveButton("wifi", null).show();//显示wifi网络连接状态
- return true;
- }else{
- State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
- if(mobile != null){
- new AlertDialog.Builder(this).setMessage(mobile.toString()).setPositiveButton("3G", null).show();//显示3G网络连接状态
- return true;
- }
- }
- return false;
- }
- }