今天就跟大家聊聊有關(guān)怎么在Android中獲取手機(jī)的SIM卡信息,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

成都創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元華容做網(wǎng)站,已為上家服務(wù),為華容各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
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");
}看完上述內(nèi)容,你們對(duì)怎么在Android中獲取手機(jī)的SIM卡信息有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章名稱:怎么在Android中獲取手機(jī)的SIM卡信息
路徑分享:http://chinadenli.net/article14/ppccge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、云服務(wù)器、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站收錄、網(wǎng)站改版、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)