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





