Java Code Examples for android.telephony.SubscriptionInfo#getSubscriptionId()

The following examples show how to use android.telephony.SubscriptionInfo#getSubscriptionId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubscriptionManager.java    From GravityBox with Apache License 2.0 7 votes vote down vote up
private PhoneAccountHandle subscriptionToPhoneAccountHandle(final SubscriptionInfo subInfo) {
    if (subInfo == null)
        return null;

    final TelecomManager telecomManager =
            (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
    final TelephonyManager telephonyManager =
            (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    final Iterator<PhoneAccountHandle> phoneAccounts =
            telecomManager.getCallCapablePhoneAccounts().listIterator();
    while (phoneAccounts.hasNext()) {
        final PhoneAccountHandle phoneAccountHandle = phoneAccounts.next();
        final PhoneAccount phoneAccount = telecomManager.getPhoneAccount(phoneAccountHandle);
        if (subInfo.getSubscriptionId() == getSubIdForPhoneAccount(telephonyManager, phoneAccount)) {
            return phoneAccountHandle;
        }
    }

    return null;
}
 
Example 2
Source File: MainActivity.java    From Msgs with MIT License 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
private void sendSms(final int which,String phone,String context) {
    SubscriptionInfo sInfo = null;

    final SubscriptionManager sManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

    List<SubscriptionInfo> list = sManager.getActiveSubscriptionInfoList();

    if (list.size() == 2) {
        // 双卡
        sInfo = list.get(which);
    } else {
        // 单卡
        sInfo = list.get(0);
    }

    if (sInfo != null) {
        int subId = sInfo.getSubscriptionId();
        SmsManager manager = SmsManager.getSmsManagerForSubscriptionId(subId);

        if (!TextUtils.isEmpty(phone)) {
            ArrayList<String> messageList =manager.divideMessage(context);
            for(String text:messageList){
                manager.sendTextMessage(phone, null, text, null, null);
            }
            Toast.makeText(this, "信息正在发送,请稍候", Toast.LENGTH_SHORT)
                    .show();
        } else {
            Toast.makeText(this, "无法正确的获取SIM卡信息,请稍候重试",
                    Toast.LENGTH_SHORT).show();
        }
    }
}
 
Example 3
Source File: SubscriptionHelper.java    From BlackList with Apache License 2.0 5 votes vote down vote up
/**
 * @return id of the current subscription (id of SIM)
 */
@Nullable
public static Integer getCurrentSubscriptionId(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        SubscriptionInfo info = getCurrentSubscription(context);
        if (info != null) return info.getSubscriptionId();
    }

    return null;
}
 
Example 4
Source File: SubscriptionHelper.java    From BlackList with Apache License 2.0 5 votes vote down vote up
@Nullable
public static SubscriptionInfo getSubscriptionById(Context context, int subscriptionId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        List<SubscriptionInfo> list = getSubscriptions(context);
        if (list != null) {
            for (SubscriptionInfo info : list) {
                if (info.getSubscriptionId() == subscriptionId) {
                    return info;
                }
            }
        }
    }

    return null;
}
 
Example 5
Source File: SubscriptionHelper.java    From BlackList with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Integer getId(SubscriptionInfo info) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && info != null) {
        return info.getSubscriptionId();
    }
    return null;
}
 
Example 6
Source File: ExampleInstrumentedTest.java    From BlackList with Apache License 2.0 5 votes vote down vote up
@Test
public void smsSubscriptionManager() throws Exception {
    Context context = InstrumentationRegistry.getTargetContext();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        List<SubscriptionInfo> list = SubscriptionHelper.getSubscriptions(context);
        if (list != null) {
            int subscriptionId = -1;
            for (SubscriptionInfo info : list) {
                Log.d("TEST_SmsManager", info.toString());
                subscriptionId = info.getSubscriptionId();
            }
            assertTrue(subscriptionId >= 0);
        }
    }
}
 
Example 7
Source File: DualNetworkIconData.java    From Status with Apache License 2.0 5 votes vote down vote up
@Override
public void register() {
    if (telephonyManager != null) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && subscriptionManager != null) {

                if (networkListener == null) {
                    int index = 0;
                    for (SubscriptionInfo info : subscriptionManager.getActiveSubscriptionInfoList()) {
                        if (info.getSimSlotIndex() > index) {
                            networkListener = new NetworkListener(info.getSubscriptionId());
                            index = info.getSimSlotIndex();
                        }
                    }
                }
            }

            if (networkListener != null)
                telephonyManager.listen(networkListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    isRegistered = true;
}
 
Example 8
Source File: SimCard.java    From batteryhub with Apache License 2.0 5 votes vote down vote up
/**
 * Experimental call to retrieve sim operator names by subscription ids.
 *
 * @param context Application context
 * @return SIM operator name/names with ";" as a delimiter for many.
 */
