两种例子
/**
* 移动网络开关
*/
private void toggleMobileData(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) {
final String TAG = "setMobileDataEnabled";
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class conmanClass;
try {
conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
Log.d(TAG, "ClassNotFoundException");
} catch (NoSuchFieldException e) {
Log.d(TAG, "NoSuchFieldException");
} catch (IllegalArgumentException e) {
Log.d(TAG, "IllegalArgumentException");
} catch (IllegalAccessException e) {
Log.d(TAG, "IllegalAccessException");
} catch (NoSuchMethodException e) {
Log.d(TAG, "NoSuchMethodException");
} catch (InvocationTargetException e) {
Log.d(TAG, "InvocationTargetException");
}finally{
}
} 打开GPS,在2.3.5系统测试可以通过,在4.1.1系统中测试无效
/**
*
GPS开关
*
当前若关则打开
*
当前若开则关闭
*/
private void toggleGPS() {
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE"); gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
}
}