android 手机号分段_android开发中想获取双卡手机号码,两个号码的怎样获取?

展开全部

Android的官方文档是32313133353236313431303231363533e58685e5aeb931333337626230没有提供相应的Api的,因为标准的Andoird是没有双卡的,好像也只有国内才会搞双卡双待的神器吧。以下记录一下做这个功能所学习到的东西。直接上代码:

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;

}

}

不要忘记添加权限:

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取双卡 Android 手机的电话号码可以使用下面的代码: ```java TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String sim1PhoneNumber = telephonyManager.getLine1Number(0); String sim2PhoneNumber = telephonyManager.getLine1Number(1); ``` 需要注意的是,有些运营商可能不会将电话号码存储在 SIM 卡中,因此可能无法获取到电话号码获取双卡 Android 手机的运营商频段可以使用下面的代码: ```java TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo(); for (CellInfo cellInfo : cellInfoList) { if (cellInfo instanceof CellInfoGsm) { CellIdentityGsm cellIdentityGsm = ((CellInfoGsm) cellInfo).getCellIdentity(); int mcc = cellIdentityGsm.getMcc(); int mnc = cellIdentityGsm.getMnc(); int arfcn = cellIdentityGsm.getArfcn(); // TODO: 处理 GSM 频段信息 } else if (cellInfo instanceof CellInfoCdma) { CellIdentityCdma cellIdentityCdma = ((CellInfoCdma) cellInfo).getCellIdentity(); int sid = cellIdentityCdma.getSystemId(); int nid = cellIdentityCdma.getNetworkId(); int bid = cellIdentityCdma.getBasestationId(); int freq = cellIdentityCdma.getFrequency(); // TODO: 处理 CDMA 频段信息 } else if (cellInfo instanceof CellInfoLte) { CellIdentityLte cellIdentityLte = ((CellInfoLte) cellInfo).getCellIdentity(); int mcc = cellIdentityLte.getMcc(); int mnc = cellIdentityLte.getMnc(); int earfcn = cellIdentityLte.getEarfcn(); // TODO: 处理 LTE 频段信息 } } ``` 需要注意的是,以上代码需要在 Android 5.1 及以上的版本上运行,并且需要在 AndroidManifest.xml 文件中添加相应的权限: ```xml <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值