该setMobileDataEnabled方法不再调用由于Android升,后来后来、方法、setMobileDataEnabled、Android

2023-09-11 11:50:18 作者:殉情

我已经登录发行78084 与谷歌关于 setMobileDataEnabled()方法是不再通过反射调用。这是可调用由于Android 2.1(API 7),以通过反射的Andr​​oid 4.4(API 19),但由于Android L和以后,即使有根,在 setMobileDataEnabled()方法是不是可调用的。

I have logged Issue 78084 with Google regarding the setMobileDataEnabled() method being no longer callable via reflection. It was callable since Android 2.1 (API 7) to Android 4.4 (API 19) via reflection, but as of Android L and later, even with root, the setMobileDataEnabled() method is not callable.

该负责人的回答是,这个问题是封闭的状态设置为WorkingAsIntended。谷歌的简单解释是:

The official response is that the issue is "Closed" and the status set to "WorkingAsIntended". Google's simple explanation is:

私有API是私有的,因为他们没有稳定,可能会消失,恕不另行通知。

Private APIs are private because they are not stable and might disappear without notice.

是的,谷歌,我们都知道使用反射来调用隐藏于方法之前的Andr​​oid来到了scene-的风险,但你需要提供一个更坚实的答案,备选方案,如果有的话,为了实现相同的结果为 setMobileDataEnabled()。 (如果你是不满谷歌的决定,因为我,然后登录到发行78084 和明星也尽可能多的让谷歌知道他们的方式错误。)

Yes, Google, we are aware of the risk of using reflection to call hidden method- even before Android came on the scene- but you need to provide a more solid answer as to alternatives, if any, for accomplishing the same result as setMobileDataEnabled(). (If you are displeased with Google's decision as I am, then log into Issue 78084 and star it as many as possible to let Google know the error of their way.)

所以,我想问你的是:我们在一条死胡同的时候还以编程方式启用或Android设备上禁用手机的网络功能?从谷歌这样严厉的方式不知何故不跟我坐好。如果你有解决方法为Android 5.0(棒棒堂)及以后,我很想听到这个线程你的答案/讨论。

So, my question to you is: Are we at a dead end when it comes to programmatically enable or disable mobile network function on an Android device? This heavy-handed approach from Google somehow does not sit well with me. If you have workaround for Android 5.0 (Lollipop) and beyond, I would love to hear your answer/discussion in this thread.

我已经用下面的code,看 setMobileDataEnabled()方法是可行的:

I have used the code below to see if the setMobileDataEnabled() method is available:

final Class<?> conmanClass = Class.forName(context.getSystemService(Context.CONNECTIVITY_SERVICE).getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(context.getSystemService(Context.CONNECTIVITY_SERVICE));
final Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method[] methods = iConnectivityManagerClass.getDeclaredMethods();
for (final Method method : methods) {
    if (method.toGenericString().contains("set")) {
        Log.i("TESTING", "Method: " + method.getName());
    }
}

但事实并非如此。

But it's not.

更新:目前,有可能触发移动网络设备是否扎根。然而,对于非root权限的设备,它仍然是一个调查的过程,因为没有切换的移动网络通用的方法。

UPDATE: Currently, it's possible to toggle mobile network if the device is rooted. However, for non-rooted devices, it's still an investigative process as there is no universal method to toggle mobile network.

推荐答案

为了延长Muzikant的解决方案2,有人可以请尝试以下是Android 5.0植根设备上的解决方案(因为我目前所不具备的)和让我知道,如果它工作或不工作。

To extend Muzikant's Solution #2, can someone please try the solution below on an Android 5.0 rooted device (as I currently do not possess one) and let me know if it works or does not work.

要启用或禁用移动数据,请尝试:

To enable or disable mobile data, try:

// 1: Enable; 0: Disable
su -c settings put global mobile_data 1
su -c am broadcast -a android.intent.action.ANY_DATA_STATE --ez state 1

注: mobile_data 变量可以在Android的API 21源$ C ​​$ CS在 / Android的SDK /来源/安卓-21被发现/android/provider/Settings.java ,并声明为:

Note: The mobile_data variable can be found in Android API 21 source codes at /android-sdk/sources/android-21/android/provider/Settings.java and is declared as:

/**
 * Whether mobile data connections are allowed by the user.  See
 * ConnectivityManager for more info.
 * @hide
*/
public static final String MOBILE_DATA = "mobile_data";

虽然 android.intent.action.ANY_DATA_STATE 意图可以在Android的API 21源$ C ​​$ CS在 / Android的SDK中找到/来源/ Android为21 / COM /安卓/内部/电话/ TelephonyIntents.java ,并声明为:

While the android.intent.action.ANY_DATA_STATE Intent can be found in Android API 21 source codes at /android-sdk/sources/android-21/com/android/internal/telephony/TelephonyIntents.java and is declared as:

/**
 * Broadcast Action: The data connection state has changed for any one of the
 * phone's mobile data connections (eg, default, MMS or GPS specific connection).
 *
 * <p class="note">
 * Requires the READ_PHONE_STATE permission.
 * <p class="note">This is a protected intent that can only be sent by the system.
 *
 */
public static final String ACTION_ANY_DATA_CONNECTION_STATE_CHANGED
        = "android.intent.action.ANY_DATA_STATE";

更新1 :如果你不想执行上面的Java codeS的Andr​​oid应用程序,那么你可以运行