简体   繁体   English

如何检测哪个运营商(Sim1或Sim2)在设备中提供移动数据?

[英]How to detect which operator(Sim1 or Sim2) is providing the mobile data in a device?

I was able to identify whether my device is using Wifi or mobile Data. 我能够确定我的设备正在使用Wifi还是移动数据。 Now, I'm stuck in identifying the SIM operator who is providing the mobile data in case of a dual SIM device. 现在,我要确定在双SIM卡设备中提供移动数据的SIM卡操作员。

 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. 上面的代码在WIFI和数据之间进行标识。 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? 但是,如果该设备正在使用移动数据,并且它是双SIM卡设备,那么我如何识别哪个SIM卡在提供移动数据? enter code here

For API >= 21 对于API> = 21

From Settings.Global 从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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM