操作APN的工具类

package com.testaijia.apn;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
 * 代码是人家的 我只是重新组合了一下 比较清晰
	http://blog.csdn.net/napolun007/article/details/5748595
 
 */
public class TestAijiaApnActivity extends Activity {

	private Button insertbtn;
	private TelephonyManager tm;
	private int apnd_id = 0;
	int m_oldApnId = -1;
	int m_oldNetWorkType = -1;
	private ContentResolver resolver;
	private static final Uri APN_TABLE_URI = Uri
			.parse("content://telephony/carriers");

	private static final Uri PREFERRED_APN_URI = Uri
			.parse("content://telephony/carriers/preferapn");

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		insertbtn = (Button) this.findViewById(R.id.insertApn);
		insertbtn.setOnClickListener(insertlis);
		resolver = getContentResolver();
		tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

	}

	private OnClickListener insertlis = new OnClickListener() {

		@Override
		public void onClick(View v) {

			deleteApn();// 先删除所有apn再添加
			apnd_id = InsetAPN();// 插入apn
			setDefaultApn(apnd_id);// 设置为默认apn
			getDefaultAPN();

			if (apnd_id != 0)
				Toast.makeText(TestAijiaApnActivity.this,
						String.valueOf(apnd_id), Toast.LENGTH_SHORT).show();
			else
				Toast.makeText(TestAijiaApnActivity.this,
						"insertAPN is unsuccess", Toast.LENGTH_SHORT).show();

		}

	};

	// 检查指定APN是否存在

	public int checkAPN() {
		ApnNode checkApn = new ApnNode();
		checkApn.setName("中国电信ctnet");
		checkApn.setApn("ctnet");
		checkApn.setMcc(getMCC());
		checkApn.setMnc(getMNC());
		checkApn.setNumeric(getSimOperator());
		return isApnExisted(checkApn);
	}

	// 添加一个APN
	private int InsetAPN() {
		ApnNode checkApn = new ApnNode();
		checkApn.setName("爱家物联专用接口");
		checkApn.setApn("ctnet");
		// checkApn.setProxy("10.0.0.200");
		// checkApn.setPort("80");
		checkApn.setUser("qdaj@qdaj.vpdn.sd");
		checkApn.setPassword("123456");
		checkApn.setMcc(getMCC());
		checkApn.setMnc(getMNC());
		checkApn.setNumeric(getSimOperator());
		return addNewApn(checkApn);

	}

	/**
	 * 判断一个apn是否存在 存在返回id
	 * 
	 * @param apnNode
	 * @return
	 */
	public int isApnExisted(ApnNode apnNode) {

		int apnId = -1;
		Cursor mCursor = resolver.query(APN_TABLE_URI, null, null, null, null);
		while (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);// 遍历了所有的apn
			if (/* apnNode.getName().equals(name) */(apnNode.getApn().equals(
					apn)
					&& apnNode.getMcc().equals(mcc)
					&& apnNode.getMnc().equals(mnc) && apnNode.getNumeric()
					.equals(numeric))
					&& (type == null || "default".equals(type) || ""
							.equals(type)))// || (apnNode.getApn().equals(apn)
											// && "".equals(proxy) &&
											// "".equals(port))
			{
				return apnId;
			} else {
				apnId = -1;
			}

		}
		return apnId;
	}

	/**
	 * 获取默认的apn
	 */
	public 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 = resolver.query(PREFERRED_APN_URI, null, null, null,
				null);
		if (mCursor == null) {
			// throw new Exception("Non prefer apn exist");
			return null;

		}
		while (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);

		}
		apnNode.setName(name);
		apnNode.setApn(apn);
		apnNode.setProxy(proxy);
		apnNode.setPort(port);
		apnNode.setMcc(mcc);
		apnNode.setMnc(mnc);
		apnNode.setNumeric(numeric);
		return apnNode;
	}

	/**
	 * 设置默认的apn
	 * 
	 * @param apnId
	 * @return
	 */

	public boolean setDefaultApn(int apnId) {
		boolean res = false;
		ContentValues values = new ContentValues();
		values.put("apn_id", apnId);

		try {
			resolver.update(PREFERRED_APN_URI, values, null, null);
			Cursor c = resolver.query(PREFERRED_APN_URI, new String[] { "name",
					"apn" }, "_id=" + apnId, null, null);
			if (c != null) {
				res = true;
				c.close();
			}
		} catch (SQLException e) {

		}
		return res;

	}

	/**
	 * 删除所有apn
	 */
	public void deleteApn() {

		resolver.delete(APN_TABLE_URI, null, null);

	}

	/**
	 * 增加新的apn
	 * 
	 * @param apnNode
	 * @return
	 */
	public int addNewApn(ApnNode apnNode) {
		int apnId = -1;
		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());
		// Note: this values need to be update, and for now, it only for XT800.

		Cursor c = null;
		try {

			Uri newRow = resolver.insert(APN_TABLE_URI, values);

			if (newRow != null) {
				c = resolver.query(newRow, null, null, null, null);

				int idindex = c.getColumnIndex("_id");
				c.moveToFirst();
				apnId = c.getShort(idindex);
				Log.d("Robert", "New ID: " + apnId
						+ ": Inserting new APN succeeded!");

			}
		} catch (SQLException e) {

		}

		if (c != null)
			c.close();

		return apnId;
	}

	private String getMCC() {
		String numeric = tm.getSimOperator();
		String mcc = numeric.substring(0, 3);
		Log.i("MCC  is", mcc);
		return mcc;
	}

	private String getMNC() {

		String numeric = tm.getSimOperator();
		String mnc = numeric.substring(3, numeric.length());
		Log.i("MNC  is", mnc);
		return mnc;
	}

	private String getSimOperator() {
		String SimOperator = tm.getSimOperator();
		return SimOperator;
	}

}


