static Uri uri = Uri.parse("content://telephony/carriers");
static Context context;
public APNUtils(Context context){
this.context=context;
}
/**
* 检查网络
* @return
*/
public static boolean checkNet() {
boolean flag = false;
ConnectivityManager cwjManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cwjManager.getActiveNetworkInfo() != null) {
flag = cwjManager.getActiveNetworkInfo().isAvailable();
}
return flag;
}
/**
* 打开网络
*/
public static void openAPN() {
List<APN> list = getAPNList();
for (APN apn : list) {
ContentValues cv = new ContentValues();
cv.put("apn", APNMatchUtils.matchAPN(apn.apn));
cv.put("type", APNMatchUtils.matchAPN(apn.type));
context.getContentResolver().update(uri, cv, "_id=?",
new String[] { apn.id });
}
}
/**
* 关闭网络
*/
public static void closeAPN() {
List<APN> list = getAPNList();
for (APN apn : list) {
ContentValues cv = new ContentValues();
cv.put("apn", APNMatchUtils.matchAPN(apn.apn) + "mdev");
cv.put("type", APNMatchUtils.matchAPN(apn.type) + "mdev");
context.getContentResolver().update(uri, cv, "_id=?",
new String[] { apn.id });
}
}
private static List<APN> getAPNList() {
String tag = "Main.getAPNList()";
// current不为空表示可以使用的APN
String projection[] = { "_id,apn,type,current" };
Cursor cr = context.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;
}