首页>代码>android设置移动联通电信wap接入点>/SetApn/src/com/bon/setapn/SetCmWap.java
package com.bon.setapn;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * 移动wap apn
 * @author lei
 *
 */
public class SetCmWap {

	 //添加APN共享数据类的Uri
	 final static String APN_URI = "content://telephony/carriers";   
	 //设置默认APN选项的数据共享APN
	 final static String MAIN_APN = "content://telephony/carriers/preferapn";        
	 final static String MY_APN = "移动wap";
	 private static int apnId = 0;
	 private static SharedPreferences sharedPreferences;
		  
		  /**
			 * 添加新的APN选项并返回所添加的APNID
			 * @return  id 所添加APN的ID
			 */
			 private static int getAPNId(ContentResolver resolver) {
					  int id = -1;
					  ContentValues values = new ContentValues();
						  values.put("name", MY_APN);
						  values.put("apn", "cmWap");
					      values.put("proxy", "10.0.0.172");
					      values.put("port", "80");
						  values.put("numeric", "46002");
						  values.put("mcc", "460");
						  values.put("mnc", "02");
//					  ContentResolver resolver = getContentResolver();
					  Cursor c = null;
					  Uri newRow = resolver.insert(Uri.parse(APN_URI), values);
					  if (newRow != null) {
					   c = resolver.query(newRow, null, null, null, null);
					   int idIndex = c.getColumnIndex("_id");
					   c.moveToFirst();
					   id = c.getShort(idIndex);
					  }
					  if (c != null) {
					   c.close();
					  }
					 
					  return id;
			 }
			 
			 /**
			  * 设置默认的APN选项
			  * @param id 所要设置APN的系统数据库中所对应的ID
			  * 相关说明:通过数据共享类进行数据  共享Uri地址是:content://telephony/carriers/preferapn
			  */
			 public static void setWapAPN(ContentResolver resolver, SharedPreferences _sharedPreferences) {
			  ContentValues values = new ContentValues();
			  
			  sharedPreferences = _sharedPreferences;
				 apnId = sharedPreferences.getInt("apnId", 0);
					if(apnId==0){
						 int id = getAPNId(resolver);
						Log.i("MainActivity", "重新创建了一个");
						values.put("apn_id", id);
						 Editor editor=sharedPreferences.edit();    //创建一个并保存id
						  editor.putInt("apnId", id);
						  editor.commit();
					}else {
						Log.i("MainActivity", "设置以前的");
						values.put("apn_id", apnId);
					}
			  
			  
			  resolver.update(Uri.parse(MAIN_APN), values, null, null);
			 }
			 /**
			  * 重新创建一个接入点  并设置
			  * @param resolver
			  * @param _sharedPreferences
			  */
			 //防止用户自己删除以前创建的    删除之后   设置不了
			 public static void createWapApnAndSet(ContentResolver resolver, SharedPreferences _sharedPreferences) {
				  ContentValues values = new ContentValues();
				  
				  sharedPreferences = _sharedPreferences;
					 apnId = sharedPreferences.getInt("apnId", 0);
					 Log.i("MainActivity", "保存的id"+apnId);
					 Log.i("MainActivity", "查到的id"+apnId);
					 Log.i("MainActivity", apnId+"");
					 int id = getAPNId(resolver);
					 Log.i("MainActivity", "重新创建了一个");
					 values.put("apn_id", id);
					 Editor editor=sharedPreferences.edit();   //创建并保存id
					 editor.putInt("apnId", id);
					 editor.commit();
				     resolver.update(Uri.parse(MAIN_APN), values, null, null);
				 }

}
最近下载更多
jnetzhou  LV1 2019年4月3日
zjrzhong  LV1 2018年1月12日
ly2009158  LV1 2016年3月10日
ahbruse  LV1 2016年1月14日
daniel_gjg  LV25 2015年10月19日
kmcc123  LV1 2015年8月27日
yls  LV1 2015年8月17日
leehove  LV17 2015年3月17日
shiyu001  LV2 2014年5月13日
YonggangYuan  LV1 2013年10月22日
最近浏览更多
杰6666 2022年6月17日
暂无贡献等级
eddie2260  LV2 2022年5月24日
氟西汀来救你  LV2 2022年4月26日
isfrand  LV4 2022年4月25日
a566566  LV9 2020年12月29日
waitingforyou  LV20 2020年7月8日
doremi  LV6 2020年6月18日
bawe11  LV1 2020年6月17日
weixiao  LV6 2020年5月27日
蜡炬成灰  LV6 2020年4月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友