![](/img/trans.png)
[英]how to get call log for sim1, sim2 seperatly from an android activity?
[英]How to detect which operator(Sim1 or Sim2) is providing the mobile data in a device?
我能夠確定我的設備正在使用Wifi還是移動數據。 現在,我要確定在雙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);
}
}
}
上面的代碼在WIFI和數據之間進行標識。 但是,如果該設備正在使用移動數據,並且它是雙SIM卡設備,那么我如何識別哪個SIM卡在提供移動數據? enter code here
對於API> = 21
從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();
}
}
或使用反射
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.