我该如何在安卓系统中查找运营商名称?
我自己从未使用过,但可以看看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();
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
}
}