public
static
boolean
gprsSetter
(Context context) {
boolean
isOpen = gprsIsOpenMethod(context);
if
(isOpen) {
setGprsEnabled(context,
false
);
System.out.println(
"关闭"
);
}
else
{
setGprsEnabled(context,
true
);
System.out.println(
"开启"
);
return
!isOpen;
public
static
boolean
gprsIsOpenMethod
(Context context) {
ConnectivityManager mCM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class cmClass = mCM.getClass();
Class[] argClasses =
null
;
Object[] argObject =
null
;
Boolean isOpen =
false
;
try
{
Method method = cmClass.getMethod(
"getMobileDataEnabled"
, argClasses);
isOpen = (Boolean) method.invoke(mCM, argObject);
}
catch
(Exception e) {
e.printStackTrace();
return
isOpen;
public
static
void
setGprsEnabled
(Context context,
boolean
isEnable) {
ConnectivityManager mCM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class cmClass = mCM.getClass();
Class[] argClasses =
new
Class[
1
];
argClasses[
0
] =
boolean
.class;
try
{
Method method = cmClass.getMethod(
"setMobileDataEnabled"
, argClasses);
method.invoke(mCM, isEnable);
}
catch
(Exception e) {
e.printStackTrace();
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
工具类代码如下:package com.example.x.changegprs;import android.content.Context;import android.net.ConnectivityManager;import java.lang.reflect.Method;/** * Created by X on 2016/8/25. */public class Tools
import java.lang.reflect.Method;
import
android
.content.Context;
import
android
.net.ConnectivityManager;
public class M
public static void setDataEnabled(int slotIdx, boolean enable,Context context) throws Exception
try {
int subid = SubscriptionManager.from(cont...
在安卓5.0之前
打开
gprs
通常都是通过 反射调用 ConnectivityManager 的setMobileDataEnabled方法
打开
流量
//检测
GPRS
是否
打开
private static boolean
gprs
IsOpenMethod(ConnectivityManager mCM ,String methodName)
Class cm