开发者

setMobileDataEnabled does not work on emulator?

开发者 https://www.devze.com 2023-02-25 08:41 出处:网络
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.

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 and开发者_Go百科roid.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 ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号