android中檢測網絡連接狀態簡單總結

相應幾乎沒有不跟網絡打交道的android應用,那麼在實際中就需求檢測手機是否有網絡連接,甚至需要判斷是何種方式連接,這樣能給用戶帶來更好的體驗和一些使用指導,下面給出一些常用的判斷,如果要知道是否有網絡、以及是采用wifi連接的還是3G連接的,調用下面對應方法模型就OK瞭,代碼如下:

TestNetworkActivity:

package com.home.testnetwork;  
  
import java.util.List;  
  
import android.app.Activity;  
import android.content.Context;  
import android.location.LocationManager;  
import android.net.ConnectivityManager;  
import android.net.NetworkInfo;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
  
public class TestNetworkActivity extends Activity implements OnClickListener {  
    private Button checkBtn;  
    private EditText netText;  
    private EditText wifiText;  
    private EditText net3gText;  
    private EditText gpsText;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        checkBtn = (Button) findViewById(R.id.main_btn_check);  
        checkBtn.setOnClickListener(this);  
        wifiText = (EditText) findViewById(R.id.main_et_wifi);  
        net3gText = (EditText) findViewById(R.id.main_et_3g);  
        gpsText = (EditText) findViewById(R.id.main_et_GPS);  
        netText = (EditText) findViewById(R.id.main_et_net);  
    }  
  
    /** 
     * 檢測網絡是否連接 
     *  
     * @return 
     */  
    private boolean isNetConnected() {  
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
        if (cm != null) {  
            NetworkInfo[] infos = cm.getAllNetworkInfo();  
            if (infos != null) {  
                for (NetworkInfo ni : infos) {  
                    if (ni.isConnected()) {  
                        return true;  
                    }  
                }  
            }  
        }  
        return false;  
    }  
  
    /** 
     * 檢測wifi是否連接 
     *  
     * @return 
     */  
    private boolean isWifiConnected() {  
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
        if (cm != null) {  
            NetworkInfo networkInfo = cm.getActiveNetworkInfo();  
            if (networkInfo != null  
                    && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {  
                return true;  
            }  
        }  
        return false;  
    }  
  
    /** 
     * 檢測3G是否連接 
     *  
     * @return 
     */  
    private boolean is3gConnected() {  
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
        if (cm != null) {  
            NetworkInfo networkInfo = cm.getActiveNetworkInfo();  
            if (networkInfo != null  
                    && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {  
                return true;  
            }  
        }  
        return false;  
    }  
  
    /** 
     * 檢測GPS是否打開 
     *  
     * @return 
     */  
    private boolean isGpsEnabled() {  
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
        List<String> accessibleProviders = lm.getProviders(true);  
        for (String name : accessibleProviders) {  
            if ("gps".equals(name)) {  
                return true;  
            }  
        }  
        return false;  
    }  
  
    @Override  
    public void onClick(View v) {  
        if (v == checkBtn) {  
            netText.setText(isNetConnected() + "");  
            wifiText.setText(isWifiConnected() + "");  
            net3gText.setText(is3gConnected() + "");  
            gpsText.setText(isGpsEnabled() + "");  
        }  
    }  
}  

 

 

佈局xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
  
    <Button  
        android:id="@+id/main_btn_check"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="檢測網絡" />  
  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" >  
  
        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="網絡是否連接:" />  
  
        <EditText  
            android:id="@+id/main_et_net"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="10dp"  
            android:enabled="false" />  
    </LinearLayout>  
  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" >  
  
        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="wifi是否連接:" />  
  
        <EditText  
            android:id="@+id/main_et_wifi"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="10dp"  
            android:enabled="false" />  
    </LinearLayout>  
  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" >  
  
        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="3G是否連接:" />  
  
        <EditText  
            android:id="@+id/main_et_3g"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="10dp"  
            android:enabled="false" />  
    </LinearLayout>  
  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" >  
  
        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="GPS是否打開:" />  
  
        <EditText  
            android:id="@+id/main_et_GPS"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="10dp"  
            android:enabled="false" />  
    </LinearLayout>  
  
</LinearLayout>  

 

發佈留言