网上看了很多资料,再加上自己的理解和探索,终于实现了添加接入点设置和将当前网络设置为自己指定的网络接入点。废话不多说,上源码,大家分享下:(在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,算是属性设置吧
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;
}
}
如果里面的函数名不清楚的话,网上找APN设置,一大堆的资料
3.主界面里的代码为:
public class ApnNew extends Activity {
private Button setbtn;
private Button insertbtn;
private Button apnexist;
@Override
public 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);
}
private OnClickListener setlis = new OnClickListener(){
@Override
public void onClick(View v) {
setCmwapAPN();
}
};
private OnClickListener insertlis = new OnClickListener(){
@Override
public void onClick(View v) {
InsetAPN();
if(apnd_id != 0)
Toast.makeText(ApnNew.this, String.valueOf(apnd_id), Toast.LENGTH_SHORT).show();
else
Toast.makeText(ApnNew.this, "insertAPN is unsuccess", Toast.LENGTH_SHORT).show();
}
};
private OnClickListener existlis = new OnClickListener(){
@Override
public void onClick(View v) {
int ri = checkAPN();
if(ri == -1)
Toast.makeText(ApnNew.this, "不存在此APN", Toast.LENGTH_SHORT).show();
else
Toast.makeText(ApnNew.this, "存在此APN", Toast.LENGTH_SHORT).show();
}
};
/*
* 入口
* 设置指定的APN接入点
* */
public void setCmwapAPN()
{
try
{
if (!isCmwap())
{
int net_type = getNetWorkType();
m_oldNetWorkType = net_type;
if ((phoneSettedApnID = checkAPN()) != -1)
{
SetDefaultAPN(phoneSettedApnID);
}
else
{
InsetAPN();
SetDefaultAPN(apnd_id);
}
Thread.sleep(8000L);
}
}
catch (Exception e)
{
Log.i("setCmwapAPN error", e.getMessage());
}
}
private int apnd_id = 0;
int m_oldApnId = -1;
int m_oldNetWorkType = -1;
private int phoneSettedApnID = -1;//当前APNid
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");
private String getMCC()
{
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String numeric = tm.getSimOperator();
String mcc = numeric.substring(0, 3);
Log.i("MCC is", mcc);
return mcc;
}
private 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);
return mnc;
}
private String getSimOperator()
{
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String SimOperator = tm.getSimOperator();
return SimOperator;
}
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();
// Uri uri = Uri.parse("content://telephony/carriers/preferapn");
Cursor mCursor = getContentResolver().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);
}
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);
return apnNode;
}
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();
if ((apnNode.getApn().equals(checkApn.getApn()) && apnNode.getMcc().equals(checkApn.getMcc()) && apnNode.getMnc().equals(checkApn.getMnc()) && apnNode.getNumeric().equals(
checkApn.getNumeric()))
&& (checkApn.getType() == null || "default".equals(checkApn.getType()) || "".equals(checkApn.getType())))//|| (apnNode.getApn().equals(checkApn.getApn()) && checkApn.getProxy().equals("") && checkApn.getPort().equals(""))
{
return true;
}
m_oldApnId = phoneSettedApnID;
return false;
}
/**
* 获得网络连接管理
*
* @return
*/
private ConnectivityManager getConnectManager()
{
ConnectivityManager m_ConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return m_ConnectivityManager;
}
/**
* 获得当前联网类型wifi or mobile
*
* @return
*/
private int getNetWorkType()
{
if (getConnectManager() != null)
{
NetworkInfo networkInfo = getConnectManager().getActiveNetworkInfo();
if (networkInfo != null)
return networkInfo.getType();
return -1;
}
else
{
return -1;
}
}
private boolean isCmwap()
{
int net_type = getNetWorkType();
if (net_type == ConnectivityManager.TYPE_MOBILE)
{
return isCurretApn();
}
else if (net_type == ConnectivityManager.TYPE_WIFI)
{
return false;
}
return false;
}
public boolean setDefaultApn(int apnId)
{
boolean res = false;
ContentResolver resolver = getContentResolver();
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;
}
public void SetDefaultAPN(int id)
{
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);
int rows = cursor.getCount();
cursor.moveToFirst();
String apn;
for (int i = 0; i < rows; i++)
{
apn = cursor.getString(1);
Log.e("----------------", apn);
cursor.moveToNext();
}
}
//检查指定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);
}
public int isApnExisted(ApnNode apnNode)
{
int apnId = -1;
// Uri uri = Uri.parse("content://telephony/carriers/");
Cursor mCursor = getContentResolver().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);
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;
}
public int addNewApn(ApnNode apnNode)
{
int 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());
// 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;
}
//添加一个APN
private void InsetAPN()
{
ApnNode checkApn = new ApnNode();
checkApn.setName("中国电信CTNET");
checkApn.setApn("ctnet");
// checkApn.setProxy("10.0.0.200");
// checkApn.setPort("80");
checkApn.setUser("card");
checkApn.setPassword("card");
checkApn.setMcc(getMCC());
checkApn.setMnc(getMNC());
checkApn.setNumeric(getSimOperator());
apnd_id = addNewApn(checkApn);
}
}
4.主界面的xml文件为:
<?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="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/setApn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="设置Apn"
>
</Button>
<Button
android:id="@+id/insertApn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="插入一个APN"
>
</Button>
<Button
android:id="@+id/Apnexist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="判断指定APN是否存在"
>
</Button>
</LinearLayout>
只要是开发过android的肯定清楚这些代码和界面吧,我也不多解释了,如果哥们你看不懂,那就是说明你还没弄清android开发,先去补补课再来看