1、android APN 的打开与关闭 由于 Android 对于 APN 的网络 API 没有公开,不过我们可以阅读源代码,然后进行数据库操作,系统会自动监听数据库的变化,从而实现开启或者关闭 APN。大家可以研究一下 frameworks/base/core/java/android/provider/Telephony.java 这个类,比较重要的就是 URI 和数据库字段: content:/telephony/carriers字段可以在 Telephony.java 中找到。其实原理很简单 :1 、 当开启 APN 的时候,设置一个正确的移动或者联通的 APN2、 关闭的时候设置一个错
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 .Uri;import android.os.Bundle;import android.util.Log;import android.view.View;import android.
3、widget.Button;public class Main extends Activity Uri uri = Uri.parse(“content:/telephony/carriers“);Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);Button open= (Button) findViewById(R.id.open);Button close= (Button) findViewBy
4、Id(R.id.close);open.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) openAPN(););close.setOnClickListener(new View.OnClickListener() Overridepublic void onClick(View v) closeAPN(););public void openAPN()List list = getAPNList();for (APN apn : list) ContentValues cv =
5、 new ContentValues();cv.put(“apn“, APNMatchTools.matchAPN(apn.apn);cv.put(“type“, APNMatchTools.matchAPN(apn.type);getContentResolver().update(uri, cv, “_id=?“, new Stringapn.id);public void closeAPN()List list = getAPNList();for (APN apn : list) ContentValues cv = new ContentValues();cv.put(“apn“,
6、APNMatchTools.matchAPN(apn.apn)+“mdev“);cv.put(“type“, APNMatchTools.matchAPN(apn.type)+“mdev“);getContentResolver().update(uri, cv, “_id=?“, new Stringapn.id);private List getAPNList()String tag = “Main.getAPNList()“;/current 不为空表示可以使用的 APNString projection = “_id,apn,type,current“;Cursor cr = this
7、.getContentResolver().query(uri, projection, null, null, null);List list = new ArrayList();while(cr!=null 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();re
8、turn list;public static class APNString id;String apn;String type;APNMatchTools.javaJava 代码package cc.mdev.apn;public final class APNMatchTools public static class APNNetpublic static String CMWAP = “cmwap“;public static String CMNET = “cmnet“;/中国联通 3GWAP 设置 中国联通 3G 因特网设置 中国联通 WAP 设置 中国联通因特网设置/3gwap
9、 3gnet uniwap uninetpublic 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.toL
10、owerCase();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. 经过测试在 G1 上联通和移动卡均是成功的。