欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android关于网络 博客分类: android androidnetwork网络设置 

程序员文章站 2024-03-14 22:35:47
...
/**
  * 检测当前网络状态是否可用
  * 
  * @return
  */
 public boolean checkNet() {
  ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
    .getState();
  State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
    .getState();
  // 移动网络
  if (mobile == State.CONNECTED || mobile == State.CONNECTING)
   return true;
  // wifi网络
  if (wifi == State.CONNECTED || wifi == State.CONNECTING)
   return true;
  return false;
 }

 @Override
 protected void onResume() {

  super.onResume();
  // 3秒后界面消失

 }
 /**
  * 网络不可用  进行网络设置
  */
 private void netWorkStatus() {
  AlertDialog.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); // 如果在设置完成后需要再次进行操作,可以重写操作代码,在这里不再重写
    finish();
   }
  }).setNeutralButton("否", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
    dialog.cancel();
    System.exit(0);
   }
  }).show();

 }