首页 > 代码库 > android 开关数据流量
android 开关数据流量
这个方法,具体代码如下:
[java] view plaincopyprint?
/**
* gprs狀態
* @param getMobileDataEnabled
* @return
*/
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;
}
/** * gprs狀態 * @param getMobileDataEnabled * @return */ 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; }
[java] view plaincopyprint?
/**
* 移动网络开关
*/
private void setMobileDataEnabled(Context context, boolean enabled) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?> conMgrClass = null; // ConnectivityManager类
Field iConMgrField = null; // ConnectivityManager类中的字段
Object iConMgr = null; // IConnectivityManager类的引用
Class<?> iConMgrClass = null; // IConnectivityManager类
Method setMobileDataEnabledMethod = null; // setMobileDataEnabled方法
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();
}
}
/** * 移动网络开关 */ private void setMobileDataEnabled(Context context, boolean enabled) { ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Class<?> conMgrClass = null; // ConnectivityManager类 Field iConMgrField = null; // ConnectivityManager类中的字段 Object iConMgr = null; // IConnectivityManager类的引用 Class<?> iConMgrClass = null; // IConnectivityManager类 Method setMobileDataEnabledMethod = null; // setMobileDataEnabled方法 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(); } }
出现这个错误找了半天~~~ 没找到解决的方法
[html] view plaincopyprint?
java.lang.NoSuchMethodException: setMobileDataEnabled [boolean]
java.lang.NoSuchMethodException: setMobileDataEnabled [boolean]
后来终于网上找到一个方法 试下发现可以
[java] view plaincopyprint?
private void toggleMobileData(Context context, boolean enabled){
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Method setMobileDataEnabl;
try {
setMobileDataEnabl = connectivityManager.getClass().getDeclaredMethod("setMobileDataEnabled", boolean.class);
setMobileDataEnabl.invoke(connectivityManager, enabled);
} catch (Exception e) {
e.printStackTrace();
}
}
private void toggleMobileData(Context context, boolean enabled){ ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); Method setMobileDataEnabl; try { setMobileDataEnabl = connectivityManager.getClass().getDeclaredMethod("setMobileDataEnabled", boolean.class); setMobileDataEnabl.invoke(connectivityManager, enabled); } catch (Exception e) { e.printStackTrace(); } }
android 开关数据流量