Get Both Sim Numbers In A Dual Sim Android Phone
I want to get both the sim numbers in a dual sim device.
Solution 1:
Work with API > 21
Need permission android.Manifest.permission.READ_PHONE_STATE.
publicclassSplahActivityextendsAppCompatActivity {
privateContext context;
privateSubscriptionManager mSubscriptionManager;
publicstaticboolean isMultiSimEnabled = false;
publicstaticString defaultSimName;
publicstaticList<SubscriptionInfo> subInfoList;
publicstaticArrayList<String> Numbers;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_splah);
Numbers = newArrayList<String>();
mSubscriptionManager = SubscriptionManager.from(context);
GetCarriorsInformation();
}
privatevoidGetCarriorsInformation() {
subInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
if (subInfoList.size() > 1) {
isMultiSimEnabled = true;
}
for (SubscriptionInfo subscriptionInfo : subInfoList) {
Numbers.add(subscriptionInfo.getNumber());
}
}
}
SubscriptionManager Api 22 Android Official
Solution 2:
Make this class TelephonyInfo.java :
import java.lang.reflect.Method;
import android.content.Context;
import android.telephony.TelephonyManager;
publicfinalclassTelephonyInfo {
privatestatic TelephonyInfo telephonyInfo;
private String imeiSIM1;
private String imeiSIM2;
privateboolean isSIM1Ready;
privateboolean 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;
}*/publicbooleanisSIM1Ready() {
return isSIM1Ready;
}
/*public static void setSIM1Ready(boolean isSIM1Ready) {
TelephonyInfo.isSIM1Ready = isSIM1Ready;
}*/publicbooleanisSIM2Ready() {
return isSIM2Ready;
}
/*public static void setSIM2Ready(boolean isSIM2Ready) {
TelephonyInfo.isSIM2Ready = isSIM2Ready;
}*/publicbooleanisDualSIM() {
return imeiSIM2 != null;
}
privateTelephonyInfo() {
}
publicstatic TelephonyInfo getInstance(Context context){
if(telephonyInfo == null) {
telephonyInfo = newTelephonyInfo();
TelephonyManagertelephonyManager= ((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;
}
privatestatic String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID)throws GeminiMethodNotFoundException {
Stringimei=null;
TelephonyManagertelephony= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try{
Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
Class<?>[] parameter = newClass[1];
parameter[0] = int.class;
MethodgetSimID= telephonyClass.getMethod(predictedMethodName, parameter);
Object[] obParameter = newObject[1];
obParameter[0] = slotID;
Objectob_phone= getSimID.invoke(telephony, obParameter);
if(ob_phone != null){
imei = ob_phone.toString();
}
} catch (Exception e) {
e.printStackTrace();
thrownewGeminiMethodNotFoundException(predictedMethodName);
}
return imei;
}
privatestaticbooleangetSIMStateBySlot(Context context, String predictedMethodName, int slotID)throws GeminiMethodNotFoundException {
booleanisReady=false;
TelephonyManagertelephony= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try{
Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
Class<?>[] parameter = newClass[1];
parameter[0] = int.class;
MethodgetSimStateGemini= telephonyClass.getMethod(predictedMethodName, parameter);
Object[] obParameter = newObject[1];
obParameter[0] = slotID;
Objectob_phone= getSimStateGemini.invoke(telephony, obParameter);
if(ob_phone != null){
intsimState= Integer.parseInt(ob_phone.toString());
if(simState == TelephonyManager.SIM_STATE_READY){
isReady = true;
}
}
} catch (Exception e) {
e.printStackTrace();
thrownewGeminiMethodNotFoundException(predictedMethodName);
}
return isReady;
}
privatestaticclassGeminiMethodNotFoundExceptionextendsException {
privatestaticfinallongserialVersionUID= -996812356902545308L;
publicGeminiMethodNotFoundException(String info) {
super(info);
}
}
}
And then use this class like that:
privatevoidisDualSimOrNot(){
TelephonyInfotelephonyInfo= TelephonyInfo.getInstance(this);
StringimeiSIM1= telephonyInfo.getImeiSIM1();
StringimeiSIM2= telephonyInfo.getImeiSIM2();
booleanisSIM1Ready= telephonyInfo.isSIM1Ready();
booleanisSIM2Ready= telephonyInfo.isSIM2Ready();
booleanisDualSIM= telephonyInfo.isDualSIM();
Log.i("Dual = "," IME1 : " + imeiSIM1 + "\n" +
" IME2 : " + imeiSIM2 + "\n" +
" IS DUAL SIM : " + isDualSIM + "\n" +
" IS SIM1 READY : " + isSIM1Ready + "\n" +
" IS SIM2 READY : " + isSIM2Ready + "\n");
}
Add this permission to the apps AndroidManifest.xml:
<uses-permissionandroid:name="android.permission.READ_PHONE_STATE" />
Above code works perfectly for me.
Post a Comment for "Get Both Sim Numbers In A Dual Sim Android Phone"