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

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

View File

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

View File

@ -12,27 +12,46 @@ public class SimUtil {
//获取卡槽信息ID //获取卡槽信息ID
public static int getSimId(Bundle bundle) { public static int getSimId(Bundle bundle) {
int whichSIM = -1; int whichSIM = -1;
if (bundle != null) { if (bundle == null) {
if (bundle.containsKey("simId")) { return whichSIM;
whichSIM = bundle.getInt("simId"); }
} else if (bundle.containsKey("com.android.phone.extra.slot")) {
whichSIM = bundle.getInt("com.android.phone.extra.slot"); if (bundle.containsKey("simId")) {
} else { whichSIM = bundle.getInt("simId");
String keyName = ""; Log.d(TAG, "simId = " + whichSIM);
for (String key : bundle.keySet()) { } else if (bundle.containsKey("com.android.phone.extra.slot")) {
if (key.contains("sim")) whichSIM = bundle.getInt("com.android.phone.extra.slot");
keyName = key; Log.d(TAG, "com.android.phone.extra.slot = " + whichSIM);
} } else {
if (bundle.containsKey(keyName)) { String keyName = "";
whichSIM = bundle.getInt(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; 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) { public static String getSimInfo(int simId) {
String res = ""; String res = "";