优化获取SIM信息(兼容高版本Android)

This commit is contained in:
pppscn 2021-03-11 14:41:23 +08:00
parent bd58d8a05a
commit 7b0de2b279
2 changed files with 45 additions and 21 deletions

View File

@ -31,18 +31,23 @@ public class SmsForwarderBroadcastReceiver extends BroadcastReceiver {
try {
Bundle extras = intent.getExtras();
Log.d(TAG, "subscription = " + extras.getInt("subscription"));
Log.d(TAG, "simId = " + extras.getInt("simId"));
Object[] object = (Object[]) Objects.requireNonNull(extras).get("pdus");
if (object != null) {
//获取接收手机号
//接收手机卡信息
String simInfo = "";
//卡槽ID默认卡槽为1
int simId = 1;
try {
//获取卡槽ID
int simId = SimUtil.getSimId(extras);
if (extras.containsKey("simId")) {
simId = extras.getInt("simId");
} else if (extras.containsKey("subscription")) {
simId = SimUtil.getSimIdBySubscriptionId(extras.getInt("subscription"));
}
//自定义备注优先
simInfo = simId == 2 ? SettingUtil.getAddExtraSim2() : SettingUtil.getAddExtraSim1();
if (!simInfo.isEmpty()) { //自定义备注优先
if (!simInfo.isEmpty()) {
simInfo = "SIM" + simId + "_" + simInfo;
} else {
simInfo = SimUtil.getSimInfo(simId);

View File

@ -12,27 +12,46 @@ public class SimUtil {
//获取卡槽信息ID
public static int getSimId(Bundle bundle) {
int whichSIM = -1;
if (bundle != null) {
if (bundle.containsKey("simId")) {
whichSIM = bundle.getInt("simId");
} else if (bundle.containsKey("com.android.phone.extra.slot")) {
whichSIM = bundle.getInt("com.android.phone.extra.slot");
} else {
String keyName = "";
for (String key : bundle.keySet()) {
if (key.contains("sim"))
keyName = key;
}
if (bundle.containsKey(keyName)) {
whichSIM = bundle.getInt(keyName);
}
if (bundle == null) {
return whichSIM;
}
if (bundle.containsKey("simId")) {
whichSIM = bundle.getInt("simId");
Log.d(TAG, "simId = " + whichSIM);
} else if (bundle.containsKey("com.android.phone.extra.slot")) {
whichSIM = bundle.getInt("com.android.phone.extra.slot");
Log.d(TAG, "com.android.phone.extra.slot = " + whichSIM);
} else {
String keyName = "";
for (String key : bundle.keySet()) {
if (key.contains("sim"))
keyName = key;
}
if (bundle.containsKey(keyName)) {
whichSIM = bundle.getInt(keyName);
}
}
Log.d(TAG, " Slot Number " + whichSIM);
Log.d(TAG, "Slot Number " + whichSIM);
return whichSIM + 1;
}
//通过SubscriptionId获取卡槽信息ID
public static int getSimIdBySubscriptionId(int subscriptionId) {
try {
for (PhoneUtils.SimInfo simInfo : MyApplication.SimInfoList) {
if (simInfo.mSubscriptionId == subscriptionId) {
return simInfo.mSimSlotIndex + 1;
}
}
} catch (Exception e) {
Log.d(TAG, "getSimExtra Fail: " + e.getMessage());
}
return 0;
}
//获取卡槽备注
public static String getSimInfo(int simId) {
String res = "";