赞
踩
由于Android对于APN的网络API没有公开,不过我们可以阅读源代码,然后进行数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。
大家可以研究一下frameworks/base/core/java/android/provider/Telephony.java这个类,
比较重要的就是 URI 和数据库字段: content://telephony/carriers
字段可以在Telephony.java中找到。
其实原理很简单 :
1 、 当开启APN的时候,设置一个正确的移动或者联通的APN
2、 关闭的时候设置一个错误APN就会自动关闭网络
请看代码:Activity:
- 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;
-
-
- /**
- * 這裡是Activity
- * @author SinFrancis wong
- * @site http://mdev.cc
- * @wiki http://mdev.cc/wiki
- * @since 2010-01-08
- */
- public class Main extends Activity {
- /** Called when the activity is first created. */
- 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;
- }
-
- }
- package cc.mdev.apn;
-
-
-
- /**
- * 這裡是APN匹配,用於匹配移動或者聯通的APN
- * @author SinFrancis wong
- * @site http://mdev.cc
- * @wiki http://mdev.cc/wiki
- * @since 2010-01-08
- *
- */
- public final class APNMatchTools {
-
- public static class APNNet{
- /**
- * 中国移动cmwap
- */
- public static String CMWAP = "cmwap";
-
- /**
- * 中国移动cmnet
- */
- public static String CMNET = "cmnet";
-
- //中国联通3GWAP设置 中国联通3G因特网设置 中国联通WAP设置 中国联通因特网设置
- //3gwap 3gnet uniwap uninet
-
-
- /**
- * 3G wap 中国联通3gwap APN
- */
- public static String GWAP_3 = "3gwap";
-
- /**
- * 3G net 中国联通3gnet APN
- */
- public static String GNET_3="3gnet";
-
- /**
- * uni wap 中国联通uni wap APN
- */
- public static String UNIWAP="uniwap";
- /**
- * uni net 中国联通uni net APN
- */
- 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权限
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>
Android对于APN的网络API没有公开,不过我们可以阅读源代码,然后进行数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭APN。
content://telephony/carriers //取得全部apn列表
content://telephony/carriers/preferapn //取得当前设置的apn
content://telephony/carriers/current //取得current=1的apn列表
- public class APNList extends ListActivity implements OnItemClickListener{
- public static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
- public static final Uri PREFERRED_APN_URI =Uri.parse("content://telephony/carriers/preferapn");
- public static final Uri CURRENT_APN_URI =Uri.parse("content://telephony/carriers/current");
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- Cursor cr = this.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
- String[] mStrings=new String[cr.getCount()];
- int i=0;
- while(cr!=null && cr.moveToNext()){
- mStrings[i]="ID:"+cr.getString(cr.getColumnIndex("_id"))+
- "\n"+"name:"+cr.getString(cr.getColumnIndex("name"))+
- "\n"+"numeric:"+cr.getString(cr.getColumnIndex("numeric"))+
- "\n"+"mcc:"+cr.getString(cr.getColumnIndex("mcc"))+
- "\n"+"mnc:"+cr.getString(cr.getColumnIndex("mnc"))+
- "\n"+"apn:"+cr.getString(cr.getColumnIndex("apn"))+
- "\n"+"user:"+cr.getString(cr.getColumnIndex("user"))+
- "\n"+"server:"+cr.getString(cr.getColumnIndex("server"))+
- "\n"+"password:"+cr.getString(cr.getColumnIndex("password"))+
- "\n"+"proxy:"+cr.getString(cr.getColumnIndex("proxy"))+
- "\n"+"port:"+cr.getString(cr.getColumnIndex("port"))+
- "\n"+"mmsproxy:"+cr.getString(cr.getColumnIndex("mmsproxy"))+
- "\n"+"mmsport:"+cr.getString(cr.getColumnIndex("mmsport"))+
- "\n"+"mmsc:"+cr.getString(cr.getColumnIndex("mmsc"))+
- "\n"+"authtype:"+cr.getString(cr.getColumnIndex("authtype"))+
- "\n"+"type:"+cr.getString(cr.getColumnIndex("type"))+
- "\n"+"current:"+cr.getString(cr.getColumnIndex("current"));
-
- i++;
- }
-
- setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, mStrings));
- getListView().setOnItemClickListener(this);
- }
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- // TODO Auto-generated method stub
- SetDefaultAPN(arg2+1);
- }
-
- /*
- * Set an apn to be the default apn for web traffic
- * Require an input of the apn id to be set
- */
- public boolean SetDefaultAPN(int id)
- {
- boolean res = false;
- ContentResolver resolver = getContentResolver();
- ContentValues values = new ContentValues();
-
- //See /etc/apns-conf.xml. The TelephonyProvider uses this file to provide
- //content://telephony/carriers/preferapn URI mapping
- values.put("apn_id", id);
- Log.d("", "change1:");
- try
- {
- int cc=resolver.update(PREFERRED_APN_URI, values, null, null);
- Log.d("", "change:"+cc);
- }
- catch (SQLException e)
- {
- Log.d("", "change"+e.getMessage());
- }
- return res;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。