【发布时间】:2022-01-09 18:28:34
【问题描述】:
如何在 Android 中找到运营商的名称?
【问题讨论】:
标签: android
如何在 Android 中找到运营商的名称?
【问题讨论】:
标签: android
自己没用过,看看TelephonyManager->getNetworkOperatorName()。
您可以尝试以下简单的方法:
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String carrierName = manager.getNetworkOperatorName();
【讨论】:
TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
String operatorName = telephonyManager.getNetworkOperatorName();
【讨论】:
如果需要@Waza_Be 询问的通知栏上显示的运营商的运营商名称。可以改用 getSimOperatorName 方法,因为有几家电信公司将他们的网络转租给其他公司。
TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
String simOperatorName = telephonyManager.getSimOperatorName();
Kotlin 空安全实现:
val operatorName = (context.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager)?.networkOperatorName ?: "unknown"
【讨论】:
你可以试试这样的东西 - 最新的工作和改进的代码
在 JAVA 中
String getCarrierName() {
try {
TelephonyManager manager = (TelephonyManager) OneSignal.appContext.getSystemService(Context.TELEPHONY_SERVICE);
// May throw even though it's not in noted in the Android docs.
// Issue #427
String carrierName = manager.getNetworkOperatorName();
return "".equals(carrierName) ? null : carrierName;
} catch(Throwable t) {
t.printStackTrace();
return null;
}
}
在科特林
fun getCarrierName(): String? {
return try {
val manager =
App.instance.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
// May throw even though it's not in noted in the Android docs.
// Issue #427
val carrierName = manager.networkOperatorName
if ("" == carrierName) null else carrierName
} catch (t: Throwable) {
t.printStackTrace()
null
}
}
【讨论】: