置使用3G数据功能:
从源代码看到隐藏的API(ConnectivityManager.java):
-
-
-
-
-
-
-
- public void setMobileDataEnabled(boolean enabled) {
- try {
- mService.setMobileDataEnabled(enabled);
- } catch (RemoteException e) {
- }
- }
通过java
reflection功能来实现该功能,即调用
- mService.setMobileDataEnabled(enabled);
代码如下:
- private void EnableMobileData(boolean enable)
- {
- ConnectivityManager connectivitymanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
-
- try
- {
-
-
- Field field = Class.forName(ConnectivityManager.class.getName())
- .getDeclaredField("mService");
- field.setAccessible(true);
-
-
-
-
-
-
-
-
-
-
- Object obj = field.get(connectivitymanager);
-
- Class myClass = Class.forName(obj.getClass().getName());
- Log.i(TAG, "class3:" + obj.getClass().getName());
-
-
-
- Method method = myClass.getDeclaredMethod("setMobileDataEnabled",
- boolean.class);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- method.setAccessible(true);
- method.invoke(obj, enable);
-
- }
- catch (Exception e)
- {
-
- e.printStackTrace();
- }
-
- }
注意添加相应的permission:
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
补充一个网上利用java
reflection解析APK软件包信息的代码:
Android:利用Java反射调用@hide的API,布布扣,bubuko.com
Android:利用Java反射调用@hide的API
原文:http://www.cnblogs.com/veins/p/3717630.html