【问题标题】:Programmatically toggle "Restrict Background Data"以编程方式切换“限制背景数据”
【发布时间】:2013-09-12 01:40:28
【问题描述】:

如果我转到“设置 - 数据使用”并按“属性”,我可以使用带有 Android 4.1.2 的三星 Galaxy S2 (i9105P) 激活“限制后台数据”。

有什么方法可以通过编程方式来实现,无论是开启还是关闭?

我只想在特定条件下(由我的应用确定)激活/停用它,因此我不必手动记住激活它。

PS:我搜索了android.developer.com网站,但没有成功。

【问题讨论】:

    标签: android networking restriction


    【解决方案1】:

    您可以在命令行中运行此命令 svc data disablesvc data enable 你显然需要 root 才能做到这一点,像这样:

    Runtime.getRuntime().exec("echo svc data disable | su");

    如果您的设备已root,这应该会打开root对话框。

    【讨论】:

      【解决方案2】:

      据我所知,对于 Android 4.x,您无法做到这一点。只有猴子跑步者插件可以帮助你。

      但如果你需要 Android 2.x,这是我使用的方法:

      /**
       * Switch mobile data network access 
       * */
      
      public void nmSwitchMobileNetworkDataAccess(boolean swtichCellOn){
      
          boolean disable;
          TelephonyManager telephonyManager = (TelephonyManager)m_context.getSystemService(Context.TELEPHONY_SERVICE);
      
          if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
              disable = false;
          }else{
              disable = true;  
          }
      
          try{
              final ConnectivityManager conman = (ConnectivityManager)m_context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
              final Method setMobileDataEnabledMethod = conman.getClass().getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
      
              if(disable == true && swtichCellOn == true){
                  setMobileDataEnabledMethod.invoke(conman, true);//turn cell on
      
                  DispatcherAndroid.androidObserverItf.androidObserver_OnProgress("Turn cell on, done",
                          EMethodResponse.ON_NM_REQ.FromEnumToString()  );
              }
              else if(disable == false && swtichCellOn == false){
                  setMobileDataEnabledMethod.invoke(conman, false);//turn cell off
      
                  DispatcherAndroid.androidObserverItf.androidObserver_OnProgress("Turn cell off, done",
                          EMethodResponse.ON_NM_REQ.FromEnumToString()  );
              }   
              else if((disable == false && swtichCellOn == true) || (disable == true && swtichCellOn == false)){
                  DispatcherAndroid.androidObserverItf.androidObserver_OnProgress("No changes",
                          EMethodResponse.ON_NM_REQ.FromEnumToString()  );
              }
      
          }
          catch(Exception e){
              e.printStackTrace();
          }
      }
      

      【讨论】:

      • 上述方法打开/关闭移动数据不删除后台限制
      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多