您仅在广播接收器中知道 sim 插槽号。经过一个月的研究,我得到了一种对我来说很好的解决方案,如下所示
首先将 android.permission.READ_PHONE_STATE 权限添加到您的清单文件
为您的应用程序接收呼叫/短信事件的电话事件实现接收器
public class CallSMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
Log.d("SIM_SLOT"," Slot Number "+capturedSimSlot(extras));
}
}
/*below methods captures the sim slot number from bundle */
public int capturedSimSlot(Bundle bundle){
int whichSIM =-1;
if (bundle.containsKey("subscription")) {
whichSIM = bundle.getInt("subscription");
}
if(whichSIM >=0 && whichSIM < 5){
/*In some device Subscription id is return as subscriber id*/
sim = ""+whichSIM;
}else{
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);
}
}
}
return whichSIM;
}
}