Android獲取雙卡雙待手機(jī)的SIM卡信息示例代碼
前言
需要驗(yàn)證手機(jī)號(hào)的功能,但是國(guó)內(nèi)的手機(jī)多是雙卡雙待的,無法獲取到兩個(gè)號(hào)碼。在Android的官方文檔是沒有提供相應(yīng)的Api的,因?yàn)闃?biāo)準(zhǔn)的Andoird是沒有雙卡的,好像也只有國(guó)內(nèi)才會(huì)搞雙卡雙待的神器吧。
以下記錄一下做這個(gè)功能所學(xué)習(xí)到的東西。
Android 獲取本機(jī)手機(jī)號(hào)(適用于雙卡雙待手機(jī))
直接上代碼:
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("不知道哪個(gè)卡可用!");
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("這是雙卡手機(jī)!");
tv.setText("本機(jī)號(hào)碼是:" + " " + phoneNumber1 + " " + "這是雙卡手機(jī)!");
} else
{
// tv.setText("這是單卡手機(jī)");
tv.setText("本機(jī)號(hào)碼是:" + " " + phoneNumber1 + " " + "這是單卡手機(jī)");
}
}
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 這個(gè)函數(shù)時(shí)報(bào)了錯(cuò)就是單卡手機(jī)(這是我自己的經(jīng)驗(yàn),不一定全正確)
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)
{
// 保存為雙卡手機(jī)
editor.putBoolean(ISDOUBLE, true);
// 保存雙卡是否可用
// 如下判斷哪個(gè)卡可用.雙卡都可以用
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
{// 兩個(gè)卡都不可用(飛行模式會(huì)出現(xiàn)這種種情況)
editor.putBoolean(SIMCARD_1, false);
editor.putBoolean(SIMCARD_2, false);
tv2.setText("飛行模式");
}
} else
{
// 保存為單卡手機(jī)
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;
}
}
當(dāng)然不要忘記添加權(quán)限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
獲取雙卡雙待手機(jī)SIM卡信息
使用反射遍歷 TelephonyManager 中的方法,通過肉眼基本能找到獲取雙卡雙待號(hào)碼的方法,最后通過反射取到 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 卡序列號(hào)
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");
}
是否能取到手機(jī)號(hào),取決于手機(jī)卡,而大部分手機(jī)卡都取不到手機(jī)號(hào)碼,只能取到 SIM 卡序列號(hào)。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Flutter桌面開發(fā)windows插件開發(fā)
這篇文章主要為大家介紹了Flutter桌面開發(fā)windows插件開發(fā)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
基于android樣式與主題(style&theme)的詳解
本篇文章是對(duì)android中的樣式與主題(style&theme)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Kotlin 封裝萬能SharedPreferences存取任何類型詳解
這篇文章主要介紹了Kotlin 封裝萬能SharedPreferences存取任何類型詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程詳解
這篇文章主要介紹了Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-05-05
XListView實(shí)現(xiàn)下拉刷新和上拉加載原理解析
這篇文章主要為大家解析了XListView實(shí)現(xiàn)下拉刷新和上拉加載原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
android實(shí)現(xiàn)可以滑動(dòng)的平滑曲線圖
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)可以滑動(dòng)的平滑曲線圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
android monkey自動(dòng)化測(cè)試改為java調(diào)用monkeyrunner Api
一般情況下我們使用android中的monkeyrunner進(jìn)行自動(dòng)化測(cè)試時(shí),使用的是python語言來寫測(cè)試腳本。不過,最近發(fā)現(xiàn)可以用java調(diào)用monkeyrunner Api,于是,就簡(jiǎn)單研究了一下。這里做一些總結(jié)。希望有對(duì)在研究的午飯可以有所用處2012-11-11

