Android基于APN获取手机号的方法
本文实例讲述了Android基于APN获取手机号的方法。分享给大家供大家参考。具体如下:
之前很多人说无法完全获取手机号,是因为现在有的卡不能获取,有的卡能获取,现在我们可以换一种思路来考虑问题,就是用APN的方式。请看代码:
APNNET.java如下:
/**
*电信APN列表
*@authorwudongdong
*
*/
publicclassAPNNET{
publicstaticStringCTWAP="ctwap";
publicstaticStringCTNET="ctnet";
}
/**
*电信APN列表
*@authorwudongdong
*
*/
publicclassAPNNET{
publicstaticStringCTWAP="ctwap";
publicstaticStringCTNET="ctnet";
}
//获得APN的类型
/**
*获得APN类型
*@authorwudongdong
*
*/
publicclassApnUtil{
privatestaticUriPREFERRED_APN_URI=Uri
.parse("content://telephony/carriers/preferapn");
/**
*getapntype
*@paramcontext
*@return
*/
publicstaticStringgetApnType(Contextcontext){
Stringapntype="nomatch";
Cursorc=context.getContentResolver().query(PREFERRED_APN_URI,null,null,null,null);
c.moveToFirst();
Stringuser=c.getString(c.getColumnIndex("user"));
if(user.startsWith(APNNET.CTNET)){
apntype=APNNET.CTNET;
}elseif(user.startsWith(APNNET.CTWAP)){
apntype=APNNET.CTWAP;
}
returnapntype;
}
}
/**
*获得APN类型
*@authorwudongdong
*
*/
publicclassApnUtil{
privatestaticUriPREFERRED_APN_URI=Uri
.parse("content://telephony/carriers/preferapn");
/**
*getapntype
*@paramcontext
*@return
*/
publicstaticStringgetApnType(Contextcontext){
Stringapntype="nomatch";
Cursorc=context.getContentResolver().query(PREFERRED_APN_URI,null,null,null,null);
c.moveToFirst();
Stringuser=c.getString(c.getColumnIndex("user"));
if(user.startsWith(APNNET.CTNET)){
apntype=APNNET.CTNET;
}elseif(user.startsWith(APNNET.CTWAP)){
apntype=APNNET.CTWAP;
}
returnapntype;
}
}
Java代码如下:
/**
获得手机号码的话可以传IMSI码到指定接口,接口地址不方便说。但可以透露一点,必须走CTWAP,这也是判断APN类型的原因,发现很多应用如果APN是走代理的话就不能联网,那么再介绍一下用APN设置网络的代理信息。
*/
Cursorc=context.getContentResolver().query(PREFERRED_APN_URI,null,null,null,null);
c.moveToFirst();
Stringproxy=c.getString(c.getColumnIndex("proxy"));
if(!"".equals(proxy)&&proxy!=null){
Propertiesprop=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")));
Stringauthentication=c.getString(c.getColumnIndex("user"))
+":"+c.getString(c.getColumnIndex("password"));
StringencodedLogin=Base64.encode(authentication);
uc.setRequestProperty("Proxy-Authorization","BASIC"
+encodedLogin);
}
c.close();
希望本文所述对大家的Android程序设计有所帮助。