Android 开发中常见的公共方法总结
RWEFreddie
9年前
来自: http://my.oschina.net/u/2491515/blog/607553
1、强制显示或隐藏软键盘
/** isOpen = true 显示 isOpen = false 隐藏 当v为空时可以调用getWindow().peekDecorView()获取整个屏幕的View */ public static void handlerKeyboard(View v, boolean isOpen) { InputMethodManager imm = (InputMethodManager) v.getContext() .getSystemService(Context.INPUT_METHOD_SERVICE); if (isOpen) { imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT); } else { if (imm.isActive()) { imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0); } } }
2、判断网络状态
/** 返回true网络正常 */ public static boolean JudgeNetWorkStatus(Context context) { if (context == null) { return false; } boolean netStatus = false; ConnectivityManager conManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = conManager.getActiveNetworkInfo(); if (info != null) { netStatus = info.isAvailable(); } return netStatus; }
3、判断WiFi是否连接
public static boolean isWifiConnected(Context context) { if (context == null) { return false; } ConnectivityManager connMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeInfo = connMgr.getActiveNetworkInfo(); if (activeInfo != null && activeInfo.isConnected()) { return activeInfo.getType() == ConnectivityManager.TYPE_WIFI; } return false; }
4、判断移动网络是否连接
public static boolean isMobileConnected(Context context) { if (context == null) { return false; } ConnectivityManager connMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeInfo = connMgr.getActiveNetworkInfo(); if (activeInfo != null && activeInfo.isConnected()) { return activeInfo.getType() == ConnectivityManager.TYPE_MOBILE; } return false; }