android - 重置安卓手机网络信号?

标签 android android-networking airplane-mode

我需要我的应用来重置设备的移动网络信号。这与切换暂时失去连接的飞行模式具有相同的效果,重新连接时会分配一个新的 IP 地址,并且状态栏中的 LTE/信号图标应该消失,然后在重新连接时重新出现。我找到了 an app在我在运行 Android 4.4.4 和 CyanogenMod 的手机上测试过的 Play Store 上,它确实做到了这一点,但我不确定如何在我自己的应用程序中实现它。我认为这与 CHANGE_NETWORK_STATE 权限有关。我正在寻找将重置网络连接的文档或一些简单的示例代码。

请注意,我不是专门尝试切换飞行模式,而是按照上面链接的应用程序的方式重置移动数据,因为我已经测试过它确实可以工作,甚至不需要根权限。

最佳答案

Lollipop 支持需要新的系统级权限 android.permission.MODIFY_PHONE_STATE 才能工作。

private static boolean setMobileConnectionEnabled(Context context, boolean enabled)
{
    try{
        // Requires: android.permission.CHANGE_NETWORK_STATE
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD){
            // 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){
            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);
                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);
                setMobileDataEnabled.invoke(connService, context.getPackageName(), Boolean.valueOf(enabled));
            }
        }
        // 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);
            final Method setDataEnabled = telMgr.getClass().getDeclaredMethod("setDataEnabled", Boolean.TYPE);
            setDataEnabled.setAccessible(true);
            setDataEnabled.invoke(telMgr, Boolean.valueOf(enabled));
        }
        return true;
    }
    catch(NoSuchFieldException e){
        Log.e(TAG, "setMobileConnectionEnabled", e);
    }
    catch(IllegalAccessException e){
        Log.e(TAG, "setMobileConnectionEnabled", e);
    }
    catch(IllegalArgumentException e){
        Log.e(TAG, "setMobileConnectionEnabled", e);
    }
    catch(NoSuchMethodException e){
        Log.e(TAG, "setMobileConnectionEnabled", e);
    }
    catch(InvocationTargetException e){
        Log.e(TAG, "setMobileConnectionEnabled", e);
    }
    return false;
}

需要许可。

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

关于android - 重置安卓手机网络信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28131335/

相关文章:

android - 如何在 Android 中识别音调?

android - 错误 :Could not find com. google.gms :google-services:3. 0.0

c# - 使用 HTTP 请求获取数据 Android Xamarin C#

android - 通过谷歌播放更新应用程序时下载内容?

android - ConnectivityManager getActiveNetworkInfo 空指针异常

java - HttpEntity的consumeContent()的使用

ios - 是否有一种(合法的)方法可以在 iOS 下捕获整个屏幕?

Android hive 飞行模式检测

ios - canDisplayBannerAds 和飞行模式

java - 使上传方法返回字符串 "the uploaded file url"