stevenhu_223 / blog_20150508_1_6708817

来自CSDN博客:实现开启和关闭android移动网络(做AppWidget开发的收获) http://blog.csdn.net/stevenhu_223/article/details/7860964#

  最后更新时间 2015-05-08 13:14:10
blog_20150508_1_6708817 66行 Java
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
//创建一个匹配类,用于匹配移动、电信、联通的APN
public final class APNMatchTools
{

// 中国移动cmwap
public static String CMWAP = "cmwap";

// 中国移动cmnet
public static String CMNET = "cmnet";

// 中国联通3gwap APN

public static String GWAP_3 = "3gwap";

// 中国联通3gnet APN
public static String GNET_3 = "3gnet";

// 中国联通uni wap APN
public static String UNIWAP = "uniwap";

// 中国联通uni net APN
public static String UNINET = "uninet";

// 中国电信 ct wap APN
public static String CTWAP = "ctwap";

// 中国电信ct net APN
public static String CTNET = "ctnet";

public static String matchAPN(String currentName)
{

if ("".equals(currentName) || null == currentName)
{

return "";
}

// 参数转为小写
currentName = currentName.toLowerCase();
// 检查参数是否与各APN匹配,返回匹配值
if (currentName.startsWith(CMNET))
return CMNET;
else if (currentName.startsWith(CMWAP))
return CMWAP;
else if (currentName.startsWith(GNET_3))
return GNET_3;

else if (currentName.startsWith(GWAP_3))
return GWAP_3;
else if (currentName.startsWith(UNINET))
return UNINET;

else if (currentName.startsWith(UNIWAP))
return UNIWAP;
else if (currentName.startsWith(CTWAP))
return CTWAP;
else if (currentName.startsWith(CTNET))
return CTNET;
else if (currentName.startsWith("default"))
return "default";
else
return "";
}

}
blog_20150508_2_357979 100行 Java
Raw
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
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.util.Log;

public class ApnSwitchTest extends Activity
{

Uri uri = Uri.parse("content://telephony/carriers/preferapn");
// 开启APN
public void openAPN()
{
List<APN> list = getAPNList();
for (APN apn : list)
{
ContentValues cv = new ContentValues();

// 获取及保存移动或联通手机卡的APN网络匹配
cv.put("apn", APNMatchTools.matchAPN(apn.apn));
cv.put("type", APNMatchTools.matchAPN(apn.type));

// 更新系统数据库,改变移动网络状态
getContentResolver().update(uri, cv, "_id=?", new String[]
{
apn.id
});
}

}

// 关闭APN
public void closeAPN()
{
List<APN> list = getAPNList();
for (APN apn : list)
{
// 创建ContentValues保存数据
ContentValues cv = new ContentValues();
// 添加"close"匹配一个错误的APN,关闭网络
cv.put("apn", APNMatchTools.matchAPN(apn.apn) + "close");
cv.put("type", APNMatchTools.matchAPN(apn.type) + "close");

// 更新系统数据库,改变移动网络状态
getContentResolver().update(uri, cv, "_id=?", new String[]
{
apn.id
});
}
}
public static class APN
{
String id;

String apn;

String type;
}

private List<APN> getAPNList()
{
// current不为空表示可以使用的APN
String projection[] =
{
"_id, apn, type, current"
};
// 查询获取系统数据库的内容
Cursor cr = getContentResolver().query(uri, projection, null, null, null);

// 创建一个List集合
List<APN> list = new ArrayList<APN>();

while (cr != null && cr.moveToNext())
{

Log.d("ApnSwitch", "id" + cr.getString(cr.getColumnIndex("_id")) + " \n" + "apn"
+ cr.getString(cr.getColumnIndex("apn")) + "\n" + "type"
+ cr.getString(cr.getColumnIndex("type")) + "\n" + "current"
+ 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;
}

}<span style="font-family: 'Comic Sans MS'; "> </span>
blog_20150508_3_1390202 131行 Java
Raw
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;

public class MobileDataSwitchTest extends Activity
{

// 移动数据开启和关闭
public void setMobileDataStatus(Context context,boolean enabled)

{

ConnectivityManager conMgr = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);

//ConnectivityManager类

Class<?> conMgrClass = null;

//ConnectivityManager类中的字段
Field iConMgrField = null;
//IConnectivityManager类的引用
Object iConMgr = null;
//IConnectivityManager类
Class<?> iConMgrClass = null;
//setMobileDataEnabled方法
Method setMobileDataEnabledMethod = null;
try
{

//取得ConnectivityManager类
conMgrClass = Class.forName(conMgr.getClass().getName());
//取得ConnectivityManager类中的对象Mservice
iConMgrField = conMgrClass.getDeclaredField("mService");
//设置mService可访问
iConMgrField.setAccessible(true);
//取得mService的实例化类IConnectivityManager
iConMgr = iConMgrField.get(conMgr);
//取得IConnectivityManager类
iConMgrClass = Class.forName(iConMgr.getClass().getName());

//取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法
setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);

//设置setMobileDataEnabled方法是否可访问
setMobileDataEnabledMethod.setAccessible(true);
//调用setMobileDataEnabled方法
setMobileDataEnabledMethod.invoke(iConMgr, enabled);

}

catch(ClassNotFoundException e)
{

e.printStackTrace();
}
catch(NoSuchFieldException e)
{

e.printStackTrace();
}

catch(SecurityException e)
{
e.printStackTrace();

}
catch(NoSuchMethodException e)

{
e.printStackTrace();
}

catch(IllegalArgumentException e)
{

e.printStackTrace();
}

catch(IllegalAccessException e)
{

e.printStackTrace();
}

catch(InvocationTargetException e)

{

e.printStackTrace();

}

}


//获取移动数据开关状态


public boolean getMobileDataStatus(String getMobileDataEnabled)

{

ConnectivityManager cm;

cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);

Class cmClass = cm.getClass();
Class[] argClasses = null;
Object[] argObject = null;
Boolean isOpen = false;
try
{

Method method = cmClass.getMethod(getMobileDataEnabled, argClasses);

isOpen = (Boolean)method.invoke(cm, argObject);
}catch(Exception e)
{
e.printStackTrace();
}

return isOpen;

}
}