原來的獲取方法不好用了,適配安卓10.0設備,最新獲取移動網絡運營商名稱方法。
獲取權限:
android.permission.READ_PHONE_STATE
工具類代碼:
/**
* Created by zachary on 2020/04/02.
* 獲取設備信息
*/
public class DeviceUtil {
/**
* 獲取網絡運營商名稱
* <p>中國移動、如中國聯通、中國電信</p>
*
* @return 運營商名稱
*/
public static String getNetworkOperatorName() {
String opeType = "unknown";
// No sim
if (!hasSim(BaseApplication.getApplication().getApplicationContext())) {
return opeType;
}
TelephonyManager tm = (TelephonyManager) BaseApplication.getApplication().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String operator = tm.getSimOperator();
if ("46001".equals(operator) || "46006".equals(operator) || "46009".equals(operator)) {
opeType = "中國聯通";
} else if ("46000".equals(operator) || "46002".equals(operator) || "46004".equals(operator) || "46007".equals(operator)) {
opeType = "中國移動";
} else if ("46003".equals(operator) || "46005".equals(operator) || "46011".equals(operator)) {
opeType = "中國電信";
} else {
opeType = "unknown";
}
return opeType;
}
/**
* 檢查手機是否有sim卡
*/
private static boolean hasSim(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String operator = tm.getSimOperator();
if (TextUtils.isEmpty(operator)) {
return false;
}
return true;
}
/**
* 判斷數據流量開關是否打開
*
* @param context
* @return
*/
public static boolean isMobileDataEnabled(Context context) {
try {
Method method = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true);
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return (Boolean) method.invoke(connectivityManager);
} catch (Throwable t) {
return false;
}
}
}
注:中國鐵通:46020