禁用/启用Android上大号的移动数据有根存取权限 [英] Disable/Enable mobile data on Android L with root acces

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

问题描述

我写一个小程序,只有我会用,我要以务实的态度启用/禁用我的移动数据有根的Andr​​oid 4.5设备上(我运行一个定制的Andr​​oid L代表的Nexus 4)。

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).

我已经看过了一段时间,我发现反映,一直工作到安卓4.3的方法。 我也看到了方法,从这个帖子<一个href="http://stackoverflow.com/questions/21511216/enable-and-disable-mobile-data-programmatically-on-android-4-4-2">Enable和禁用移动数据编程在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 Enable and Disable 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 ?

推荐答案

我创造了这个方法环顾在互联网上;它工作正常的的Andr​​oid 5.0.1 基本上你,如果你想连接启用,否则为false传递真实的,和你的应用程序的上下文。

I've created this method looking around on internet; it works fine on rooted android 5.0.1 Basically 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 <兼容href="http://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later/28683889#28683889">Credit

EDIT: Now also compatible with android 5.1 Credit

这篇关于禁用/启用Android上大号的移动数据有根存取权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