android如何设置网络 skyCc

<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下运行崩溃

添加新评论 »
 称呼

 E-mail

 网站(选填)

  1. package com.dx;   
  2.   
  3. import android.app.Activity;   
  4. import android.app.AlertDialog;   
  5. import android.content.Context;   
  6. import android.net.ConnectivityManager;   
  7. import android.net.NetworkInfo;   
  8. import android.net.NetworkInfo.State;   
  9. import android.os.Bundle;   
  10. import android.widget.TextView;   
  11.   
  12. public class Main extends Activity {   
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {   
  16.         super.onCreate(savedInstanceState);   
  17. //        setContentView(R.layout.main);   
  18.         TextView textView = new TextView(this);   
  19.         textView.setText("网络检测");   
  20.         setContentView(textView);   
  21.            
  22.         if(checkNetWorkInfo()){   
  23.             goToNetWork();   
  24.         };   
  25.     }   
  26.   
  27.     private boolean goToNetWork() {   
  28.         // TODO Auto-generated method stub   
  29.         ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);   
  30.         NetworkInfo info = connectivityManager.getActiveNetworkInfo();   
  31.         if(info == null || !info.isAvailable()){   
  32.             new AlertDialog.Builder(this).setMessage("没有可以使用的网络").setPositiveButton("Ok"null).show();       
  33.             return false;   
  34.         }   
  35.         else{   
  36.             new AlertDialog.Builder(this).setMessage("网络正常可以使用").setPositiveButton("Ok"null).show();       
  37.             return true;   
  38.         }   
  39.            
  40.            
  41.     }   
  42.   
  43.     private boolean checkNetWorkInfo() {   
  44.         // TODO Auto-generated method stub   
  45.         ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);   
  46.         State wifi  = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();   
  47.         if(wifi != null){   
  48.             new AlertDialog.Builder(this).setMessage(wifi.toString()).setPositiveButton("wifi"null).show();//显示wifi网络连接状态       
  49.             return true;   
  50.                
  51.         }else{   
  52.             State mobile  = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();   
  53.             if(mobile != null){   
  54.                 new AlertDialog.Builder(this).setMessage(mobile.toString()).setPositiveButton("3G"null).show();//显示3G网络连接状态       
  55.                 return true;   
  56.             }   
  57.         }   
  58.         return false;   
  59.            
  60.     }   
  61. }  
原文地址:https://www.cnblogs.com/cmzcheng/p/2377018.html