Android的 - 在ConnectivityManager setMobileDataEnabled公共法SDK不可用 [英] Android - public method setMobileDataEnabled in ConnectivityManager not available in SDK
问题描述
结果
我试图用setMobileDataEnabled的ConnectivityManager类的方法,使用SDK 2.2。
据<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2.1_r1/android/net/ConnectivityManager.java/?v=source\" rel=\"nofollow\">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2.1_r1/android/net/ConnectivityManager.java/?v=source这种方法被声明为public但@hide是不是在SDK和Eclipse中使用。
为了绕过我写了下面的功能即可开启/关闭移动数据连接的躲了起来。
公共无效setMobileData(布尔toBeEnabled){ 对象MyObj中= getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager厘米=(ConnectivityManager)MyObj中; C类= NULL;
尝试{
C =的Class.forName(cm.getClass()的getName());
}赶上(ClassNotFoundException的E2){
// TODO自动生成catch块
e2.printStackTrace();
}
方法M =无效;
尝试{
M = c.getDeclaredMethod(getMobileDataEnabled);
}赶上(SecurityException异常E2){
// TODO自动生成catch块
e2.printStackTrace();
}赶上(NoSuchMethodException E2){
// TODO自动生成catch块
e2.printStackTrace();
}
对象mobileDataEnabled = NULL;
如果(M!= NULL){
m.setAccessible(真);
键入res_of_m = m.getGenericReturnType();
键入[] = pars_of_m m.getGenericParameterTypes();
尝试{
mobileDataEnabled =(m.invoke(厘米));
如果(mobileDataEnabled!= NULL)
如果(mobileDataEnabled.equals(!toBeEnabled)){
方法2 =无效;
尝试{
INT索引= 0;
布尔method_found = FALSE;
方法[] = available_methods c.getDeclaredMethods();
针对(法方法:available_methods){
//以下行不起作用
// method.getName()==setMobileDataEnabled
如果(method.getName()。包含(setMobileDataEnabled))
{
method_found = TRUE;
} 如果(method_found == FALSE)
指数++;
}
//以下行不起作用
// 2 = c.getDeclaredMethod(setMobileDataEnabled);
M2 =(c.getDeclaredMethods())[指数]
如果(M2!= NULL){
m2.setAccessible(真);
m2.invoke(厘米,toBeEnabled);
}
}赶上(SecurityException异常E2){
// TODO自动生成catch块
e2.printStackTrace();
}赶上(E2的InvocationTargetException){
// TODO自动生成catch块
e2.printStackTrace();
}
}
}赶上(抛出:IllegalArgumentException五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IllegalAccessException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(的InvocationTargetException E){
// TODO自动生成catch块
e.printStackTrace();
}
}
}
要使它工作,我也在明显增加了android.permission.WRITE_SECURE_SETTINGS和安装在/系统/应用程序根据<一个href=\"http://stackoverflow.com/questions/3632449/android-add-app-to-firmware-use-write-secure-settings\">Android:添加应用固件,请使用WRITE_SECURE_SETTINGS 。
有谁知道一个更好的办法?
{尝试
最后ConnectivityManager厘米=(ConnectivityManager)this.context
.getSystemService(Context.CONNECTIVITY_SERVICE); 最后一类connectivityManager =的Class.forName(cm.getClass()
.getName()); 最后的方法[] =方法connectivityManager.getDeclaredMethods(); 对于(final方法方法:方法){
如果(MLauncherConnectivityManager.SET_MOBILE_DATA_ENABLED
.equals(method.getName())){
method.invoke(厘米,真);
}
} }赶上(最终ClassNotFoundException的E){
Log.e(类,e.getMessage());
}赶上(最终抛出:IllegalArgumentException五){
Log.e(类,e.getMessage());
}赶上(最终IllegalAccessException E){
Log.e(类,e.getMessage());
}赶上(最终的InvocationTargetException E){
Log.e(类,e.getMessage());
}
i'm trying to use the method setMobileDataEnabled of the ConnectivityManager class, using SDK 2.2.
According to http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2.1_r1/android/net/ConnectivityManager.java/?v=source this method is declared public but with @hide is not available in the SDK and in Eclipse.
In order to bypass the hiding I wrote the following function to toggle the mobile data connection on/off.
public void setMobileData(boolean toBeEnabled){
Object myObj= getSystemService(CONNECTIVITY_SERVICE);
ConnectivityManager cm = (ConnectivityManager) myObj;
Class c = null;
try {
c = Class.forName(cm.getClass().getName());
} catch (ClassNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Method m = null;
try {
m = c.getDeclaredMethod("getMobileDataEnabled");
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (NoSuchMethodException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Object mobileDataEnabled=null;
if (m!=null){
m.setAccessible(true);
Type res_of_m= m.getGenericReturnType();
Type[] pars_of_m= m.getGenericParameterTypes();
try {
mobileDataEnabled = (m.invoke(cm));
if (mobileDataEnabled!=null)
if (mobileDataEnabled.equals(!toBeEnabled)){
Method m2 = null;
try {
int index=0;
boolean method_found=false;
Method[] available_methods= c.getDeclaredMethods();
for (Method method : available_methods) {
// following line doesn't work
// method.getName()=="setMobileDataEnabled"
if (method.getName().contains("setMobileDataEnabled"))
{
method_found=true;
}
if (method_found==false)
index++;
}
// following line doesn't work
//m2 = c.getDeclaredMethod("setMobileDataEnabled");
m2 = (c.getDeclaredMethods())[index];
if (m2!=null){
m2.setAccessible(true);
m2.invoke(cm,toBeEnabled);
}
} catch (SecurityException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (InvocationTargetException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
To make it working I also added the android.permission.WRITE_SECURE_SETTINGS" in the manifest and installed in /system/app according to Android: Add app to firmware, use WRITE_SECURE_SETTINGS.
Does anyone know a better way?
try {
final ConnectivityManager cm = (ConnectivityManager) this.context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class connectivityManager = Class.forName(cm.getClass()
.getName());
final Method[] methods = connectivityManager.getDeclaredMethods();
for (final Method method : methods) {
if (MLauncherConnectivityManager.SET_MOBILE_DATA_ENABLED
.equals(method.getName())) {
method.invoke(cm, true);
}
}
} catch (final ClassNotFoundException e) {
Log.e("Class", e.getMessage());
} catch (final IllegalArgumentException e) {
Log.e("Class", e.getMessage());
} catch (final IllegalAccessException e) {
Log.e("Class", e.getMessage());
} catch (final InvocationTargetException e) {
Log.e("Class", e.getMessage());
}
这篇关于Android的 - 在ConnectivityManager setMobileDataEnabled公共法SDK不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!