
Android的官方文档是没有提供相应的Api的,因为标准的Andoird是没有双卡的,好像也只有国内才会搞双卡双待的神器吧。以下记录一下做这个功能所学习到的东西。直接上代码:
import javalangreflectInvocationTargetException;
import javalangreflectMethod;
import javautilList;
import androidappActivity;
import androidcontentContext;
import androidcontentSharedPreferences;
import androidcontentSharedPreferencesEditor;
import androidosBundle;
import androidpreferencePreferenceManager;
import androidtelephonyCellInfo;
import androidtelephonyTelephonyManager;
import androidviewMenu;
import androidwidgetTextView;
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)
{
superonCreate(savedInstanceState);
setContentView(Rlayoutactivity_main);
tv = (TextView) findViewById(Ridtext);
tv2 = (TextView) findViewById(Ridtext2);
tv2setText("不知道哪个卡可用!");
getNumber();
}
private void getNumber()
{
TelephonyManager tm = (TelephonyManager) thisgetSystemService(thisTELEPHONY_SERVICE);
String phoneNumber1 = tmgetLine1Number();
// String phoneNumber2 = tmgetGroupIdLevel1();
initIsDoubleTelephone(this);
if (isDouble)
{
tvsetText("本机号码是:" + " " + phoneNumber1 + " " + "这是双卡手机!");
} else
{
// tvsetText("这是单卡手机");
tvsetText("本机号码是:" + " " + phoneNumber1 + " " + "这是单卡手机");
}
}
public void initIsDoubleTelephone(Context context)
{
isDouble = true;
Method method = null;
Object result_0 = null;
Object result_1 = null;
TelephonyManager tm = (TelephonyManager) contextgetSystemService(ContextTELEPHONY_SERVICE);
try
{
// 只要在反射getSimStateGemini 这个函数时报了错就是单卡手机(这是我自己的经验,不一定全正确)
method = TelephonyManagerclassgetMethod("getSimStateGemini", new Class[]
{ intclass });
// 获取SIM卡1
result_0 = methodinvoke(tm, new Object[]
{ new Integer(0) });
// 获取SIM卡2
result_1 = methodinvoke(tm, new Object[]
{ new Integer(1) });
} catch (SecurityException e)
{
isDouble = false;
eprintStackTrace();
// Systemoutprintln("1_ISSINGLETELEPHONE:"+etoString());
} catch (NoSuchMethodException e)
{
isDouble = false;
eprintStackTrace();
// Systemoutprintln("2_ISSINGLETELEPHONE:"+etoString());
} catch (IllegalArgumentException e)
{
isDouble = false;
eprintStackTrace();
} catch (IllegalAccessException e)
{
isDouble = false;
eprintStackTrace();
} catch (InvocationTargetException e)
{
isDouble = false;
eprintStackTrace();
} catch (Exception e)
{
isDouble = false;
eprintStackTrace();
// Systemoutprintln("3_ISSINGLETELEPHONE:"+etoString());
}
SharedPreferences sp = PreferenceManagergetDefaultSharedPreferences(context);
Editor editor = spedit();
if (isDouble)
{
// 保存为双卡手机
editorputBoolean(ISDOUBLE, true);
// 保存双卡是否可用
// 如下判断哪个卡可用双卡都可以用
if (result_0toString()equals("5") && result_1toString()equals("5"))
{
if (!spgetString(SIMCARD, "2")equals("0") && !spgetString(SIMCARD, "2")equals("1"))
{
editorputString(SIMCARD, "0");
}
editorputBoolean(SIMCARD_1, true);
editorputBoolean(SIMCARD_2, true);
tv2setText("双卡可用");
} else if (!result_0toString()equals("5") && result_1toString()equals("5"))
{// 卡二可用
if (!spgetString(SIMCARD, "2")equals("0") && !spgetString(SIMCARD, "2")equals("1"))
{
editorputString(SIMCARD, "1");
}
editorputBoolean(SIMCARD_1, false);
editorputBoolean(SIMCARD_2, true);
tv2setText("卡二可用");
} else if (result_0toString()equals("5") && !result_1toString()equals("5"))
{// 卡一可用
if (!spgetString(SIMCARD, "2")equals("0") && !spgetString(SIMCARD, "2")equals("1"))
{
editorputString(SIMCARD, "0");
}
editorputBoolean(SIMCARD_1, true);
editorputBoolean(SIMCARD_2, false);
tv2setText("卡一可用");
} else
{// 两个卡都不可用(飞行模式会出现这种种情况)
editorputBoolean(SIMCARD_1, false);
editorputBoolean(SIMCARD_2, false);
tv2setText("飞行模式");
}
} else
{
// 保存为单卡手机
editorputString(SIMCARD, "0");
editorputBoolean(ISDOUBLE, false);
}
editorcommit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present
getMenuInflater()inflate(Rmenumain, menu);
return true;
}
}
不要忘记添加权限:<uses-permission android:name="androidpermissionREAD_PHONE_STATE" />
代码中读取sim卡的代码如下:
1在AndroidManifestxml添加权限:
<!-- 添加访问手机位置的权限 --> <uses-permission android:name="androidpermissionACCESS_COARSE_LOCATION"/> <!-- 添加访问手机状态的权限 --> <uses-permission android:name="androidpermissionREAD_PHONE_STATE"/>
2通过context对象获取TelephonyManager tManager类的实例
3获取设备当前位置
String location = tManagergetCellLocation()==null "未知地区":tManagergetCellLocation()toString();
4获取手机制式
String simOperatorName = tManagergetSimOperatorName()equals("")"未知":tManagergetSimOperatorName()toString();
5获取SIM卡运营商名称
String networkOperatorName = tManagergetNetworkOperatorName()==null "未知":tManagergetNetworkOperatorName()toString();
6获取SIM卡号
String line1Number = tManagergetLine1Number()==null "未知":tManagergetLine1Number()toString();
SIM卡的序列号?
用TelephonyManagergetSimSerialNumber
SIm卡上的联系人?
content://icc/adn或content://sim/adn
具体用法: >
以上就是关于android开发中想获取双卡手机号码,两个号码的怎样获取全部的内容,包括:android开发中想获取双卡手机号码,两个号码的怎样获取、Android如何开发获取本机手机号功能、android有提供可以抓取SIM卡号码的API么等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)