Android獲取雙卡雙待手機(jī)的SIM卡信息示例代碼
前言
需要驗(yàn)證手機(jī)號(hào)的功能,但是國(guó)內(nèi)的手機(jī)多是雙卡雙待的,無(wú)法獲取到兩個(gè)號(hào)碼。在Android的官方文檔是沒(méi)有提供相應(yīng)的Api的,因?yàn)闃?biāo)準(zhǔn)的Andoird是沒(méi)有雙卡的,好像也只有國(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 中的方法,通過(guò)肉眼基本能找到獲取雙卡雙待號(hào)碼的方法,最后通過(guò)反射取到 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à)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android實(shí)現(xiàn)左右擺動(dòng)的球體動(dòng)畫(huà)效果
這篇文章主要介紹了Android實(shí)現(xiàn)左右擺動(dòng)的球體動(dòng)畫(huà)效果,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android Drawerlayout側(cè)拉欄事件傳遞問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了Android Drawerlayout側(cè)拉欄事件傳遞問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Ubuntu 14.04下創(chuàng)建Genymotion安卓虛擬機(jī)的步驟詳解
Android 模擬器一直以速度奇慢無(wú)比著稱,基本慢到不可用。本文介紹我一直在用的 Genymotion,速度不亞于真機(jī)。而且功能齊全,使用簡(jiǎn)單。下面這篇文章主要介紹了Ubuntu 14.04下創(chuàng)建Genymotion虛擬機(jī)的步驟,需要的朋友可以參考下。2017-03-03
Android CardView詳解及使用方法和實(shí)例
這篇文章主要介紹了Android CardView詳解及使用方法和實(shí)例的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12
Android實(shí)現(xiàn)歡迎滑動(dòng)頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)歡迎滑動(dòng)頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android?如何獲取傳感器的數(shù)據(jù)方法詳解
這篇文章主要介紹了Android?如何獲取傳感器的數(shù)據(jù),傳感器?Sensor?是一種檢測(cè)裝置,能感受到被測(cè)量的信息,并能將感受到的信息,按一定規(guī)律變換成為電信號(hào)或其他所需形式的信息輸出,以滿足信息的傳輸、處理、存儲(chǔ)、顯示、記錄和控制等要求2022-07-07
Jetpack?Compose實(shí)現(xiàn)對(duì)角線滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了如何利用Jetpack?Compose實(shí)現(xiàn)一個(gè)簡(jiǎn)單的對(duì)角線滾動(dòng)效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02
Android自定義StepView仿外賣(mài)配送進(jìn)度
這篇文章主要為大家詳細(xì)介紹了Android自定義StepView仿外賣(mài)配送進(jìn)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

