android 打开/关闭 移动网络

时间:2021-10-20 23:49:30

android 没有对于APN网络的API没有公开,但是我们可以更新数据库,系统会自动监听数据库的变化,从而实现开启/关闭APN。

源代码中frameworks/base/core/java/android/provider/Telephony.java这个类,介绍了相关的URI和数据库字段:content://telephony/carriers

字段可以在Telephony.java中找到。

打开APN ,可以设置正确的APN参数;

关闭APN,可以设置错误的参数。



首先判断移动网络是否连接:

ConnectivityManager connectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == State.CONNECTED){
     true;
}else{
     false;
}


打开/关闭:

        /**
* 打开移动网络,通过设置一个正确的APN参数
* @param context
*/
public static void openApn(Context context){
ArrayList<APNInfos> apnList = new ArrayList<APNInfos>();
apnList = getAPNList(context);
for(APNInfos apnInfos : apnList){
log("-openApn--getApn-----" + apnInfos.getApn());
}
for(APNInfos apnInfos : apnList){
ContentValues values = new ContentValues();
values.put("apn",APNMatchTools.matchAPN(apnInfos.getApn()));
values.put("type",APNMatchTools.matchAPN(apnInfos.getType()));
context.getContentResolver().update(Contents.apnUri, values, "_id=?", new String[]{apnInfos.getId()});
}
}

/**
* 关闭移动网络,通过设置一个错误的APN参数
* @param context
*/
public static void closeApn(Context context){
ArrayList<APNInfos> apnList = new ArrayList<APNInfos>();
apnList = getAPNList(context);
for(APNInfos apnInfos : apnList){
log("--closeApn-getApn-----" + apnInfos.getApn());
}
for(APNInfos apnInfos : apnList){
ContentValues values = new ContentValues();
values.put("apn",APNMatchTools.matchAPN(apnInfos.getApn()) + "mdev");
values.put("type",APNMatchTools.matchAPN(apnInfos.getType()) + "mdev");
context.getContentResolver().update(Contents.apnUri, values, "_id=?", new String[]{apnInfos.getId()});
}
}

获取APN列表:

        /**
* 获取APN列表
* @param context
* @return
*/
public static ArrayList<APNInfos> getAPNList(Context context){
ArrayList<APNInfos> apnList = new ArrayList<APNInfos>();
String[] projection = {"_id,apn,type,current"};
Cursor cr = context.getContentResolver().query(Contents.apnUri, projection, null, null, null);
while(cr !=null && cr.moveToNext()){
if(cr.getString(cr.getColumnIndex("apn")).equals("")){

}else{
APNInfos apnInfos = new APNInfos();
apnInfos.setId(cr.getString(cr.getColumnIndex("_id")));
apnInfos.setApn(cr.getString(cr.getColumnIndex("apn")));
apnInfos.setType(cr.getString(cr.getColumnIndex("type")));
apnList.add(apnInfos);
}
}
if(cr != null){
cr.close();
}
return apnList;
}


APNMatchTools类:

public class APNMatchTools {



public static String matchAPN(String currentName){
if(currentName == null || currentName.equals("")){
return "";
}
currentName = currentName.toLowerCase();//变成小写字母
if(currentName.startsWith(Contents.CMNET)){
return Contents.CMNET;
}else if(currentName.startsWith(Contents.CMWAP)){
return Contents.CMWAP;
}else if(currentName.startsWith(Contents.GNET_3)){
return Contents.GNET_3;
}else if(currentName.startsWith(Contents.GWAP_3)){
return Contents.GWAP_3;
}else if(currentName.startsWith(Contents.UNINET)){
return Contents.UNINET;
}else if(currentName.startsWith(Contents.UNIWAP)){
return Contents.UNIWAP;
}else if(currentName.startsWith("default")){
return "default";
}else{
return "";
}

}

}

其中  几个变量

        public static final String CMWAP = "cmwap";   //中国移动cmwap 
public static final String CMNET = "cmnet"; //中国移动cmnet
public static final String GWAP_3 = "3gwap"; //中国联通3gwap
public static final String GNET_3 = "3gnet"; //中国联通3gnet
public static final String UNIWAP = "uniwap"; //中国联通uni wap
public static final String UNINET = "uninet"; //中国联通uni net


APNInfos类

public class APNInfos {
private String id, apn ,type;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getApn() {
return apn;
}

public void setApn(String apn) {
this.apn = apn;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}


}