Android手机号码获取问题 用APN来获取手机号

20 篇文章 0 订阅
18 篇文章 0 订阅

原文  :  http://www.cnblogs.com/growing/archive/2010/11/19/1881372.html

                

     手机号码不是所有的都能获取。只是有一部分可以拿到。这个是由于移动运营商没有把手机号码的数据写入到sim卡中.SIM卡只有唯一的编号,供网络与设备识别那就是IMSI号码,手机的信号也可以说是通过这个号码在网络中传递的,并不是手机号码。试想,你的SIM丢失后,补办一张新的会换号码吗?是不会的.就是因为在你的手机号码对应的IMSI号 在移动运营商中被修改成新SIM卡的IMSI号码。 
    那么手机号为什么有的就能显示呢? 
    这个就像是一个变量,当移动运营商为它赋值了,它自然就会有值。不赋值自然为空。 
对于移动的用户,手机号码(MDN)保存在运营商的服务器中,而不是保存在SIM卡里。SIM卡只保留了IMSI和一些验证信息。手机每次入网注册的时候,都会以短信的形式将IMSI及验证信息上传到运营商的服务器,服务器在完成注册动作之后,会以短信的形式将注册的结果下发到手机里。下发的内容会因条件不同而不同。 
    如果服务器在下发的短信中,不包含手机的号码,手机是无法取得电话号码。如果短信中包含了号码,手机才会将其缓存,以备他用.此外,对于其他运行商的SIM卡或者UIM卡,MDN有可能保存在UIM卡中。100%能够取得本机号码不太可能。 
    移动神州行,联通的卡是可以取到的.动感地带的取不到.别的卡还没有试过. 
    能够读取SIM卡号的话应该有前提.那就是SIM卡已经写入了本机号码,不然是无法读取的。

具体处理代码:(程序在模拟器上无法实现,必须连接手机)

  //创建电话管理

  TelephonyManager tm = (TelephonyManager)

  //与手机建立连接
  activity.getSystemService(Context.TELEPHONY_SERVICE);

  //获取手机号码

  String phoneId = tm.getLine1Number();

  //记得在manifest file中添加
    <uses-permission 
    android:name="android.permission.READ_PHONE_STATE" />


************************************************************************************************************************************************************


    http://www.eoeandroid.com/thread-91296-1-1.html

/**
* 电信APN列表
* @author wudongdong
*
*/
public class APNNET {
public static String CTWAP="ctwap";
public static String CTNET="ctnet";
}
/**
* 电信APN列表
* @author wudongdong
*
*/
public class APNNET {
public static String CTWAP="ctwap";
public static String CTNET="ctnet";
}
//获得APN的类型

/**
* 获得APN类型
* @author wudongdong
*
*/
  
public class ApnUtil {
private static Uri PREFERRED_APN_URI = Uri
.parse("content://telephony/carriers/preferapn");
  
/**
* get apntype
* @param context
* @return
*/
  
public static String getApnType(Context context){
String apntype="nomatch";
Cursor c = context.getContentResolver().query(PREFERRED_APN_URI,null, null, null, null);
c.moveToFirst();
String user=c.getString(c.getColumnIndex("user"));
if(user.startsWith(APNNET.CTNET)){
apntype=APNNET.CTNET;
}else if(user.startsWith(APNNET.CTWAP)){
apntype=APNNET.CTWAP;
}
return apntype;
}
}
  
/**
* 获得APN类型
* @author wudongdong
*
*/
public class ApnUtil {
private static Uri PREFERRED_APN_URI = Uri
.parse("content://telephony/carriers/preferapn");


/**
* get apntype
* @param context
* @return
*/
  
public static String getApnType(Context context){
String apntype="nomatch";
Cursor c = context.getContentResolver().query(PREFERRED_APN_URI,null, null, null, null);
c.moveToFirst();
String user=c.getString(c.getColumnIndex("user"));
if(user.startsWith(APNNET.CTNET)){
apntype=APNNET.CTNET;
}else if(user.startsWith(APNNET.CTWAP)){
apntype=APNNET.CTWAP;
}
return apntype;
}
}

 获得手机号码的话可以传IMSI码到指定接口,接口地址不方便说。但可以透露一点,必须走CTWAP,这也是判断APN类型的原因,发现很多应用如果APN是走代理的话就不能联网,那么再介绍一下用APN设置网络的代理信息。

Cursor c = context.getContentResolver().query(PREFERRED_APN_URI,null, null, null, null);
c.moveToFirst();
String proxy=c.getString(c.getColumnIndex("proxy"));

 
if (!"".equals(proxy) && proxy!=null) {
Properties prop = System.getProperties();
System.getProperties().put("proxySet", "true");
prop.setProperty("http.proxyHost", c.getString(c.getColumnIndex("proxy")));
prop.setProperty("http.proxyPort", c.getString(c.getColumnIndex("port")));
String authentication = c.getString(c.getColumnIndex("user"))
+ ":" + c.getString(c.getColumnIndex("password"));
String encodedLogin = Base64.encode(authentication);
uc.setRequestProperty("Proxy-Authorization", " BASIC "
+ encodedLogin);
}
  
c.close();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值