setMobileDataEnabled不能在模拟器上工作? [英] setMobileDataEnabled does not work on emulator?

查看:305
本文介绍了setMobileDataEnabled不能在模拟器上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ConnectivityManager的setMobileDataEnabled方法和某些原因,我得到我的仿真器和实际的设备不同的结果工作。
在我的设备(Nexus One的,运行的CyanogenMod 7.0)调用此函数并设置移动数据正确设置,当它工作完全正常(使用反射让这个功能之后):

I'm trying to work with the setMobileDataEnabled method of ConnectivityManager and for some reason i get different results on my emulator and actual device. On my device (Nexus One, running CyanogenMod 7.0) it works perfectly fine when calling this function and setting the mobile data setting correctly (after getting to this function using reflection):

ConnectivityManager connService = (ConnectivityManager) p_context.getSystemService(Context.CONNECTIVITY_SERVICE);

if (null != connService)
{
    try
    {
        Method setMobileDataEnabledMethod = connService.getClass().getDeclaredMethod("setMobileDataEnabled", boolean.class);

        if (null != setMobileDataEnabledMethod)
        {
            setMobileDataEnabledMethod.invoke(connService, true);
        }
    }
    catch (Exception ex)
    {
        // Error
    }
}

问题是,当我使用的相同 code在模拟器上我得到了以下异常:

The problem is that when I'm using the same code on the emulator I'm getting the following exception:

java.lang.reflect.InvocationTargetException
    at android.net.ConnectivityManager.setMobileDataEnabled(ConnectivityManager.java:379)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
Caused by: java.lang.SecurityException: Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS
    at android.os.Parcel.readException(Parcel.java:1247)
    at android.os.Parcel.readException(Parcel.java:1235)
    at android.net.IConnectivityManager$Stub$Proxy.setMobileDataEnabled(IConnectivityManager.java:540)

文件(即我能找到...)它指出WRITE_SECURE_SETTINGS权限需要这个功能,我不知道,如果这只是一个仿真程序问题,或者说我的设备上它的工作原理其实在任何地方仅仅是一些奇怪的一致?

Nowhere in the documentation (that I could find...) it states that the "WRITE_SECURE_SETTINGS" permission is required for this function and I wonder if this is just an emulator issue or the fact that on my device it works is just some weird coincident ?

推荐答案

这是一个安全的设置,您需要与系统权限的应用程序来设置这个值。对于您需要登录与我不知道将是一个灵活的解决方案考虑到你有这么多版本的供应商建立的Andr​​oid版本的软件发布钥匙您的应用程序。

This is a secure setting, you need to be a application with system permissions to set this value. For that you need to sign your application with vendor release keys which i am not sure will be a scalable solution considering you have so many versions of vendor builds of android.

相反,你可以尝试提示用户启动该屏幕并设置值。

Instead you can try prompting user to launch that screen and set the value.

这篇关于setMobileDataEnabled不能在模拟器上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