package com.testaijia.apn;
/**
 * apn实体
 */
public class ApnNode {

	private String name;

	private String apn;

	private String proxy;

	private String port;

	private String user;

	private String server;

	private String password;

	private String mmsc;

	private String mmsproxy;

	private String mmsport;

	private String mcc;

	private String mnc;

	private String numeric;

	private String type;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the apn
	 */
	public String getApn() {
		return apn;
	}

	/**
	 * @param apn
	 *            the apn to set
	 */
	public void setApn(String apn) {
		this.apn = apn;
	}

	/**
	 * @return the proxy
	 */
	public String getProxy() {
		return proxy;
	}

	/**
	 * @param proxy
	 *            the proxy to set
	 */
	public void setProxy(String proxy) {
		this.proxy = proxy;
	}

	/**
	 * @return the port
	 */
	public String getPort() {
		return port;
	}

	/**
	 * @param port
	 *            the port to set
	 */
	public void setPort(String port) {
		this.port = port;
	}

	/**
	 * @return the user
	 */
	public String getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(String user) {
		this.user = user;
	}

	/**
	 * @return the server
	 */
	public String getServer() {
		return server;
	}

	/**
	 * @param server
	 *            the server to set
	 */
	public void setServer(String server) {
		this.server = server;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the mmsc
	 */
	public String getMmsc() {
		return mmsc;
	}

	/**
	 * @param mmsc
	 *            the mmsc to set
	 */
	public void setMmsc(String mmsc) {
		this.mmsc = mmsc;
	}

	/**
	 * @return the mmsproxy
	 */
	public String getMmsproxy() {
		return mmsproxy;
	}

	/**
	 * @param mmsproxy
	 *            the mmsproxy to set
	 */
	public void setMmsproxy(String mmsproxy) {
		this.mmsproxy = mmsproxy;
	}

	/**
	 * @return the mmsport
	 */
	public String getMmsport() {
		return mmsport;
	}

	/**
	 * @param mmsport
	 *            the mmsport to set
	 */
	public void setMmsport(String mmsport) {
		this.mmsport = mmsport;
	}

	/**
	 * @return the mcc
	 */
	public String getMcc() {
		return mcc;
	}

	/**
	 * @param mcc
	 *            the mcc to set
	 */
	public void setMcc(String mcc) {
		this.mcc = mcc;
	}

	/**
	 * @return the mnc
	 */
	public String getMnc() {
		return mnc;
	}

	/**
	 * @param mnc
	 *            the mnc to set
	 */
	public void setMnc(String mnc) {
		this.mnc = mnc;
	}

	/**
	 * @return the numeric
	 */
	public String getNumeric() {
		return numeric;
	}

	/**
	 * @param numeric
	 *            the numeric to set
	 */
	public void setNumeric(String numeric) {
		this.numeric = numeric;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}
}


posted @ 2012-08-09 13:47  sfshine  阅读(258)  评论(0编辑  收藏  举报