android apn

由于Android对于APN的网络API没有公开,不过我们可以阅读源代码,然后进行数
据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。
大家可以研究一下frameworks/base/core/java/android/provider
/Telephony.java这个类,
比较重要的就是 URI 和数据库字段: content://telephony/carriers
字段可以在Telephony.java中找到。
其实原理很简单 :
1 、 当开启APN的时候,设置一个正确的移动或者联通的APN
2、 关闭的时候设置一个错误APN就会自动关闭网络
看代码:Activity:

Java代码
package cc.mdev.apn;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {

Uri uri = Uri.parse("content://telephony/carriers");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button open= (Button) findViewById(R.id.open);
Button close= (Button) findViewById(R.id.close);
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openAPN();
}
});
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
closeAPN();
}
});
}
public 
void openAPN(){
List<APN> list = getAPNList();
for (APN apn : list) {
ContentValues cv = new ContentValues();
cv.put("apn", APNMatchTools.matchAPN(apn.apn));
cv.put("type", APNMatchTools.matchAPN(apn.type));
getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id});
}
}
public void closeAPN(){
List<APN> list = getAPNList();
for (APN apn : list) {
ContentValues cv = new ContentValues();
cv.put("apn", APNMatchTools.matchAPN(apn.apn)+"mdev");
cv.put("type", APNMatchTools.matchAPN(apn.type)+"mdev");
getContentResolver().update(uri, cv, "_id=?", new String[]{apn.id});
}
}
private List<APN> getAPNList(){
String tag = "Main.getAPNList()";
//current不为空表示可以使用的APN
String  projection[] = {"_id,apn,type,current"};
Cursor cr = this.getContentResolver().query(uri, projection, null, null, null);
List<APN> list = new ArrayList<APN>();
while(cr!=null && cr.moveToNext()){
Log.d(tag, cr.getString(cr.getColumnIndex("_id")) + "  " + cr.getString(cr.getColumnIndex("apn")) + "  " + cr.getString(cr.getColumnIndex("type"))+ "  " + cr.getString(cr.getColumnIndex("current")));
APN a = new APN();
a.id = cr.getString(cr.getColumnIndex("_id"));
a.apn = cr.getString(cr.getColumnIndex("apn"));
a.type = cr.getString(cr.getColumnIndex("type"));
list.add(a);
}
if(cr!=null)
cr.close();
return list;
}
public static class APN{
String id;
String apn;
String type;
}
}


APNMatchTools.java
Java代码
package cc.mdev.apn;

public final class APNMatchTools {
public static class APNNet{

public static String CMWAP = "cmwap";

public static String CMNET = "cmnet";
//中国联通3GWAP设置              中国联通3G因特网设置              中国联通WAP设置              中国联通因特网设置
//3gwap                                3gnet                              uniwap                      uninet

public static String GWAP_3 = "3gwap";

public static String GNET_3="3gnet";

public static String UNIWAP="uniwap";

public static String UNINET="uninet";
}
public static String matchAPN(String currentName) {
if("".equals(currentName) || null==currentName){
return "";
}
currentName = currentName.toLowerCase();
if(currentName.startsWith(APNNet.CMNET))
return APNNet.CMNET;
else if(currentName.startsWith(APNNet.CMWAP))
return APNNet.CMWAP;
else if(currentName.startsWith(APNNet.GNET_3))
return APNNet.GNET_3;
else if(currentName.startsWith(APNNet.GWAP_3))
return APNNet.GWAP_3;
else if(currentName.startsWith(APNNet.UNINET))
return APNNet.UNINET;
else if(currentName.startsWith(APNNet.UNIWAP))
return APNNet.UNIWAP;
else if(currentName.startsWith("default"))
return "default";
else return "";
// return currentName.substring(0, currentName.length() - SUFFIX.length());
}
}

  最后不要忘记加上修改APN的权限:
Xml代码
    1. <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission> 
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>
  经过测试在G1 上联通和移动卡均是成功的。

id 从1开始计数,
name Telkila, china mobile
numeric 310260
mcc 310
mnc 995
apn cmwap, cmnet
user *
server *
password *
proxy null
port null
mmsproxy null
mmsport null
mmsc null
authtype -1
type null
current null或者1,1表示当前激活的


以下是字段部分源码
 public static final Uri CONTENT_URI =
            Uri.parse("content://telephony/carriers");

        /**
         * The default sort order for this table
         */
        public static final String DEFAULT_SORT_ORDER = "name ASC";

        public static final String NAME = "name";

        public static final String APN = "apn";

        public static final String PROXY = "proxy";

        public static final String PORT = "port";

        public static final String MMSPROXY = "mmsproxy";

        public static final String MMSPORT = "mmsport";

        public static final String SERVER = "server";

        public static final String USER = "user";

        public static final String PASSWORD = "password";

        public static final String MMSC = "mmsc";

        public static final String MCC = "mcc";

        public static final String MNC = "mnc";

        public static final String NUMERIC = "numeric";

        public static final String AUTH_TYPE = "authtype";

        public static final String TYPE = "type";

        public static final String INACTIVE_TIMER = "inactivetimer";

        // Only if enabled try Data Connection.
        public static final String ENABLED = "enabled";

        // Rules apply based on class.
        public static final String CLASS = "class";

        /**
         * The protocol to be used to connect to this APN.
         *
         * One of the PDP_type values in TS 27.007 section 10.1.1.
         * For example, "IP", "IPV6", "IPV4V6", or "PPP".
         */
        public static final String PROTOCOL = "protocol";

        /**
          * The protocol to be used to connect to this APN when roaming.
          *
          * The syntax is the same as protocol.
          */
        public static final String ROAMING_PROTOCOL = "roaming_protocol";

        public static final String CURRENT = "current";

        /**
          * Current status of APN
          * true : enabled APN, false : disabled APN.
          */
        public static final String CARRIER_ENABLED = "carrier_enabled";

        /**
          * Radio Access Technology info
          * To check what values can hold, refer to ServiceState.java.
          * This should be spread to other technologies,
          * but currently only used for LTE(14) and EHRPD(13).
          */
        public static final String BEARER = "bearer";



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值