天天看點

Android網絡狀态之----ConnectivityManager下面咱們直接看代碼:

java.lang.Object
  繼承者 android.net.ConnectivityManager
           

他的英文解釋:

Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by calling Context.getSystemService(Context.CONNECTIVITY_SERVICE).
           

我用有道翻譯了下:

類,回答關于網絡連接配接狀态的查詢。當網絡連接配接發生變化也通知應用程式。得到該類的執行個體通過調用Context.getSystemService(上下文。連接配接服務)。
           

幾個相關的網絡狀态管理類

ConnectivityManager:主要管理和網絡連接配接相關的操作  

TelephonyManager:則管理和手機、營運商等的相關資訊

WifiManager :則管理和wifi相關的資訊。 想通路網絡狀态,
           

下面咱們直接看代碼:

  • 這個dome是一個很簡單的,一個MainActivity和兩個公共類,其中一個是內建了BroadcastReceiver!如下圖所示:
Android網絡狀态之----ConnectivityManager下面咱們直接看代碼:
  • 注意,清單檔案中的權限一定要加。

>首先看一下NetUtils這個類(普通無繼承),類裡寫了三個方法:

方法一:netStete(Context context)–檢查網絡連結狀态

方法二:testConnectivityManager(Context context)–檢查GPRS連結狀态

方法三: testWIFIManager(Context context)–檢查wifi連結狀态

//建立一個網絡管理對象
public static ConnectivityManager conn;
           
public static boolean netStete(Context context) {
        // 擷取系統的網絡管理對象
         conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (conn == null) {
            return false;
        } else {
            // 因為網絡有wifi和gprs是以用數組來接受
            NetworkInfo[] infos = conn.getAllNetworkInfo();
            if (infos != null) {
                for (NetworkInfo info : infos) {
                    // 判斷網絡是否連接配接了
                    if (info.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
           
//GPRS網絡狀态
    public static boolean testConnectivityManager(Context context) {
        conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo.State state = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if(NetworkInfo.State.CONNECTED==state){
            Log.i("Text", "GPRS網絡已連接配接");
            return true;
        }
        return false;
    }
           
//wifi網絡狀态
    public static boolean testWIFIManager(Context context) {
        conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo.State state = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        state = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if(NetworkInfo.State.CONNECTED==state){
            Log.i("Text", "WIFI網絡已連接配接");
            return true;
        }
        return false;
    }
           

上面是封裝好的方法,直接在MainActivity中引用即可!,例如下面代碼

if(!NetUtils.netStete(MainActivity.this)){
              //假如沒有網絡
               Ly_false.setVisibility(View.VISIBLE);
               Log.i("Text", "img_false11111111111"); 
        }else {
              //假如有網絡
               Ly_ture.setVisibility(View.VISIBLE);
               Log.i("Text", "img_false222222222");
        }
           

現在我們再來看一下用BroadcastReceiver怎麼運用

  • 這個公共類NetUtisBroad,繼承了BroadcastReceiver
    這裡面有兩個方法。

    onReceive()

    isNetworkAvailable()

public void onReceive(Context context, Intent intent) {
        Log.i("Text", "網絡狀态.");
        if (!isNetworkAvailable(context)) {
            Toast.makeText(context, "網絡斷開了!", Toast.LENGTH_SHORT).show();
        }
    }
           
public static boolean isNetworkAvailable(Context context) {
//擷取網絡管理對象
        ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //擷取所有可用網絡
        NetworkInfo[] info = mgr.getAllNetworkInfo();
        if (info != null) {
            for (int i = ; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }
           
  • 然後我們需要在AndroidManifest.xml中配置相關資訊
<receiver android:name=".NetUtisBroad">
            <intent-filter>
                <action android:name="com.example.com.CONNECTIVITY_CHANGE"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
           
  • 我們看一下在MainActivity中的引用
Intent intent = new Intent();
                intent.setAction("com.example.com.CONNECTIVITY_CHANGE");
                intent.putExtra("df","fdsafadsaafds");
                sendBroadcast(intent);
           

dome下載下傳位址:dome

http://download.csdn.net/detail/bobo8945510/9626948

繼續閱讀