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>

摘自:http://www.iteye.com/problems/78511

更多相关文章

  1. [android][利用JNI技术在Android中调用、调试C++代码]
  2. 【Android 开发】: Android 消息处理机制之三: Handler 中 sendM
  3. 获取Android SDK 源代码并在Eclipse中关联查看的方法
  4. 如何调试跟踪Android源代码
  5. Android应用程序启动过程源代码分析(1)
  6. Google 将 Android 4.0.4 源代码送交 AOSP 开源项目
  7. android java代码的启动:app_process

随机推荐

  1. GitHub 优秀的 Android(安卓)开源项目
  2. Android Studio 2.0和Android Studio 2.1
  3. Android TextView背景色、圆角、内部填充
  4. android实例
  5. android 如何给图片添加水印
  6. android View的三个构造方法 简单总结
  7. android studio最新sdk更新方法
  8. Android Application - No window title,
  9. android剪切合并MP3音乐
  10. 【Android开发环境安装】