两种例子
01/**02* 移动网络开关03*/04privatevoidtoggleMobileData(Context context,booleanenabled) {05ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);06Class<?> conMgrClass =null;// ConnectivityManager类07Field iConMgrField =null;// ConnectivityManager类中的字段08Object iConMgr =null;// IConnectivityManager类的引用09Class<?> iConMgrClass =null;// IConnectivityManager类10Method setMobileDataEnabledMethod =null;// setMobileDataEnabled方法11try{12// 取得ConnectivityManager类13conMgrClass = Class.forName(conMgr.getClass().getName());14// 取得ConnectivityManager类中的对象mService15iConMgrField = conMgrClass.getDeclaredField("mService");16// 设置mService可访问17iConMgrField.setAccessible(true);18// 取得mService的实例化类IConnectivityManager19iConMgr = iConMgrField.get(conMgr);20// 取得IConnectivityManager类21iConMgrClass = Class.forName(iConMgr.getClass().getName());22// 取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法23setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);24// 设置setMobileDataEnabled方法可访问25setMobileDataEnabledMethod.setAccessible(true);26// 调用setMobileDataEnabled方法27setMobileDataEnabledMethod.invoke(iConMgr, enabled);28}catch(ClassNotFoundException e) {29e.printStackTrace();30}catch(NoSuchFieldException e) {31e.printStackTrace();32}catch(SecurityException e) {33e.printStackTrace();34}catch(NoSuchMethodException e) {35e.printStackTrace();36}catch(IllegalArgumentException e) {37e.printStackTrace();38}catch(IllegalAccessException e) {39e.printStackTrace();40}catch(InvocationTargetException e) {41e.printStackTrace();42}43}
第二种
01privatevoidsetMobileDataEnabled(Context context,booleanenabled) {02finalString TAG ="setMobileDataEnabled";03finalConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);04Class conmanClass;05try{06conmanClass = Class.forName(conman.getClass().getName());07finalField iConnectivityManagerField = conmanClass.getDeclaredField("mService");08iConnectivityManagerField.setAccessible(true);09finalObject iConnectivityManager = iConnectivityManagerField.get(conman);10finalClass iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());11finalMethod setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);12setMobileDataEnabledMethod.setAccessible(true);1314setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);15}catch(ClassNotFoundException e) {16// TODO Auto-generated catch block17Log.d(TAG,"ClassNotFoundException");18}catch(NoSuchFieldException e) {19Log.d(TAG,"NoSuchFieldException");20}catch(IllegalArgumentException e) {21Log.d(TAG,"IllegalArgumentException");22}catch(IllegalAccessException e) {23Log.d(TAG,"IllegalAccessException");24}catch(NoSuchMethodException e) {25Log.d(TAG,"NoSuchMethodException");26}catch(InvocationTargetException e) {27Log.d(TAG,"InvocationTargetException");28}finally{2930}3132}
打开GPS,在2.3.5系统测试可以通过,在4.1.1系统中测试无效
01/**02* <p>GPS开关03* <p>当前若关则打开04* <p>当前若开则关闭05*/06privatevoidtoggleGPS() {07Intent gpsIntent =newIntent();08gpsIntent.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");09gpsIntent.addCategory("android.intent.category.ALTERNATIVE"); gpsIntent.setData(Uri.parse("custom:3"));10try{11PendingIntent.getBroadcast(this,0, gpsIntent,0).send();12}catch(CanceledException e) {13e.printStackTrace();14}15}