Android工具類整理:檢查是否有網絡、檢查SD卡是否存在
以下工具類主要用于:
(1)檢查是否有網絡、
(2)檢查是否有WIFI、
(3)檢查是否有移動網絡
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo;public class CommonUtils {
/** 檢查是否有網絡 */ public static boolean isNetworkAvailable(Context context) { NetworkInfo info = getNetworkInfo(context); if (info != null) { return info.isAvailable(); } return false; } /** 檢查是否是WIFI */ public static boolean isWifi(Context context) { NetworkInfo info = getNetworkInfo(context); if (info != null) { if (info.getType() == ConnectivityManager.TYPE_WIFI) return true; } return false; } /** 檢查是否是移動網絡 */ public static boolean isMobile(Context context) { NetworkInfo info = getNetworkInfo(context); if (info != null) { if (info.getType() == ConnectivityManager.TYPE_MOBILE) return true; } return false; } private static NetworkInfo getNetworkInfo(Context context) { ConnectivityManager cm = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); return cm.getActiveNetworkInfo(); } /** 檢查SD卡是否存在 */ public static boolean checkSdCard() { if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) return true; else return false; }}</pre>
本文由用戶 mip33 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!