在具有root访问权限的Android

在具有root访问权限的Android

本文介绍了在具有root访问权限的Android L上禁用/启用移动数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在编写一个只有我会使用的小应用程序,我想在一个扎根的android 4.5设备(我正在为Nexus 4运行自定义Android L)上实用地启用/禁用我的移动数据.

I am writing a little app that only I will use and I want to pragmatically enable / disable my mobile data on a rooted android 4.5 device (I am running a custom Android L for Nexus 4).

我已经寻找了一段时间,我发现反射的方法在android 4.3之前一直有效.我还从这篇文章中看到了该方法在Android 4.4.2上以编程方式移动数据,但这需要使用cyanogenmod.

I have looked for a while and I found the methods with reflection that worked until android 4.3.I have also seen the method from this post Toggle mobile data programmatically on Android 4.4.2 but this requires cyanogenmod.

根据我在互联网上可以找到的信息,对于非root用户应用程序来说这是不可能的,但我的问题是:

From what I can find on the internet this is impossible for non-root apps but my question is:

我可以用我的root特权来完成此操作吗?

is there something I can do with my root privileges to accomplish this?

推荐答案

我创建了这种方法,可以在Internet上四处查看;它在 rooted android 5.0.1上运行良好基本上,如果要启用连接,则必须传递true;否则,则传递false,以及应用程序的上下文.

I've created this method looking around on internet; it works fine on rooted android 5.0.1Basically you have to pass true if you want the connection to be enabled, false otherwise, and the context of your application.

private final static String COMMAND_L_ON = "svc data enable\n ";
private final static String COMMAND_L_OFF = "svc data disable\n ";
private final static String COMMAND_SU = "su";

public static void setConnection(boolean enable,Context context){

    String command;
    if(enable)
        command = COMMAND_L_ON;
    else
        command = COMMAND_L_OFF;

    try{
        Process su = Runtime.getRuntime().exec(COMMAND_SU);
        DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

        outputStream.writeBytes(command);
        outputStream.flush();

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        outputStream.close();
    }catch(IOException e){
        e.printStackTrace();
    }
}

请报告在某些设备上是否有问题.

Please report if this has problems on some device.

编辑:现在还与Android 5.1

EDIT: Now also compatible with android 5.1 Credit

这篇关于在具有root访问权限的Android L上禁用/启用移动数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:46