为什么 htc one m8M8 不能连入电信网络接入点 ctnet

114网址导航android(117)
网上看了很多资料,再加上自己的理解和探索,终于实现了添加接入点设置和将当前网络设置为自己指定的网络接入点。废话不多说,上源码,大家分享下:(在moto-xt800,CDMA下进行了测试)
1.准备工作:
AndroidManifest。xml文件里添加以下三个权限设置:
&uses-permission
android:name=&android.permission.WRITE_APN_SETTINGS&&&/uses-permission&
&uses-permission
android:name=&android.permission.INTERNET&&&/uses-permission&
&uses-permission
android:name=&android.permission.ACCESS_NETWORK_STATE&
开始上代码了,很期待吧?
2.新建一个类ApnNode,算是属性设置吧
class ApnNode {
&&private&&
&&&&&private&
&&&&&private
&&&&&private&
&&&&&private&
&&&&&private&
&&&&&private&
&&&&&private&
&&&&&private&
&&&&&private&
&&&&&private&
&&&&&private&
&&&&&private&
&&&&&private
&&&&&&* @return the name
&&&&&public
String getName() {
&&&&&&&&&return
&&&&&&* @param name the name to set
&&&&&public
void setName(String name) {
&&&&&&&&&this.name =
&&&&&&* @return the apn
&&&&&public
String getApn() {
&&&&&&&&&return
&&&&&&* @param apn the apn to set
&&&&&public
void setApn(String apn) {
&&&&&&&&&this.apn =
&&&&&&* @return the proxy
&&&&&public
String getProxy() {
&&&&&&&&&return
&&&&&&* @param proxy the proxy to set
&&&&&public
void setProxy(String proxy) {
&&&&&&&&&this.proxy =
&&&&&&* @return the port
&&&&&public
String getPort() {
&&&&&&&&&return
&&&&&&* @param port the port to set
&&&&&public
void setPort(String port) {
&&&&&&&&&this.port =
&&&&&&* @return the user
&&&&&public
String getUser() {
&&&&&&&&&return
&&&&&&* @param user the user to set
&&&&&public
void setUser(String user) {
&&&&&&&&&this.user =
&&&&&&* @return the server
&&&&&public
String getServer() {
&&&&&&&&&return
&&&&&&* @param server the server to set
&&&&&public
void setServer(String server) {
&&&&&&&&&this.server =
&&&&&&* @return the password
&&&&&public
String getPassword() {
&&&&&&&&&return
&&&&&&* @param password the password to set
&&&&&public
void setPassword(String password) {
&&&&&&&&&this.password =
&&&&&&* @return the mmsc
&&&&&public
String getMmsc() {
&&&&&&&&&return
&&&&&&* @param mmsc the mmsc to set
&&&&&public
void setMmsc(String mmsc) {
&&&&&&&&&this.mmsc =
&&&&&&* @return the mmsproxy
&&&&&public
String getMmsproxy() {
&&&&&&&&&return
&&&&&&* @param mmsproxy the mmsproxy to set
&&&&&public
void setMmsproxy(String mmsproxy) {
&&&&&&&&&this.mmsproxy =
&&&&&&* @return the mmsport
&&&&&public
String getMmsport() {
&&&&&&&&&return
&&&&&&* @param mmsport the mmsport to set
&&&&&public
void setMmsport(String mmsport) {
&&&&&&&&&this.mmsport =
&&&&&&* @return the mcc
&&&&&public
String getMcc() {
&&&&&&&&&return
&&&&&&* @param mcc the mcc to set
&&&&&public
void setMcc(String mcc) {
&&&&&&&&&this.mcc =
&&&&&&* @return the mnc
&&&&&public
String getMnc() {
&&&&&&&&&return
&&&&&&* @param mnc the mnc to set
&&&&&public
void setMnc(String mnc) {
&&&&&&&&&this.mnc =
&&&&&&* @return the numeric
&&&&&public
String getNumeric() {
&&&&&&&&&return
&&&&&&* @param numeric the numeric to set
&&&&&public
void setNumeric(String numeric) {
&&&&&&&&&this.numeric =
&&&&&&* @return the type
&&&&&public
String getType() {
&&&&&&&&&return
&&&&&&* @param type the type to set
&&&&&public
void setType(String type) {
&&&&&&&&&this.type =
如果里面的函数名不清楚的话,网上找APN设置,一大堆的资料
3.主界面里的代码为:
class ApnNew extends
Activity {
&@Override
void onCreate(Bundle savedInstanceState){
&&super.onCreate(savedInstanceState);
&&setContentView(R.layout.apnnew);
&&setbtn = (Button)this.findViewById(R.id.setApn);
&&setbtn.setOnClickListener(setlis);
&&insertbtn = (Button)this.findViewById(R.id.insertApn);
&&insertbtn.setOnClickListener(insertlis);
&&apnexist = (Button)this.findViewById(R.id.Apnexist);
&&apnexist.setOnClickListener(existlis);
OnClickListener setlis = new
OnClickListener(){
&&@Override
void onClick(View v) {
&&&setCmwapAPN();
OnClickListener insertlis = new
OnClickListener(){
&&@Override
void onClick(View v) {
&&&InsetAPN();
&&&if(apnd_id !=
&&&&Toast.makeText(ApnNew.this, String.valueOf(apnd_id), Toast.LENGTH_SHORT).show();
&&&&Toast.makeText(ApnNew.this,
&insertAPN is unsuccess&, Toast.LENGTH_SHORT).show();
OnClickListener existlis = new
OnClickListener(){
&&@Override
void onClick(View v) {
ri = checkAPN();
&&&if(ri == -1)
&&&&Toast.makeText(ApnNew.this,
&不存在此APN&, Toast.LENGTH_SHORT).show();
&&&&Toast.makeText(ApnNew.this,
&存在此APN&, Toast.LENGTH_SHORT).show();
&&&&&* 入口
&&&&&* 设置指定的APN接入点
&&&&public
void setCmwapAPN()
(!isCmwap())
net_type = getNetWorkType();
&&&&m_oldNetWorkType = net_
((phoneSettedApnID = checkAPN()) != -1)
&&&&&SetDefaultAPN(phoneSettedApnID);
&&&&&InsetAPN();
&&&&&SetDefaultAPN(apnd_id);
&&&&Thread.sleep(8000L);
(Exception e)
&&&Log.i(&setCmwapAPN error&, e.getMessage());
&&&&private
int apnd_id = 0;
m_oldApnId = -1;
m_oldNetWorkType = -1;
&&&&private
int phoneSettedApnID = -1;
&&&&private
static final
Uri APN_TABLE_URI = Uri.parse(&content://telephony/carriers&);
static final
Uri PREFERRED_APN_URI = Uri.parse(&content://telephony/carriers/preferapn&);
&&&&private
String getMCC()
&&TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
&&String numeric = tm.getSimOperator();
&&String mcc = numeric.substring(0,
&&Log.i(&MCC& is&, mcc);
String getMNC()
&&TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
&&String numeric = tm.getSimOperator();
&&String mnc = numeric.substring(3, numeric.length());
&&Log.i(&MNC& is&, mnc);
String getSimOperator()
&&TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
&&String SimOperator = tm.getSimOperator();
ApnNode getDefaultAPN()
&&String id =
&&String apn =
&&String proxy =
&&String name =
&&String port =
&&String type =
&&String mcc =
&&String mnc =
&&String numeric =
&&ApnNode apnNode =
new ApnNode();
&&Cursor mCursor = getContentResolver().query(PREFERRED_APN_URI,
null, null,
null, null);
(mCursor == null)
(mCursor != null
&& mCursor.moveToNext())
&&&id = mCursor.getString(mCursor.getColumnIndex(&_id&));
&&&name = mCursor.getString(mCursor.getColumnIndex(&name&));
&&&apn = mCursor.getString(mCursor.getColumnIndex(&apn&)).toLowerCase();
&&&proxy = mCursor.getString(mCursor.getColumnIndex(&proxy&));
&&&port = mCursor.getString(mCursor.getColumnIndex(&port&));
&&&mcc = mCursor.getString(mCursor.getColumnIndex(&mcc&));
&&&mnc = mCursor.getString(mCursor.getColumnIndex(&mnc&));
&&&numeric = mCursor.getString(mCursor.getColumnIndex(&numeric&));
&&&Log.d(&getDefaultAPN&,
&default Apn info:&
+ id + &_& + name +
&_& + apn +
&_& + proxy + &_&
+ proxy);
&&phoneSettedApnID = Integer.valueOf(id);
&&apnNode.setName(name);
&&apnNode.setApn(apn);
&&apnNode.setProxy(proxy);
&&apnNode.setPort(port);
&&apnNode.setMcc(mcc);
&&apnNode.setMnc(mnc);
&&apnNode.setNumeric(numeric);
&&&&public
boolean isCurretApn()
&&ApnNode apnNode =
new ApnNode();
&&apnNode.setName(&中国电信CTNET&);
&&apnNode.setApn(&ctnet&);
&&apnNode.setProxy(&10.0.0.200&);
&&apnNode.setPort(&80&);
&&apnNode.setMcc(getMCC());
&&apnNode.setMnc(getMNC());
&&apnNode.setNumeric(getSimOperator());
&&ApnNode checkApn = getDefaultAPN();
((apnNode.getApn().equals(checkApn.getApn()) && apnNode.getMcc().equals(checkApn.getMcc()) && apnNode.getMnc().equals(checkApn.getMnc()) && apnNode.getNumeric().equals(
&&&&checkApn.getNumeric()))
&&&&&& (checkApn.getType() ==
&default&.equals(checkApn.getType()) ||
&&.equals(checkApn.getType())))
&&m_oldApnId = phoneSettedApnID;
&&* 获得网络连接管理
&&* @return
ConnectivityManager getConnectManager()
&&ConnectivityManager m_ConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
m_ConnectivityM
&&* 获得当前联网类型wifi or mobile
&&* @return
int getNetWorkType()
(getConnectManager() != null)
&&&NetworkInfo networkInfo = getConnectManager().getActiveNetworkInfo();
(networkInfo != null)
&&&&return
networkInfo.getType();
&&&&private
boolean isCmwap()
net_type = getNetWorkType();
(net_type == ConnectivityManager.TYPE_MOBILE)
isCurretApn();
if (net_type == ConnectivityManager.TYPE_WIFI)
&&&&public
boolean setDefaultApn(int
res = false;
&&ContentResolver resolver = getContentResolver();
&&ContentValues values =
new ContentValues();
&&values.put(&apn_id&, apnId);
&&&resolver.update(PREFERRED_APN_URI, values,
null, null);
&&&Cursor c = resolver.query(PREFERRED_APN_URI,
new String[] {
&name&, &apn&
+ apnId, null,
(c != null)
&&&&c.close();
(SQLException e)
&&&&public
void SetDefaultAPN(int
&&setDefaultApn(id);
&&ConnectivityManager cm = (ConnectivityManager)
this.getSystemService(CONNECTIVITY_SERVICE);
&&cm.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
&&Cursor cursor = getContentResolver().query(PREFERRED_APN_URI,
null, null,
null, null);
rows = cursor.getCount();
&&cursor.moveToFirst();
i = 0; i & i++)
&&&apn = cursor.getString(1);
&&&Log.e(&----------------&, apn);
&&&cursor.moveToNext();
&&&&public
int checkAPN()
&&ApnNode checkApn =
new ApnNode();
&&checkApn.setName(&中国电信ctnet&);
&&checkApn.setApn(&ctnet&);
&&checkApn.setMcc(getMCC());
&&checkApn.setMnc(getMNC());
&&checkApn.setNumeric(getSimOperator());
isApnExisted(checkApn);
&&&&public
int isApnExisted(ApnNode apnNode)
apnId = -1;
&&Cursor mCursor = getContentResolver().query(APN_TABLE_URI,
null, null,
null, null);
(mCursor != null
&& mCursor.moveToNext())
&&&apnId = mCursor.getShort(mCursor.getColumnIndex(&_id&));
&&&String name = mCursor.getString(mCursor.getColumnIndex(&name&));
&&&String apn = mCursor.getString(mCursor.getColumnIndex(&apn&));
&&&String type = mCursor.getString(mCursor.getColumnIndex(&type&));
&&&String proxy = mCursor.getString(mCursor.getColumnIndex(&proxy&));
&&&String port = mCursor.getString(mCursor.getColumnIndex(&port&));
&&&String current = mCursor.getString(mCursor.getColumnIndex(&current&));
&&&String mcc = mCursor.getString(mCursor.getColumnIndex(&mcc&));
&&&String mnc = mCursor.getString(mCursor.getColumnIndex(&mnc&));
&&&String numeric = mCursor.getString(mCursor.getColumnIndex(&numeric&));
&&&Log.e(&isApnExisted&,
&info:& + apnId +
&_& + name +
&_& + apn + &_&
+ type + &_&
+ current + &_&
+ proxy);
((apnNode.getApn().equals(apn) && apnNode.getMcc().equals(mcc) && apnNode.getMnc().equals(mnc) && apnNode.getNumeric().equals(numeric))
&&&&&&& (type ==
&default&.equals(type) ||
&&.equals(type)))
&&&&return
&&&&apnId = -1;
&&&&public
int addNewApn(ApnNode apnNode)
apnId = -1;
&&ContentResolver resolver = getContentResolver();
&&ContentValues values =
new ContentValues();
&&values.put(&name&, apnNode.getName());
&&values.put(&apn&, apnNode.getApn());
&&values.put(&proxy&, apnNode.getProxy());
&&values.put(&port&, apnNode.getPort());
&&values.put(&user&, apnNode.getUser());
&&values.put(&password&, apnNode.getPassword());
&&values.put(&mcc&, apnNode.getMcc());
&&values.put(&mnc&, apnNode.getMnc());
&&values.put(&numeric&, apnNode.getNumeric());
&&Cursor c =
&&&Uri newRow = resolver.insert(APN_TABLE_URI, values);
(newRow != null)
&&&&c = resolver.query(newRow,
null, null,
null, null);
idindex = c.getColumnIndex(&_id&);
&&&&c.moveToFirst();
&&&&apnId = c.getShort(idindex);
&&&&Log.d(&Robert&,
&New ID: & + apnId +
&: Inserting new APN succeeded!&);
(SQLException e)
(c != null)
&&&c.close();
&&&&private
void InsetAPN()
&&ApnNode checkApn =
new ApnNode();
&&checkApn.setName(&中国电信CTNET&);
&&checkApn.setApn(&ctnet&);
&&checkApn.setUser(&card&);
&&checkApn.setPassword(&card&);
&&checkApn.setMcc(getMCC());
&&checkApn.setMnc(getMNC());
&&checkApn.setNumeric(getSimOperator());
&&apnd_id = addNewApn(checkApn);
4.主界面的xml文件为:
version=&1.0&
encoding=&utf-8&?&
&LinearLayout
android:id=&@+id/widget28&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:orientation=&vertical&
xmlns:android=&/apk/res/android&
android:id=&@+id/setApn&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:text=&设置Apn&
android:id=&@+id/insertApn&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:text=&插入一个APN&
android:id=&@+id/Apnexist&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:text=&判断指定APN是否存在&
&/LinearLayout&
只要是开发过android的肯定清楚这些代码和界面吧,我也不多解释了,如果哥们你看不懂,那就是说明你还没弄清android开发,先去补补课再来看
文章转自:http://blog.csdn.net/napolun007/article/details/5748595
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:513036次
积分:5205
积分:5205
排名:第4612名
原创:27篇
转载:434篇
评论:35条
(1)(2)(1)(1)(1)(2)(1)(8)(2)(8)(1)(1)(2)(3)(3)(7)(5)(2)(29)(135)(25)(35)(34)(21)(3)(22)(64)(40)(2)}

我要回帖

更多关于 htc one m8 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信