這個方法,具體代碼以下:html
[java] view plaincopyprint?java
/** 網絡
* gprs狀態 this
* @param getMobileDataEnabled spa
* @return .net
*/ code
public boolean getMobileDataStatus(String getMobileDataEnabled){ htm
ConnectivityManager cm; 對象
cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE); blog
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(); } }