Android 判断网络状态,在没有网络的时候,打开网络设置对话框

private LinearLayout linear1,linear2;
 private Button btn1,btn2;

   linear1=(LinearLayout)findViewById(R.id.linearLayout1);
   //activity启动时隐藏
   linear1.setVisibility(View.GONE);
  
   linear2=(LinearLayout)findViewById(R.id.linearLayout2);
   linear2.setVisibility(View.GONE);
  
   btn1=(Button)findViewById(R.id.btn_login);
   btn2=(Button)findViewById(R.id.btn_exit);
   btn1.setOnClickListener(new btn1Onclick());
   btn2.setOnClickListener(new btn2Onclick());

 

------------------------------------------------------------------>>>>>>>>

//打开网络连接设置
 class btn1Onclick implements OnClickListener{
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   NetWorkStatus();
  }
 }
 //退出
 class btn2Onclick implements OnClickListener{
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   System.exit(0);
  }
 }

 


public void Connect(){
  Context contexts = MainActivity.this.getApplicationContext();
  // 得到本Activity
  if (!isConnect(contexts)) {
   // 如果网络不好使给出提示
   //isConnect是自定义的检测网络状态的函数
   Toast.makeText(MainActivity.this, "温馨提示:请检查网络连接!",
     Toast.LENGTH_SHORT).show();
   //网络没有连接时显示布局按钮
    linear1.setVisibility(View.VISIBLE);
    linear2.setVisibility(View.VISIBLE);
   return;
  }else{
    System.out.println("网络已经连接上啦!!");

  }
}
 
 public static boolean isConnect(Context context) {
  // 获取手机所有连接管理对象(包括对 Wi-Fi,net等连接的管理)
  ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  if (connectivity != null) {
   // 获取网络连接管理的对象
   NetworkInfo info = connectivity.getActiveNetworkInfo();
   if (info != null) {
    // 判断当前网络是否已经连接
    if (info.getState() == NetworkInfo.State.CONNECTED) {     
      return true;    
       
   }
  }
  return false;
 }
//打开网络设置
 private void NetWorkStatus() {
 
   ConnectivityManager connectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
   connectivity.getActiveNetworkInfo();   
  
    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); // 如果在设置完成后需要再次进行操作,可以重写操作代码,在这里不再重写
     System.exit(0);
    }
    
    }
  ).setNeutralButton("否", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
    dialog.cancel();
    System.exit(0);
    }
   }
  ).show();
 
 }

}

---------------------------------------------------------->>>>>>>>>>>>>>>>>>>>

布局文件

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/h"
    android:orientation="vertical" >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:layout_marginTop="250dip"
          >
        <Button
            android:id="@+id/btn_login"
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:background="#555555"
            android:text="@string/open" >
        </Button>
        </LinearLayout>
        <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
          >
        <Button
            android:id="@+id/btn_exit"
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:layout_marginTop="1dip"
            android:background="#555555"
            android:text="@string/exit" >
        </Button>
    </LinearLayout>

</LinearLayout> 

》》》》》》》》》》》》》》》》》》》》》》上图《《《《《《《《《《《《《《《《《《《《《《《

Android <wbr>判断网络状态,在没有网络的时候,打开网络设置对话框
QQ群:230901717



    转载:http://blog.sina.com.cn/s/blog_8be86ad301010xb6.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值