I was able to identify whether my device is using Wifi or mobile Data. Now, I'm stuck in identifying the SIM operator who is providing the mobile data in case of a dual SIM device.
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if(ni.getDetailedState().toString().toLowerCase().equals("connected")){
tv.setText(" Typename: "+ni.getTypeName());
tv.append("\n Detailed state: "+ni.getDetailedState()+"\n Extra Info: "+ni.getExtraInfo()+"\n Type: "+ni.getType()+"\n Reason :"+ni.getReason()
+"\n SubType Name :"+ni.getSubtypeName()+"\n SubType :"+ni.getSubtype()+"\n State :"+ni.getState());
if(!ni.getTypeName().toString().toLowerCase().equals("wifi")){
tv.append("\n Carrier:"+carrierName);
}
}
}
The above code identifies between WIFI and Data. But if the device is using mobile data , and if it is a dual SIM device, how can I identify which of the SIM is providing the mobile data? enter code here
For API >= 21
From Settings.Global
int sim = -1;
SubscriptionManager sm = (SubscriptionManager)context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
if (sm != null) {
try {
int id = Settings.Global.getInt(context.getContentResolver(), "multi_sim_data_call");
SubscriptionInfo si = sm.getActiveSubscriptionInfo(id);
if (si != null)
sim = si.getSimSlotIndex();
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}
or using Reflection
int sim = -1;
SubscriptionManager sm = (SubscriptionManager)context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
if (sm != null) {
try {
Method mGetDefaultDataSubId = getMethod(sm.getClass(), "getDefaultDataSubscriptionId", 0);
if (mGetDefaultDataSubId != null) {
int id = (int) mGetDefaultDataSubId.invoke(sm);
SubscriptionInfo si = sm.getActiveSubscriptionInfo(id);
if (si != null)
sim = si.getSimSlotIndex();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Method getMethod (Class c, String name, int params) {
Method[] cm = c.getDeclaredMethods();
for (Method m : cm) {
if (m.getName().equalsIgnoreCase(name)) {
m.setAccessible(true);
int length = m.getParameterTypes().length;
if (length == params)
return m;
}
}
return null;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.