[英]How to know which SIM is consuming mobile data in dual SIM android phone?
I am building a network monitor app.我正在构建一个网络监控应用程序。 Here I have successfully implemented all the things like track data usage from Wifi or mobile data, but I want to know which SIM is connected to internet and consuming mobile data.
在这里,我已经成功地实现了所有的事情,比如从 Wifi 或移动数据跟踪数据使用情况,但我想知道哪个 SIM 卡连接到互联网并使用移动数据。
Using below code I am able to know if my dual sim phone is connected to Wifi or mobile data.使用下面的代码,我可以知道我的双卡手机是否连接到 Wifi 或移动数据。
public static String isInternetConnected (Context ctx) {
ConnectivityManager connectivityMgr = (ConnectivityManager) ctx
.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// Check if wifi or mobile network is available or not. If any of them is
// available or connected then it will return true, otherwise false;
if (wifi != null) {
if (wifi.isConnected()) {
return "wifi";
}
}
if (mobile != null) {
if (mobile.isConnected()) {
return "mobile";
}
}
return "none";
}
How can I get SIM Index or sim operator name that is consuming mobile data in dual sim android phone?如何在双卡安卓手机中获取消耗移动数据的 SIM 索引或 SIM 运营商名称?
I had searched a lot and I saw many question posted in SO without answer like this .我已经搜查了很多,我看到了许多问题张贴在SO不喜欢回答这个。
I am able to get subId of both SIM in dual SIM phone but I am phasing problem to know which SIM is using internet.我能够在双 SIM 卡手机中获得两个 SIM 卡的 subId,但我正在逐步解决问题以了解哪个 SIM 卡正在使用互联网。
Many other application are able to do this like Mubble.许多其他应用程序能够像 Mubble 一样做到这一点。
Can any one provide me a solution for it?任何人都可以为我提供解决方案吗?
After api level 22, you can use the hidden system api android.telephony.SubscriptionManager#getDefaultDataSubId
via reflection to get current active data sim subscription index. api level 22之后,你可以通过反射使用隐藏的系统api
android.telephony.SubscriptionManager#getDefaultDataSubId
来获取当前活动的数据sim订阅索引。
After api level 24, there is a public system api android.telephony.SubscriptionManager#getDefaultDataSubscriptionId
to get current active data sim subscription index.在api level 24之后,有一个公共系统api
android.telephony.SubscriptionManager#getDefaultDataSubscriptionId
来获取当前活动数据sim订阅索引。
Then, you can create a android.telephony.TelephonyManager
or android.telephony.SubscriptionManager#getActiveSubscriptionInfo
from subscription index to obtain sim operator information.然后,您可以从订阅索引创建一个
android.telephony.TelephonyManager
或android.telephony.SubscriptionManager#getActiveSubscriptionInfo
来获取 sim 运营商信息。
Here is a simple solution to get data sim operator for dual sim phone.这是获取双卡手机数据卡运营商的简单解决方案。
public static String getDataSimOperator(Context context) {
if (context == null) {
return null;
}
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
int dataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
TelephonyManager dataSimManager = tm.createForSubscriptionId(dataSubId);
return dataSimManager.getSimOperator();
} else {
String operator = getDataSimOperatorBeforeN(context);
if (operator != null) {
return operator;
} else {
return tm.getSimOperator();
}
}
} else {
return tm.getSimOperator();
}
}
return null;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
private static String getDataSimOperatorBeforeN(Context context) {
if (context == null) {
return null;
}
int dataSubId = -1;
try {
Method getDefaultDataSubId = SubscriptionManager.class.getDeclaredMethod("getDefaultDataSubId");
if (getDefaultDataSubId != null) {
getDefaultDataSubId.setAccessible(true);
dataSubId = (int) getDefaultDataSubId.invoke(null);
}
} catch (Exception e) {
e.printStackTrace();
}
if (dataSubId != -1) {
SubscriptionManager sm = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
if (sm != null && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
== PackageManager.PERMISSION_GRANTED) {
SubscriptionInfo si = sm.getActiveSubscriptionInfo(dataSubId);
if (si != null) {
// format keep the same with android.telephony.TelephonyManager#getSimOperator
// MCC + MNC format
return String.valueOf(si.getMcc()) + si.getMnc();
}
}
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.