如何在android当前版本中刷新我的移动数据网络?

如何在android当前版本中刷新我的移动数据网络?,android,Android,请给出答案,我有麻烦刷新移动网络,我不想要系统应用程序,我只需要android移动应用程序,我需要刷新我的移动数据网络亲语法在当前版本,如棉花糖和牛轧糖,我已经把这个权限,我附上以下我的代码 private static boolean setMobileConnectionEnabled(Context context, boolean enabled) { try { // Requires: android.permission.CHANGE_NETWORK_STA

请给出答案,我有麻烦刷新移动网络,我不想要系统应用程序,我只需要android移动应用程序,我需要刷新我的移动数据网络亲语法在当前版本,如棉花糖和牛轧糖,我已经把这个权限,我附上以下我的代码

private static boolean setMobileConnectionEnabled(Context context, boolean enabled) {
    try {
        // Requires: android.permission.CHANGE_NETWORK_STATE
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
            Log.i("if", "" + Build.VERSION.SDK_INT);
            // pre-Gingerbread sucks!
            final TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            final Method getITelephony = telMgr.getClass().getDeclaredMethod("getITelephony");
            getITelephony.setAccessible(true);
            final Object objITelephony = getITelephony.invoke(telMgr);
            final Method toggleDataConnectivity = objITelephony.getClass()
                    .getDeclaredMethod(enabled ? "enableDataConnectivity" : "disableDataConnectivity");
            toggleDataConnectivity.setAccessible(true);
            toggleDataConnectivity.invoke(objITelephony);
        }
        // Requires: android.permission.CHANGE_NETWORK_STATE
        else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Log.i("else", "" + Build.VERSION.SDK_INT);
            final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            // Gingerbread to KitKat inclusive
            final Field serviceField = connMgr.getClass().getDeclaredField("mService");
            serviceField.setAccessible(true);
            final Object connService = serviceField.get(connMgr);
            try {
                final Method setMobileDataEnabled = connService.getClass()
                        .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
                Log.i("try", "" + setMobileDataEnabled);
                setMobileDataEnabled.setAccessible(true);
                setMobileDataEnabled.invoke(connService, Boolean.valueOf(enabled));
            } catch (NoSuchMethodException e) {
                // Support for CyanogenMod 11+
                final Method setMobileDataEnabled = connService.getClass()
                        .getDeclaredMethod("setMobileDataEnabled", String.class, Boolean.TYPE);
                setMobileDataEnabled.setAccessible(true);
                Log.i("catch", "" + setMobileDataEnabled);
                Log.i("errr", "" + e.getMessage());
                try {
                    setMobileDataEnabled.invoke(connService, context.getPackageName(), Boolean.valueOf(enabled));
                } catch (InvocationTargetException e1) {
                    e1.printStackTrace();
                }
            }
        }
        // Requires: android.permission.MODIFY_PHONE_STATE (System only, here for completions sake)
        else {
            // Lollipop and into the Future!
            final TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Log.i("telMgr",""+telMgr);
            final Method setDataEnabled = telMgr.getClass().getDeclaredMethod("setDataEnabled", Boolean.TYPE);
            Log.i("telMgdfgsdgr",""+setDataEnabled);
            setDataEnabled.setAccessible(true);
            setDataEnabled.invoke(telMgr, Boolean.valueOf(enabled));
        }
        return true;
    } catch (NoSuchFieldException e) {
        Log.e("", "setMobileConnectionEnabled", e);
    } catch (IllegalAccessException e) {
        Log.e("gsdjkghskd", "setMobileConnectionEnabled", e);
    } catch (IllegalArgumentException e) {
        Log.e("lllll", "setMobileConnectionEnabled", e);
    } catch (NoSuchMethodException e) {
        Log.e("nooooo", "setMobileConnectionEnabled", e);
    } catch (InvocationTargetException e) {
        Log.e("innnnnn", "setMobileConnectionEnabled", e);
    }
    return false;
}
private静态布尔setMobileConnectionEnabled(上下文上下文,布尔启用){
试一试{
//需要:android.permission.CHANGE\u NETWORK\u STATE
if(Build.VERSION.SDK_INT
我的清单文件

<permission
    android:name="android.permission.READ_PHONE_STATE"
    android:maxSdkVersion="24" />
<permission
    android:name="android.permission.ACCESS_FINE_LOCATION"
    android:maxSdkVersion="24" />

<permission
    android:name="android.permission.MODIFY_PHONE_STATE"
    android:maxSdkVersion="24" />

<permission
    android:name="android.permission.CHANGE_NETWORK_STATE"
    android:maxSdkVersion="24" />


请给出一些建议,我不能发表评论,所以我会把我的答案放在这里,但这可能会让你失望

在Android>=5中,目前没有办法做到这一点

您只能在根手机或系统应用程序上执行此操作。这可以通过反射实现,但由于它是@Hide注释的,所以谷歌删除了它,并且完全有权这样做

如果您希望仅禁用(或禁用更改手机数据状态的可能性),您可以使用设备提供商的api(如三星)。三星允许禁用更改电池数据状态的可能性,但。。。在更改的可能性关闭后,无法强制状态恢复为“开”

谷歌一再忽略来自社区的门票,将此选项带回SDK


只有当你有一个解决方案如何在没有根手机或系统应用程序的情况下做到这一点时,才请否决这个答案

我不能评论,所以我会把我的答案放在这里,但这可能会让你失望

在Android>=5中,目前没有办法做到这一点

您只能在根手机或系统应用程序上执行此操作。这可以通过反射实现,但由于它是@Hide注释的,所以谷歌删除了它,并且完全有权这样做

如果您希望仅禁用(或禁用更改手机数据状态的可能性),您可以使用设备提供商的api(如三星)。三星允许禁用更改电池数据状态的可能性,但。。。在更改的可能性关闭后,无法强制状态恢复为“开”

谷歌一再忽略来自社区的门票,将此选项带回SDK


只有当你有一个解决方案如何在没有根手机或系统应用程序的情况下做到这一点时,才请否决这个答案

嗨,欢迎来到Stackoverflow。你能告诉我们你的实际情况吗