操作APN的工具类

来源:互联网 发布:数据分析报告怎么 编辑:程序博客网 时间:2024/05/21 17:20

工具类

package com.example.apnutil;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.telephony.TelephonyManager;import android.util.Log;public class ApnUtil {private Context context;private int apnd_id = 0;int m_oldApnId = -1;int m_oldNetWorkType = -1;private TelephonyManager tm;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");public ApnUtil(Context context) {super();this.context = context;resolver = context.getContentResolver();tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);}/** * 判断一个apn是否存在 存在返回id *  * @param apnNode * @return */public int isApnExisted(ApnEntity 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);// 遍历了所有的apnif (/* 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 ApnEntity getDefaultAPN() {String id = "";String apn = "";String proxy = "";String name = "";String port = "";String type = "";String mcc = "";String mnc = "";String numeric = "";ApnEntity apnNode = new ApnEntity();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(ApnEntity 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;}public String getMCC() {String numeric = tm.getSimOperator();String mcc = numeric.substring(0, 3);Log.i("MCC  is", mcc);return mcc;}public String getMNC() {String numeric = tm.getSimOperator();String mnc = numeric.substring(3, numeric.length());Log.i("MNC  is", mnc);return mnc;}public String getSimOperator() {String SimOperator = tm.getSimOperator();return SimOperator;}}

实体

package com.example.apnutil;/** * apn实体 */public class ApnEntity {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;}}

测试activity

package com.example.apnutil;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;public class MainActivity extends Activity {private ApnUtil apnUtil;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);apnUtil = new ApnUtil(this);//initApn();restoreApn();}/** * 恢復爲移動的apn */private void restoreApn() {ApnEntity checkApn = new ApnEntity();checkApn.setName("中国移动");checkApn.setApn("cmnet");checkApn.setUser("");checkApn.setPassword("");checkApn.setMcc(apnUtil.getMCC());checkApn.setMnc(apnUtil.getMNC());checkApn.setNumeric(apnUtil.getSimOperator());int apnId = apnUtil.isApnExisted(checkApn);if (apnId==-1) {Log.e("apn", "不存在apn创建一个"+apnId);apnId = apnUtil.addNewApn(checkApn);}apnUtil.setDefaultApn(apnId);        }/** * 初始化一個apn 并设置为默认 */private void initApn() {ApnEntity checkApn = new ApnEntity();checkApn.setName("三方平台接口");checkApn.setApn("lsrs.qd.sd");checkApn.setUser("");checkApn.setPassword("");checkApn.setMcc(apnUtil.getMCC());checkApn.setMnc(apnUtil.getMNC());checkApn.setNumeric(apnUtil.getSimOperator());int apnId = apnUtil.isApnExisted(checkApn);if (apnId==-1) {Log.e("apn", "不存在apn创建一个"+apnId);apnId = apnUtil.addNewApn(checkApn);}apnUtil.setDefaultApn(apnId);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}


原创粉丝点击