【问题标题】:How to Refresh my Mobile data network in android current version?如何在 android 当前版本中刷新我的移动数据网络?
【发布时间】:2017-03-31 11:13:33
【问题描述】:

请回答,我无法刷新移动网络,我不需要系统应用程序,我只需要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;
}

我的清单文件

<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" />

请给点建议

【问题讨论】:

  • 嗨,欢迎来到 Stackoverflow。您能否告诉我们“刷新我的数据网络”实际上是什么意思?
  • 我的意思是移动网络先生。
  • 我已经设置了运行时权限,但它显示这样的错误原因:java.lang.SecurityException:用户 10227 和当前进程都没有 android.permission.MODIFY_PHONE_STATE。我遇到了这样的错误

标签: android


【解决方案1】:

我无法发表评论,所以我会把我的答案放在这里,但它可能会让你失望。

目前在 Android >= 5 中无法做到这一点。

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

如果您只想禁用(或禁用更改蜂窝数据状态的可能性),您可以使用设备提供商的 api(如三星)。三星允许禁用更改单元格数据状态的可能性,但是......在更改的可能性被关闭后,您不能强制状态重新打开。

Google 多次忽略来自社区的票,以将此选项带回 SDK。

请仅当您有解决方案时才能对此答案投反对票,而无需根植手机或系统应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    相关资源
    最近更新 更多