네트워크 연결 여부 확인

첫 번 째 방법: 네트워크 에 연결 되 었 는 지 확인 합 니 다.
/**
     * Check Network status
     */
    public static boolean isNetworkAvailable(Context c) {
        boolean bNetwork = false;
        ConnectivityManager mConnMgr = (ConnectivityManager) c
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] nis = mConnMgr.getAllNetworkInfo();
        if (nis != null) {
            for (NetworkInfo ni : nis) {
                if (ni != null) {
                    if (ni.isConnected() == true) {
                        bNetwork = true;
                        break;
                    }
                }
            }
        }
        return bNetwork;
    }

네트워크 클릭 에 연결 되 지 않 은 후 와 이 파이 설정 인터페이스 로 바로 이동 하 는 방법:
private boolean CheckNetwork() {
        boolean flag = false;
        flag = UpdateUtils.isNetworkAvailable(mContext);
        if (!flag) {
            chNetworkDLG = new AlertDialog.Builder(this)
                    .setTitle(R.string.txt_no_net)
                    .setMessage(R.string.txt_no_net_msg)
                    .setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Intent mIntent = new Intent("/");
                            ComponentName comp = new ComponentName("com.android.settings",
                                    "com.android.settings.Settings");
                            mIntent.setComponent(comp);
                            mIntent.setAction("android.intent.action.VIEW");
                            startActivity(mIntent);
                        }
                    })
                    .setNeutralButton(R.string.button_cancel,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.cancel();
                                }
                            }).show();
        }
        Log.d(TAG, "CheckNetwork() ===> :[" + flag + "]");
        return flag;
    }

두 번 째 방법:
android       

    android         ,             ,                。android          BroadcastReceiver            ,      :

BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e(TAG, "      ");

            boolean success = false;

            //        
            ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            // State state = connManager.getActiveNetworkInfo().getState();
            State state = connManager.getNetworkInfo(
                    ConnectivityManager.TYPE_WIFI).getState(); //         
            if (State.CONNECTED == state) { //         WIFI  
                success = true;
            }
            
            state = connManager.getNetworkInfo(
                    ConnectivityManager.TYPE_MOBILE).getState(); //         
            if (State.CONNECTED != state) { //         GPRS  
                success = true;
            }
            
            if (!success) {
                Toast.makeText(LocationMapActivity.this, "         ", Toast.LENGTH_LONG).show();
            }            
            
        }

 

 Activity onCreate :

//      
        IntentFilter filter = new IntentFilter();        
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mNetworkStateReceiver, filter);
 Activity  onDestroy :'

        unregisterReceiver(mNetworkStateReceiver); //    

현재 인터넷 상태 로 돌아 가기:
public static String getNetwrokType(Context context) {
  ConnectivityManager connectivityManager = (ConnectivityManager) context
    .getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo info = connectivityManager.getActiveNetworkInfo();
  if (info != null && info.isAvailable()) {
   if (info.getTypeName().equalsIgnoreCase("wifi")) {
    return "wifi";
   } else if (info.getTypeName() != null
     && info.getTypeName().equalsIgnoreCase("mobile")
     && info.getExtraInfo() != null) {
    if (info.getExtraInfo().equalsIgnoreCase("cmwap")) {
     return "cmwap";
    } else if (info.getExtraInfo().equalsIgnoreCase("cmnet")) {
     return "cmnet";
    }else if (info.getExtraInfo().equalsIgnoreCase("3gnet")) {
    return "3gnet";
    }
   }
  }
  return "";
 } 

좋은 웹페이지 즐겨찾기