欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android获取双卡双待手机的SIM卡信息示例代码

程序员文章站 2023-08-22 21:48:38
前言 需要验证手机号的功能,但是国内的手机多是双卡双待的,无法获取到两个号码。在android的官方文档是没有提供相应的api的,因为标准的andoird是没有双卡的,好...

前言

需要验证手机号的功能,但是国内的手机多是双卡双待的,无法获取到两个号码。在android的官方文档是没有提供相应的api的,因为标准的andoird是没有双卡的,好像也只有国内才会搞双卡双待的神器吧。

以下记录一下做这个功能所学习到的东西。

android 获取本机手机号(适用于双卡双待手机)

直接上代码:

import java.lang.reflect.invocationtargetexception; 
import java.lang.reflect.method; 
import java.util.list; 

import android.app.activity; 
import android.content.context; 
import android.content.sharedpreferences; 
import android.content.sharedpreferences.editor; 
import android.os.bundle; 
import android.preference.preferencemanager; 
import android.telephony.cellinfo; 
import android.telephony.telephonymanager; 
import android.view.menu; 
import android.widget.textview; 

public class mainactivity extends activity 
{ 

 private textview tv; 
 private textview tv2; 
 // /////////////////////////////////// 
 static string  isdouble; 
 static string  simcard; 
 static string  simcard_1; 
 static string  simcard_2; 
 static boolean  isdouble; 

 // ////////////////////////////////// 

 @override 
 protected void oncreate(bundle savedinstancestate) 
 { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.activity_main); 

  tv = (textview) findviewbyid(r.id.text); 
  tv2 = (textview) findviewbyid(r.id.text2); 

  tv2.settext("不知道哪个卡可用!"); 

  getnumber(); 

 } 

 private void getnumber() 
 { 
  telephonymanager tm = (telephonymanager) this.getsystemservice(this.telephony_service); 
  string phonenumber1 = tm.getline1number(); 

  // string phonenumber2 = tm.getgroupidlevel1(); 

  initisdoubletelephone(this); 
  if (isdouble) 
  { 
   // tv.settext("这是双卡手机!"); 
   tv.settext("本机号码是:" + " " + phonenumber1 + " " + "这是双卡手机!"); 
  } else 
  { 
   // tv.settext("这是单卡手机"); 
   tv.settext("本机号码是:" + " " + phonenumber1 + " " + "这是单卡手机"); 
  } 

 } 

 public void initisdoubletelephone(context context) 
 { 
  isdouble = true; 
  method method = null; 
  object result_0 = null; 
  object result_1 = null; 
  telephonymanager tm = (telephonymanager) context.getsystemservice(context.telephony_service); 
  try 
  { 
   // 只要在反射getsimstategemini 这个函数时报了错就是单卡手机(这是我自己的经验,不一定全正确) 
   method = telephonymanager.class.getmethod("getsimstategemini", new class[] 
   { int.class }); 
   // 获取sim卡1 
   result_0 = method.invoke(tm, new object[] 
   { new integer(0) }); 
   // 获取sim卡2 
   result_1 = method.invoke(tm, new object[] 
   { new integer(1) }); 
  } catch (securityexception e) 
  { 
   isdouble = false; 
   e.printstacktrace(); 
   // system.out.println("1_issingletelephone:"+e.tostring()); 
  } catch (nosuchmethodexception e) 
  { 
   isdouble = false; 
   e.printstacktrace(); 
   // system.out.println("2_issingletelephone:"+e.tostring()); 
  } catch (illegalargumentexception e) 
  { 
   isdouble = false; 
   e.printstacktrace(); 
  } catch (illegalaccessexception e) 
  { 
   isdouble = false; 
   e.printstacktrace(); 
  } catch (invocationtargetexception e) 
  { 
   isdouble = false; 
   e.printstacktrace(); 
  } catch (exception e) 
  { 
   isdouble = false; 
   e.printstacktrace(); 
   // system.out.println("3_issingletelephone:"+e.tostring()); 
  } 
  sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(context); 
  editor editor = sp.edit(); 
  if (isdouble) 
  { 
   // 保存为双卡手机 
   editor.putboolean(isdouble, true); 
   // 保存双卡是否可用 
   // 如下判断哪个卡可用.双卡都可以用 
   if (result_0.tostring().equals("5") && result_1.tostring().equals("5")) 
   { 
    if (!sp.getstring(simcard, "2").equals("0") && !sp.getstring(simcard, "2").equals("1")) 
    { 
     editor.putstring(simcard, "0"); 
    } 
    editor.putboolean(simcard_1, true); 
    editor.putboolean(simcard_2, true); 

    tv2.settext("双卡可用"); 

   } else if (!result_0.tostring().equals("5") && result_1.tostring().equals("5")) 
   {// 卡二可用 
    if (!sp.getstring(simcard, "2").equals("0") && !sp.getstring(simcard, "2").equals("1")) 
    { 
     editor.putstring(simcard, "1"); 
    } 
    editor.putboolean(simcard_1, false); 
    editor.putboolean(simcard_2, true); 

    tv2.settext("卡二可用"); 

   } else if (result_0.tostring().equals("5") && !result_1.tostring().equals("5")) 
   {// 卡一可用 
    if (!sp.getstring(simcard, "2").equals("0") && !sp.getstring(simcard, "2").equals("1")) 
    { 
     editor.putstring(simcard, "0"); 
    } 
    editor.putboolean(simcard_1, true); 
    editor.putboolean(simcard_2, false); 

    tv2.settext("卡一可用"); 

   } else 
   {// 两个卡都不可用(飞行模式会出现这种种情况) 
    editor.putboolean(simcard_1, false); 
    editor.putboolean(simcard_2, false); 

    tv2.settext("飞行模式"); 
   } 
  } else 
  { 
   // 保存为单卡手机 
   editor.putstring(simcard, "0"); 
   editor.putboolean(isdouble, false); 
  } 
  editor.commit(); 
 } 

 @override 
 public boolean oncreateoptionsmenu(menu menu) 
 { 
  // inflate the menu; this adds items to the action bar if it is present. 
  getmenuinflater().inflate(r.menu.main, menu); 
  return true; 
 } 

} 

