如何在Android禁用移动数据 [英] How to disable Mobile Data on Android

查看:85
本文介绍了如何在Android禁用移动数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速回故事有人告诉我买一个应用程序之前。 =)

Quick back story before someone tells me to buy an app. =)

我刚刚得到了一个EVO,它嚼通过电池相当快。我下载JuiceDefender来管理我的移动数据连接。这似乎已经制定了相当不错。但是,这些设置都只是非常有限(即使在付费版本)。

截至目前,我试图建立一个更加定制的电池节能应用。最主要的,我试图做第一个能够允许/禁止随意移动数据连接。

As of right now I am trying to develop a much more customizable battery saving application. The main thing I am trying to do first be able to enable/disable the mobile data connection at will.

现在的问题是我无法找到如何做到这一点任何code片段或文章。我发现的唯一的事情就是下面。我不知道如何准确的这个,但是这是我所能拼凑浏览developer.android.com

The problem is I can't find any code snippets or articles on how to do this. The only thing I have found is the following. I don't know how accurate this is, but this was all I could piece together browsing developer.android.com

ConnectivityManager cm = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "android.net.conn.CONNECTIVITY_CHANGE");

State state = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
textView.setText(state.name());

如果任何人都可以点我的任何可能的帮助,这将是最AP preciated。

If anyone can point me to anything that could help, it would be most appreciated.

看来,HTC的Evo在Sprint不使用APN设置。我通过下载APNDroid并看着它不能正常工作测试此。然后,我做了一个快速的应用程序转储所有APN条目到屏幕上。这产生了一个结果,这是对彩信。

It appears that the HTC Evo on Sprint does not use APN settings. I tested this by downloading APNDroid and watching it not work. I then made a quick app to dump all APN entries to the screen. That yielded one result and it was for mms.

综观时JuiceDefender正在运行的手机信息,我发现GSRP网络日臻开启和关闭。这使我相信这是可以通过code,即使每一页我发现问同样的问题说,它无法做到的做到这一点。在踢球的是,他们都表示要做得像APNDroid。请人给我一些见解。

Looking at the phone info when JuiceDefender is running, I found that the GSRP network is getting turned on and off. This leaves me to believe it is possible to do it through code even though every page I find asking about this same issue says it cannot be done. The kicker is they all say to do it like APNDroid. Please someone give me some insight.

谢谢!

推荐答案

在Dataconnection禁用和启用API是隐藏在SDK和不暴露给用户,这可以通过使用Java反射技术利用ITelephony接口achived。

The Dataconnection disable and enabling APIS are hidden in the SDK and not exposed to the user, this can be achived by accessing the ITelephony interface using the java reflection technique.

在这里你去:

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

这篇关于如何在Android禁用移动数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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