zoukankan      html  css  js  c++  java
  • 开启/关闭 移动数据连接 Mobile Data

    开启/关闭 移动数据连接 Mobile Data 

     

    Dataconnection 开关的API 并没有直接提供给上层使用,不过可以通过Java的反射机制使用 ITelephony来实现

      Method dataConnSwitchmethod;
      Class telephonyManagerClass;
      Object ITelephonyStub;   Class ITelephonyClass; TelephonyManager telephonyManager
    = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){ isEnabled = true; }else{ isEnabled = false; } telephonyManagerClass = Class.forName(telephonyManager.getClass().getName()); Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony"); getITelephonyMethod.setAccessible(true); ITelephonyStub = getITelephonyMethod.invoke(telephonyManager); ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName()); if (isEnabled) { dataConnSwitchmethod = ITelephonyClass .getDeclaredMethod("disableDataConnectivity"); } else { dataConnSwitchmethod = ITelephonyClass .getDeclaredMethod("enableDataConnectivity"); } dataConnSwitchmethod.setAccessible(true); dataConnSwitchmethod.invoke(ITelephonyStub);

    需要捕获异常,需要权限:<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>

    不过MODIFY_PHONE_STATE权限在Gingerbread 2.3开始 就无效了(详见 StackOverFlow)。

    Android 2.3 可以通过反射使用 IConnectivityManager.setMobileDataEnabled():

    private void setMobileDataEnabled(Context context, boolean enabled) {
        final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class.forName(conman.getClass().getName());
        final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
        iConnectivityManagerField.setAccessible(true);
        final Object iConnectivityManager = iConnectivityManagerField.get(conman);
        final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
    
        setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
    }

    需要权限:<uses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE"/>

    不过不能保证在更高版本中有效,我在4.0.3上试过,此法行不通。

    P.S.  Google Play 软件:Data Enabler 可以快速实现Mobile Data的开关,基本支持目前所有Android版本,不知道是怎么做到的。

    参考链接:

    http://stackoverflow.com/questions/3644144/how-to-disable-mobile-data-on-android/8962211#8962211

    http://stackoverflow.com/questions/4715250/how-to-grant-modify-phone-state-permission-for-apps-ran-on-gingerbread

  • 相关阅读:
    BMP图像信息隐藏
    多项式模2运算及求逆元
    day08 xml tomcat
    day07 c3p0连接池
    day06 多表查询
    java学习日记(day30--dbutils)
    java学习日记(29 JDBC)
    java学习日记(28)-- mysql基础
    activiti主要组件解析
    activiti流程跟踪图算法
  • 原文地址:https://www.cnblogs.com/willyan/p/2574513.html
Copyright © 2011-2022 走看看