Android 在双sim安卓手机中获取两个sim号码
我想在双sim卡设备中同时获取两个sim卡号码。将此类设置为TelephonyInfo。java:Android 在双sim安卓手机中获取两个sim号码,android,Android,我想在双sim卡设备中同时获取两个sim卡号码。将此类设置为TelephonyInfo。java: import java.lang.reflect.Method; import android.content.Context; import android.telephony.TelephonyManager; public final class TelephonyInfo { private static TelephonyInfo telephonyInfo; private St
import java.lang.reflect.Method;
import android.content.Context;
import android.telephony.TelephonyManager;
public final class TelephonyInfo {
private static TelephonyInfo telephonyInfo;
private String imeiSIM1;
private String imeiSIM2;
private boolean isSIM1Ready;
private boolean isSIM2Ready;
public String getImeiSIM1() {
return imeiSIM1;
}
/*public static void setImeiSIM1(String imeiSIM1) {
TelephonyInfo.imeiSIM1 = imeiSIM1;
}*/
public String getImeiSIM2() {
return imeiSIM2;
}
/*public static void setImeiSIM2(String imeiSIM2) {
TelephonyInfo.imeiSIM2 = imeiSIM2;
}*/
public boolean isSIM1Ready() {
return isSIM1Ready;
}
/*public static void setSIM1Ready(boolean isSIM1Ready) {
TelephonyInfo.isSIM1Ready = isSIM1Ready;
}*/
public boolean isSIM2Ready() {
return isSIM2Ready;
}
/*public static void setSIM2Ready(boolean isSIM2Ready) {
TelephonyInfo.isSIM2Ready = isSIM2Ready;
}*/
public boolean isDualSIM() {
return imeiSIM2 != null;
}
private TelephonyInfo() {
}
public static TelephonyInfo getInstance(Context context){
if(telephonyInfo == null) {
telephonyInfo = new TelephonyInfo();
TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
telephonyInfo.imeiSIM1 = telephonyManager.getDeviceId();;
telephonyInfo.imeiSIM2 = null;
try {
telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, "getDeviceIdGemini", 0);
telephonyInfo.imeiSIM2 = getDeviceIdBySlot(context, "getDeviceIdGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, "getDeviceId", 0);
telephonyInfo.imeiSIM2 = getDeviceIdBySlot(context, "getDeviceId", 1);
} catch (GeminiMethodNotFoundException e1) {
//Call here for next manufacturer's predicted method name if you wish
e1.printStackTrace();
}
}
telephonyInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;
telephonyInfo.isSIM2Ready = false;
try {
telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimStateGemini", 0);
telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimStateGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimState", 0);
telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimState", 1);
} catch (GeminiMethodNotFoundException e1) {
//Call here for next manufacturer's predicted method name if you wish
e1.printStackTrace();
}
}
}
return telephonyInfo;
}
private static String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {
String imei = null;
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try{
Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimID.invoke(telephony, obParameter);
if(ob_phone != null){
imei = ob_phone.toString();
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return imei;
}
private static boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {
boolean isReady = false;
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try{
Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimStateGemini = telephonyClass.getMethod(predictedMethodName, parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimStateGemini.invoke(telephony, obParameter);
if(ob_phone != null){
int simState = Integer.parseInt(ob_phone.toString());
if(simState == TelephonyManager.SIM_STATE_READY){
isReady = true;
}
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return isReady;
}
private static class GeminiMethodNotFoundException extends Exception {
private static final long serialVersionUID = -996812356902545308L;
public GeminiMethodNotFoundException(String info) {
super(info);
}
}
将此权限添加到apps AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
以上代码非常适合我。使用API>21
需要权限android.Manifest.permission.READ\u PHONE\u STATE.
public class SplahActivity extends AppCompatActivity {
private Context context;
private SubscriptionManager mSubscriptionManager;
public static boolean isMultiSimEnabled = false;
public static String defaultSimName;
public static List<SubscriptionInfo> subInfoList;
public static ArrayList<String> Numbers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_splah);
Numbers = new ArrayList<String>();
mSubscriptionManager = SubscriptionManager.from(context);
GetCarriorsInformation();
}
private void GetCarriorsInformation() {
subInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
if (subInfoList.size() > 1) {
isMultiSimEnabled = true;
}
for (SubscriptionInfo subscriptionInfo : subInfoList) {
Numbers.add(subscriptionInfo.getNumber());
}
}
}
公共类splahaActivity扩展了AppCompative活动{
私人语境;
私有订阅管理器MSSubscriptionManager;
公共静态布尔值isMultiSimEnabled=false;
公共静态字符串defaultSimName;
公共静态列表子信息列表;
公共静态数组列表编号;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
上下文=这个;
setContentView(R.layout.activity\u splah);
数字=新的ArrayList();
mSubscriptionManager=SubscriptionManager.from(上下文);
GetCarriorInformation();
}
私有void GetCarriorsInformation(){
subInfoList=mSubscriptionManager.getActiveSubscriptionInfoList();
if(subInfoList.size()>1){
isMultiSimEnabled=true;
}
用于(订阅信息订阅信息:子信息列表){
add(subscriptionInfo.getNumber());
}
}
}
但在双sim卡手机中,我可以同时获得两个sim卡号码吗?您有没有解决方案,没有延迟。目的地?没有,我没有任何解决方案。我正在将他们的默认号码用于我的应用程序。您缺少
。我已经更新了答案。非常好的答案,谢谢。有一件事,有没有办法从sim卡获取更多信息?具体来说,我想知道它属于哪个巫师。imei更多地与设备相关,而不是sim卡。我说的对吗?不使用三星Note 3 Neo(SM-N7502)是双SIM卡:对于Android 5.1及以上的设备,您可以使用Subscription Manager读取两个SIM卡信息。。如果您正在使用beow版本,请使用下面的替代方法getDeviceIdDS(int插槽号)getDeviceId(int插槽号)注意:三星Neo中不存在getDeviceIdGemini方法…您将使用getDeviceIdDS关于sim号码(IMSI)的问题,但回答关于IMEI的问题,这是不同的尝试此方法并订阅Info.getNumber()总是回来null@mindw0rk它将从sim卡设置中重新运行存储的号码[
public class SplahActivity extends AppCompatActivity {
private Context context;
private SubscriptionManager mSubscriptionManager;
public static boolean isMultiSimEnabled = false;
public static String defaultSimName;
public static List<SubscriptionInfo> subInfoList;
public static ArrayList<String> Numbers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_splah);
Numbers = new ArrayList<String>();
mSubscriptionManager = SubscriptionManager.from(context);
GetCarriorsInformation();
}
private void GetCarriorsInformation() {
subInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
if (subInfoList.size() > 1) {
isMultiSimEnabled = true;
}
for (SubscriptionInfo subscriptionInfo : subInfoList) {
Numbers.add(subscriptionInfo.getNumber());
}
}
}