如何知道每次通话/短信的SimSlot号码? [英] How to know SimSlot Number for every Call / sms?
本文介绍了如何知道每次通话/短信的SimSlot号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您仅在广播接收器中知道sim插槽号.经过一个月的研究,我得到了一种对我来说很好的解决方案,如下所示:
You know the sim slot number only in broadcast receiver. After one month of research I got one solution which is work fine for me as below
首先将android.permission.READ_PHONE_STATE权限添加到清单文件中
电话事件的接收器,可为您的应用程序接收呼叫/短信事件
Implement receiver for the phone event which receive call/sms events for your application
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;
}
}
推荐答案
用于棒棒糖22+中的短信
for sms in lollipop 22+
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int slot = Integer.parseInt((String) intent.getExtras().get("slot"));
if(slot == 0){
// sim1
}
if(slot == 1){
// sim2
}
}
}
在Lenovo K3 note中测试
tested in Lenovo K3 note
这篇关于如何知道每次通话/短信的SimSlot号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文