`
guojianhui0906
  • 浏览: 46126 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

判断Android 网络状态

阅读更多
当需要开启Activity之前需要检测网络状态的时候需要用到一下代码。最近也在做DataConn方面的内容,分享下了...
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;  
 }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics