重置Android移动网络信号?

时间:2015-01-24 23:12:43

标签: android android-networking airplane

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

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

2 个答案:

答案 0 :(得分:3)

棒棒糖支持需要新的系统级别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"/>

答案 1 :(得分:1)

从4.4.2开始,人们无法获得切换飞行模式或移动数据的权限。但是,使用反射,可以解决这个限制。

以下是切换飞机模式的方法: Toggle airplane mode in Android

这是一种在4.4.2之前切换移动数据并发布(使用反射)的方法: Toggle mobile data programmatically on Android 4.4.2

注意,我在不到3分钟的时间内通过在我最喜欢的搜索引擎上搜索&#34; android切换飞机模式&#34;和&#34; android切换移动数据&#34;。

注2:您也可以使用反射进行飞行模式切换,但您可能需要进行一些挖掘以找到必要的ASOP API或查看其他人(如CyanogenMod)是否已经完成此操作。