一、強制顯示或隱藏軟鍵盤
/**
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);
}
}
}
二、判斷網絡狀態
/**
返回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;
}
三、判斷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;
}
四、判斷移動網絡是否鏈接
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;
}