setMobileDataEnabled does not work on emulator?

自作多情 提交于 2019-12-11 20:43:55

问题


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
    }
}

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)

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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/5700747/setmobiledataenabled-does-not-work-on-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!