Android5.0以上系統的移動網路開關

來源:互聯網
上載者:User

標籤:com   man   ber   郵箱   except   whether   工作   doc   一個   

筆者近期遇到一個非常有意思的bug,貼出來和大家分享下。
那是一個溫暖的早晨,陽光曬得人非常舒服。一封bug郵件像一片葉子飄到我的郵箱。

一番交流。筆者確認負責的Widget開關在Android5.0以上系統沒有作用。相信非常多做過移動網路開關的朋友都知道。傳統的方法是在ConnectivityManager中通過反射兩個方法setMobileDataEnabled和getMobileDataEnabled來控制移動網路開和關的。

    /**     * Gets the value of the setting for enabling Mobile data.     *     * @return Whether mobile data is enabled.     * @hide     */    public boolean getMobileDataEnabled() {        try {            return mService.getMobileDataEnabled();        } catch (RemoteException e) {            return true;        }    }    /**     * Sets the persisted value for enabling/disabling Mobile data.     *     * @param enabled Whether the mobile data connection should be     *            used or not.     * @hide     */    public void setMobileDataEnabled(boolean enabled) {        try {            mService.setMobileDataEnabled(enabled);        } catch (RemoteException e) {        }    }

可是開啟5.0以上的原始碼,這兩個方法已經不存在了。
推薦一個不錯的線上看原始碼的網站。線上原始碼網址。

此時老大的提醒幫了我。我開啟5.0以上代碼的TelephonyMananger類嘗試通過反射擷取setDataEnabled和getDataEnabled類完畢操作。
原始碼

/** @hide */    @SystemApi    public void setDataEnabled(boolean enable) {        try {            getITelephony().setDataEnabled(enable);        } catch (RemoteException e) {            Log.e(TAG, "Error calling ITelephony#setDataEnabled", e);        }    }    /** @hide */    @SystemApi    public boolean getDataEnabled() {        try {            return getITelephony().getDataEnabled();        } catch (RemoteException e) {            Log.e(TAG, "Error calling ITelephony#getDataEnabled", e);        }        return false;    }

我的反射方法:

public void setMobileDataState(Context cxt, boolean mobileDataEnabled) {        TelephonyManager telephonyService = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);    try {        Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);        if (null != setMobileDataEnabledMethod)        {            setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);        }    }    catch (Exception e) {        LogHelper.v(TAG, "Error setting" + ((InvocationTargetException)e).getTargetException() + telephonyService);    }}public boolean getMobileDataState(Context cxt) {        TelephonyManager telephonyService = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);    try {        Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");        if (null != getMobileDataEnabledMethod)        {            boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);            return mobileDataEnabled;        }    }    catch (Exception e) {        LogHelper.v(TAG, "Error getting" + ((InvocationTargetException)e).getTargetException() + telephonyService);    }    return false;}

可是報了InvocationTargetException錯誤。

通過:

((InvocationTargetException)e).getTargetException()

方法得知是運行反射運行時發生例如以下錯誤:

Error settingjava.lang.SecurityException: Neither user 10240 nor current process has android.permission.MODIFY_PHONE_STATE.android.telephony.TelephonyManager

這個時候老大提醒我把APP移動到system/app下試試。


幾番嘗試。仍是相同的結果。

這個時候一個我無意的操作,將APP移動到system/priv-app,居然成功了。在這個檔案夾下,開關又能夠又一次工作了。

當然,我們的應用不可能裝在這個檔案夾下,僅僅能採取折中的方案。但整個過程還是非常開心的,這個bug算是我第一次發現了不同Android版本號碼之間原始碼的差異,感覺非常有意思。希望對大家有所啟示。

Android5.0以上系統的移動網路開關

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.