Android如何获取网络连接状态(3G/Wifi)及怎样调用网络配置界面

    1. 配置AndroidManifest.xml中的权限

      添加:

      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

      注:这是Android的安全机制.
    2. 调用到ConnectivityManager 类
      public void testConnectivityManager()
                {
                        ConnectivityManager connManager = (ConnectivityManager) this
                                        .getSystemService(CONNECTIVITY_SERVICE);
                        // 获取代表联网状态的NetWorkInfo对象
                        NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
                        // 获取当前的网络连接是否可用
                        if (null == networkInfo)
                        {
                                Toast.makeText(this, "当前的网络连接不可用", Toast.LENGTH_SHORT).show();
                                //当网络不可用时,跳转到网络设置页面
                                startActivityForResult(new Intent(
                                                android.provider.Settings.ACTION_WIRELESS_SETTINGS), 1);
      
                        } else
                        {
                                boolean available = networkInfo.isAvailable();
                                if (available)
                                {
                                        Log.i("通知", "当前的网络连接可用");
                                        Toast.makeText(this, "当前的网络连接可用", Toast.LENGTH_SHORT).show();
                                } else
                                {
                                        Log.i("通知", "当前的网络连接不可用");
                                        Toast.makeText(this, "当前的网络连接不可用", Toast.LENGTH_SHORT).show();
                                }
                        }
      
                        State state = connManager.getNetworkInfo(
                                        ConnectivityManager.TYPE_MOBILE).getState();
                        if (State.CONNECTED == state)
                        {
                                Log.i("通知", "GPRS网络已连接");
                                Toast.makeText(this, "GPRS网络已连接", Toast.LENGTH_SHORT).show();
                        }
      
                        state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                                        .getState();
                        if (State.CONNECTED == state)
                        {
                                Log.i("通知", "WIFI网络已连接");
                                Toast.makeText(this, "WIFI网络已连接", Toast.LENGTH_SHORT).show();
                        }
      
                        //// 跳转到无线网络设置界面
                        // startActivity(new
                        // Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                        //// 跳转到无限wifi网络设置界面
                        // startActivity(new
                        // Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
      
                }

      也可以:

      private void testConnectivityManager()
          {
              ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      
              //mobile 3G Network
              State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
              Log.d(mobile.toString());
              //wifi Network
              State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
              Log.d(wifi.toString());
              
              //如果3G网络和wifi网络都未连接,且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接
              if(mobile==State.CONNECTED||mobile==State.CONNECTING)
                  return;
              if(wifi==State.CONNECTED||wifi==State.CONNECTING)
                  return;
              
              startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面
              //startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //进入手机中的wifi网络设置界面
              
          }

      转 : http://www.cnblogs.com/rockdean/articles/2259806.html

posted @ 2013-04-19 13:58  编程小爬虫  阅读(304)  评论(0编辑  收藏  举报