的Android如何得到电话号码的双卡手机 [英] Android How to get Phone number from the Dual sim phone
本文介绍了的Android如何得到电话号码的双卡手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我的应用程序的朋友我想告诉用户他们的SIM卡的电话号码,但在双卡双待手机是不给任何类型的数字我还是Google,但没有得到exact.please帮助梅托获取电话号码双卡。 注以下是不工作的双卡拿到号:
字符串联系号码= telephonymanager.getLine1Number();
解决方案
使用下面的code。
请类SimNoInfo如下图所示。
进口java.lang.reflect.Method中;
进口android.content.Context;
进口android.telephony.TelephonyManager;
公共final类SimNoInfo {
私有静态SimNoInfo telephonyInfo;
私人字符串imeiSIM1;
私人字符串imeiSIM2;
私人布尔isSIM1Ready;
私人布尔isSIM2Ready;
公共字符串getImeiSIM1(){
返回imeiSIM1;
}
公共字符串getImeiSIM2(){
返回imeiSIM2;
}
公共布尔isSIM1Ready(){
返回isSIM1Ready;
}
公共布尔isSIM2Ready(){
返回isSIM2Ready;
}
公共布尔isDualSIM(){
返回imeiSIM2!= NULL;
}
私人TelephonyInfo(){
}
公共静态SimNoInfo的getInstance(上下文的背景下){
如果(telephonyInfo == NULL){
telephonyInfo =新SimNoInfo();
TelephonyManager telephonyManager =((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE));
telephonyInfo.imeiSIM1 = telephonyManager.getDeviceId();;
telephonyInfo.imeiSIM2 = NULL;
尝试 {
telephonyInfo.imeiSIM1 = getDeviceIdBySlot(上下文中,getDeviceIdGemini,0);
telephonyInfo.imeiSIM2 = getDeviceIdBySlot(上下文中,getDeviceIdGemini,1);
}赶上(GeminiMethodNotFoundException E){
e.printStackTrace();
尝试 {
telephonyInfo.imeiSIM1 = getDeviceIdBySlot(上下文中,getDeviceId,0);
telephonyInfo.imeiSIM2 = getDeviceIdBySlot(上下文中,getDeviceId,1);
}赶上(GeminiMethodNotFoundException E1){
//调用这里下一个制造商的pdicted $ P $方法名,如果你想
e1.printStackTrace();
}
}
telephonyInfo.isSIM1Ready = telephonyManager.getSimState()== TelephonyManager.SIM_STATE_READY;
telephonyInfo.isSIM2Ready = FALSE;
尝试 {
telephonyInfo.isSIM1Ready = getSIMStateBySlot(上下文中,getSimStateGemini,0);
telephonyInfo.isSIM2Ready = getSIMStateBySlot(上下文中,getSimStateGemini,1);
}赶上(GeminiMethodNotFoundException E){
e.printStackTrace();
尝试 {
telephonyInfo.isSIM1Ready = getSIMStateBySlot(上下文中,getSimState,0);
telephonyInfo.isSIM2Ready = getSIMStateBySlot(上下文中,getSimState,1);
}赶上(GeminiMethodNotFoundException E1){
//调用这里下一个制造商的pdicted $ P $方法名,如果你想
e1.printStackTrace();
}
}
}
返回telephonyInfo;
}
私有静态字符串getDeviceIdBySlot(上下文的背景下,字符串predictedMethodName,诠释slotID用于)抛出GeminiMethodNotFoundException {
IMEI串= NULL;
TelephonyManager电话=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
尝试{
类<> telephonyClass =的Class.forName(telephony.getClass()的getName());
类<> []参数=新类[1];
参数[0] = int.class;
方法getSimID = telephonyClass.getMethod(predictedMethodName,参数);
[对象] obParameter =新的对象[1];
obParameter [0] = slotID用于;
对象ob_phone = getSimID.invoke(电话,obParameter);
如果(ob_phone!= NULL){
IMEI = ob_phone.toString();
}
}赶上(例外五){
e.printStackTrace();
抛出新GeminiMethodNotFoundException(predictedMethodName);
}
返回IMEI;
}
私有静态布尔getSIMStateBySlot(上下文的背景下,字符串predictedMethodName,INT slotID用于)抛出GeminiMethodNotFoundException {
布尔isReady = FALSE;
TelephonyManager电话=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
尝试{
类<> telephonyClass =的Class.forName(telephony.getClass()的getName());
类<> []参数=新类[1];
参数[0] = int.class;
方法getSimStateGemini = telephonyClass.getMethod(predictedMethodName,参数);
[对象] obParameter =新的对象[1];
obParameter [0] = slotID用于;
对象ob_phone = getSimStateGemini.invoke(电话,obParameter);
如果(ob_phone!= NULL){
INT SIMSTATE =的Integer.parseInt(ob_phone.toString());
如果(SIMSTATE == TelephonyManager.SIM_STATE_READY){
isReady = TRUE;
}
}
}赶上(例外五){
e.printStackTrace();
抛出新GeminiMethodNotFoundException(predictedMethodName);
}
返回isReady;
}
私有静态类GeminiMethodNotFoundException扩展异常{
私有静态最后长的serialVersionUID = -996812356902545308L;
公共GeminiMethodNotFoundException(字符串信息){
超(信息);
}
}
}
然后再使用这个类这样的:
私人无效isDualSimOrNot(){
SimNoInfo telephonyInfo = SimNoInfo.getInstance(本);
串imeiSIM1 = telephonyInfo.getImeiSIM1();
串imeiSIM2 = telephonyInfo.getImeiSIM2();
布尔isSIM1Ready = telephonyInfo.isSIM1Ready();
布尔isSIM2Ready = telephonyInfo.isSIM2Ready();
布尔isDualSIM = telephonyInfo.isDualSIM();
Log.i(双=,IME1:+ imeiSIM1 +\ N+
IME2:+ imeiSIM2 +\ N+
采用双SIM卡:+ isDualSIM +\ N+
IS SIM1 READY:+ isSIM1Ready +\ N+
IS SIM2 READY:+ isSIM2Ready +\ N);
}
和不要忘了补充许可androidmanifest。
<使用-权限的Android:名称=android.permission.READ_PHONE_STATE/>
Hello friends in my app i want to show user to their sim phone number but with in dual sim mobile is not give any type number i much googling but not get exact.please help meto get phone number from dual sim. Note following is not working on dual sim to get number:
String phonenumber = telephonymanager.getLine1Number();
解决方案
Use following code.
make class SimNoInfo like below.
import java.lang.reflect.Method;
import android.content.Context;
import android.telephony.TelephonyManager;
public final class SimNoInfo {
private static SimNoInfo telephonyInfo;
private String imeiSIM1;
private String imeiSIM2;
private boolean isSIM1Ready;
private boolean isSIM2Ready;
public String getImeiSIM1() {
return imeiSIM1;
}
public String getImeiSIM2() {
return imeiSIM2;
}
public boolean isSIM1Ready() {
return isSIM1Ready;
}
public boolean isSIM2Ready() {
return isSIM2Ready;
}
public boolean isDualSIM() {
return imeiSIM2 != null;
}
private TelephonyInfo() {
}
public static SimNoInfo getInstance(Context context){
if(telephonyInfo == null) {
telephonyInfo = new SimNoInfo();
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);
}
}
}
Then then use this class like that:
private void isDualSimOrNot(){
SimNoInfo telephonyInfo = SimNoInfo.getInstance(this);
String imeiSIM1 = telephonyInfo.getImeiSIM1();
String imeiSIM2 = telephonyInfo.getImeiSIM2();
boolean isSIM1Ready = telephonyInfo.isSIM1Ready();
boolean isSIM2Ready = telephonyInfo.isSIM2Ready();
boolean isDualSIM = 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");
}
And dont forgot to add permission in androidmanifest.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
这篇关于的Android如何得到电话号码的双卡手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文