当然不要忘记添加权限:

<uses-permission android:name="android.permission.read_phone_state" />

获取双卡双待手机sim卡信息

使用反射遍历 telephonymanager 中的方法,通过肉眼基本能找到获取双卡双待号码的方法,最后通过反射取到 sim 卡信息。

// 遍历 telephonymanager 里的方法
public void printtelephonymanagermethodnamesforthisdevice() {
 telephonymanager telephony = (telephonymanager) getsystemservice(context.telephony_service);
 class<?> telephonyclass;
 try {
  telephonyclass = class.forname(telephony.getclass().getname());
  method[] methods = telephonyclass.getmethods();
  for (int i = 0; i < methods.length; i++) {
   log.i(tag, "\n" + methods[i] + " declared by " + methods[i].getdeclaringclass());
  }
 } catch (classnotfoundexception e) {
  e.printstacktrace();
 }
}
// 获取双卡双待 sim 卡序列号
public void getsubscriberid() {
 telephonymanager telephony = (telephonymanager) getsystemservice(context.telephony_service);
 class<?> telephonyclass;
 object result = null;
 object result0 = null;
 object result1 = null;
 try {
  telephonyclass = class.forname(telephony.getclass().getname());
  method m1 = telephonyclass.getmethod("getsubscriberid");
  method m2 = telephonyclass.getmethod("getsubscriberid", new class[]{int.class});
  result = m1.invoke(telephony);
  result0 = m2.invoke(telephony, 0);
  result1 = m2.invoke(telephony, 1);
 } catch (exception e) {
  e.printstacktrace();
 }
 log.i(tag, " getsubscriberid : " + telephony.getsubscriberid() + "\n"
   + " result : " + result + "\n"
   + " result0 : " + result0 + "\n"
   + " result1 : " + result1 + "\n");
}

是否能取到手机号,取决于手机卡,而大部分手机卡都取不到手机号码,只能取到 sim 卡序列号。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。