您好,登錄后才能下訂單哦!
該筆記是用來實時獲取網絡的狀態,有點想QQ的斷網提醒,為了以后的工作中用到,就在此處寫下關于此判斷的代碼,下面就是寫的一些代碼:
package com.example.nettest; import receiver.NetReceiver; import android.app.Activity; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.os.Bundle; public class MainActivity extends Activity { NetReceiver mReceiver = new NetReceiver(); IntentFilter mFilter = new IntentFilter(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(mReceiver, mFilter); } @Override protected void onDestroy() { unregisterReceiver(mReceiver); super.onDestroy(); } } package receiver; import network.NetUtils; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.widget.Toast; public class NetReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) { boolean isConnected = NetUtils.isNetworkConnected(context); System.out.println("網絡狀態:" + isConnected); System.out.println("wifi狀態:" + NetUtils.isWifiConnected(context)); System.out.println("移動網絡狀態:" + NetUtils.isMobileConnected(context)); System.out.println("網絡連接類型:" + NetUtils.getConnectedType(context)); if (isConnected) { Toast.makeText(context, "已經連接網絡", Toast.LENGTH_LONG).show(); } else { Toast.makeText(context, "已經斷開網絡", Toast.LENGTH_LONG).show(); } } } } package network; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class NetUtils { // 判斷網絡連接狀態 public static boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } // 判斷wifi狀態 public static boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; } // 判斷移動網絡 public static boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); } } return false; } // 獲取連接類型 public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; } } 最后在清單文件中添加了兩個重要的權限: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> 總結下基本有這么幾個要注意的點: 1、首先要獲取對應的管理器 context.getSystemService(Context.CONNECTIVITY_SERVICE) 返回ConnectivityManager 2、獲取對應的網絡信息狀態: NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 3、判斷信息是否有效 workInfo.isAvailable() 4、該廣播被注冊之后,只要無線信號已發送改變,就會發送廣播 然后 我們做出對應的判斷就可以了
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。