private static String getSIMOperators(final Context context) {

    String operators = "";

    if (!PermissionsUtils.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
        return operators;
    }

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        List<SubscriptionInfo> subscriptions =
                SubscriptionManager.from(context).getActiveSubscriptionInfoList();
        if (subscriptions != null && subscriptions.size() > 0) {
            for (SubscriptionInfo info : subscriptions) {
                int subId = info.getSubscriptionId();
                String operator = getSimOperatorNameForSubscription(context, subId);
                if (operator != null && operator.length() > 0) {
                    operators += operator + ";";
                }
            }
            // Remove last delimiter
            if (operators.length() > 1) {
                operators = operators.substring(0, operators.length() - 1);
            }
        }
    }
    return operators;
}
 
Example 9
Source File: SimCardUtils.java    From MobileInfo with Apache License 2.0 4 votes vote down vote up
@SuppressLint({"NewApi"})
private void deviceInfoTwo(List<SubscriptionInfo> list, TelephonyManager telephonyManager) {
    int num = list != null ? list.size() : 0;
    if (num > 1) {
        try {
            this.mSimCardInfo.setSim2Imei(this.simDeviceInfo(telephonyManager, "getDeviceId", 1));
        } catch (Exception e1) {
            try {
                this.mSimCardInfo.setSim2Imei(this.simDeviceInfo(telephonyManager, "getDeviceIdGemini", 1));
            } catch (Exception e2) {
                Log.i(TAG, e2.toString());
            }
        }

        try {
            this.mSimCardInfo.sim2State = this.isSimdevice(telephonyManager, "getSimState", 1);
        } catch (Exception e3) {
            try {
                this.mSimCardInfo.sim2State = this.isSimdevice(telephonyManager, "getSimStateGemini", 1);
            } catch (Exception e4) {
                Log.i(TAG, e4.toString());
            }
        }

        SubscriptionInfo subscriptionInfo = this.getSubsInfo(list, 1);
        this.mSimCardInfo.setSim2SlotIndex(subscriptionInfo.getSimSlotIndex());
        this.mSimCardInfo.setSim2SubscriptionId(subscriptionInfo.getSubscriptionId());
        int brandId = chargeBrand() ? 1 : subscriptionInfo.getSubscriptionId();

        try {
            this.mSimCardInfo.setSim2Imsi(this.simDeviceInfo(telephonyManager, "getSubscriberId", subscriptionInfo.getSubscriptionId()));
        } catch (Exception e5) {
            try {
                this.mSimCardInfo.setSim2Imsi(this.simDeviceInfo(telephonyManager, "getSubscriberIdGemini", brandId));
            } catch (Exception e6) {
                Log.i(TAG, e6.toString());
            }
        }

        try {
            this.mSimCardInfo.setSim2SimOperator(this.simDeviceInfo(telephonyManager, "getSimOperator", subscriptionInfo.getSubscriptionId()));
        } catch (Exception e7) {
            try {
                this.mSimCardInfo.setSim2SimOperator(this.simDeviceInfo(telephonyManager, "getSimOperatorGemini", brandId));
            } catch (Exception e) {
                Log.i(TAG, e.toString());
            }
        }
    }

}
 
Example 10
Source File: RNSimDataModule.java    From react-native-sim-data with MIT License 4 votes vote down vote up
@Override
public Map<String, Object> getConstants() {

  final Map<String, Object> constants = new HashMap<>();

  try {
    TelephonyManager telManager = (TelephonyManager) this.reactContext.getSystemService(Context.TELEPHONY_SERVICE);

    SubscriptionManager manager = (SubscriptionManager) this.reactContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    List<SubscriptionInfo> subscriptionInfos = manager.getActiveSubscriptionInfoList();

    int sub = 0;
    for (SubscriptionInfo subInfo : subscriptionInfos) {
      CharSequence carrierName = subInfo.getCarrierName();
      String countryIso        = subInfo.getCountryIso();
      int dataRoaming          = subInfo.getDataRoaming();  // 1 is enabled ; 0 is disabled
      CharSequence displayName = subInfo.getDisplayName();
      String iccId             = subInfo.getIccId();
      int mcc                  = subInfo.getMcc();
      int mnc                  = subInfo.getMnc();
      String number            = subInfo.getNumber();
      int simSlotIndex         = subInfo.getSimSlotIndex();
      int subscriptionId       = subInfo.getSubscriptionId();
      boolean networkRoaming   = telManager.isNetworkRoaming();
      String deviceId          = telManager.getDeviceId(simSlotIndex);
      //String deviceId          = telManager.getImei(simSlotIndex) || telManager.getMeid(simSlotIndex);

      constants.put("carrierName" + sub, carrierName.toString());
      constants.put("displayName" + sub, displayName.toString());
      constants.put("countryCode" + sub, countryIso);
      constants.put("mcc" + sub, mcc);
      constants.put("mnc" + sub, mnc);
      constants.put("isNetworkRoaming" + sub, networkRoaming);
      constants.put("isDataRoaming"    + sub, (dataRoaming == 1));
      constants.put("simSlotIndex"     + sub, simSlotIndex);
      constants.put("phoneNumber"      + sub, number);
      constants.put("deviceId"         + sub, deviceId);
      constants.put("simSerialNumber"  + sub, iccId);
      constants.put("subscriptionId"   + sub, subscriptionId);
      sub++;
    }
  } catch (Exception e) {
    e.printStackTrace();
  }

  return constants;
}