相关疑难解决方法(0)

以编程方式在android中启用/禁用数据连接

我想以编程方式启用/禁用数据连接.我使用了以下代码:

void enableInternet(boolean yes)
{
    ConnectivityManager iMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    Method iMthd = null;
    try {
        iMthd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
        } catch (Exception e) {
               } 
    iMthd.setAccessible(false);

    if(yes)
     {

                try {
                    iMthd.invoke(iMgr, true);
                    Toast.makeText(getApplicationContext(), "Data connection Enabled", Toast.LENGTH_SHORT).show();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                     dataButton.setChecked(false);
                     Toast.makeText(getApplicationContext(), "IllegalArgumentException", Toast.LENGTH_SHORT).show();

                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(getApplicationContext(), "IllegalAccessException", Toast.LENGTH_SHORT).show();

                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                     dataButton.setChecked(false);
                     Toast.makeText(getApplicationContext(), "InvocationTargetException", Toast.LENGTH_SHORT).show();

                } …
Run Code Online (Sandbox Code Playgroud)

android gprs invocationtargetexception

47
推荐指数
1
解决办法
7万
查看次数

从Android L及更高版本开始,setMobileDataEnabled方法不再可调用

我已经向Google报告了问题78084,因为该setMobileDataEnabled()方法不再可以通过反射调用.它可以通过反射从Android 2.1(API 7)到Android 4.4(API 19)进行调用,但是从Android L及更高版本开始,即使使用root,该setMobileDataEnabled()方法也不可调用.

官方回应是问题是"已关闭",状态设置为"WorkingAsIntended".谷歌的简单解释是:

私有API是私有的,因为它们不稳定,可能会在没有通知的情况下消失.

是的,谷歌,我们意识到使用反射调用隐藏方法的风险 - 甚至在Android出现之前 - 但是你需要提供一个更加可靠的答案,如果有的话,可以实现相同的结果setMobileDataEnabled().(如果您对Google的决定不满意,请登录问题78084,并尽可能多地将其加注,以便让Google知道他们的错误.)

所以,我的问题是:在Android设备上以编程方式启用或禁用移动网络功能时,我们是否处于死胡同?谷歌的这种严厉的方法在某种程度上并不适合我.如果您有Android 5.0(Lollipop)及其他方法的解决方法,我很乐意听到您在此主题中的回答/讨论.

我使用下面的代码来查看该setMobileDataEnabled()方法是否可用:

final Class<?> conmanClass = Class.forName(context.getSystemService(Context.CONNECTIVITY_SERVICE).getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(context.getSystemService(Context.CONNECTIVITY_SERVICE));
final Class<?> iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method[] methods = iConnectivityManagerClass.getDeclaredMethods();
for (final Method method : methods) {
    if (method.toGenericString().contains("set")) {
        Log.i("TESTING", "Method: " + method.getName());
    }
}
Run Code Online (Sandbox Code Playgroud)

但事实并非如此.

更新:目前,如果设备已植根,则可以切换移动网络.但是,对于非根设备,它仍然是一个调查过程,因为没有通用的方法来切换移动网络.

reflection android android-5.0-lollipop

47
推荐指数
3
解决办法
4万
查看次数

如何在Android上禁用移动数据

在有人告诉我购买应用程序之前快速回复故事.=)

我刚拿到EVO,它可以很快地咀嚼电池.我下载了JuiceDefender来管理我的移动数据连接.这看起来相当不错.但是,设置非常有限(即使在付费版本上).

截至目前,我正在尝试开发更加可定制的节电应用程序.我想要做的主要事情是能够随意启用/禁用移动数据连接.

问题是我找不到任何代码片段或文章如何做到这一点.我发现的唯一的事情是以下.我不知道这是多么准确,但这就是我可以拼凑浏览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());
Run Code Online (Sandbox Code Playgroud)

如果有人能指出任何有帮助的事情,那将是非常感激的.

UPDATE

似乎Sprint上的HTC Evo不使用APN设置.我通过下载APNDroid并观察它无法正常测试.然后,我制作了一个快速应用程序,将所有APN条目转储到屏幕上.这产生了一个结果,它是为了mms.

查看JuiceDefender运行时的电话信息,我发现GSRP网络正在打开和关闭.这让我相信通过代码可以做到这一点,即使我发现询问同一问题的每一页都表示无法完成.踢球者是他们所说的像APNDroid一样.请有人给我一些见解.

谢谢!

android

44
推荐指数
2
解决办法
9万
查看次数

Android L(5.x)以编程方式打开/关闭"移动数据"

我需要以编程方式打开/关闭移动数据.下面的代码不适用于5.x. 你能帮我么.提前致谢.

private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class.forName(conman.getClass().getName());
        final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
        connectivityManagerField.setAccessible(true);
        final Object connectivityManager = connectivityManagerField.get(conman);
        final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        setMobileDataEnabledMethod.invoke(connectivityManager, enabled);    }
Run Code Online (Sandbox Code Playgroud)

03-30 12:42:29.466:W/System.err(5966):java.lang.NoSuchMethodException:setMobileDataEnabled [boolean] 03-30 12:42:29.466:W/System.err(5966):at java.lang .Class.getMethod(Class.java:664)03-30 12:42:29.466:W/System.err(5966):at java.lang.Class.getDeclaredMethod(Class.java:626)

java.lang.NoSuchMethodException:setMobileDataEnabled [boolean] @在line下面.

final方法setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);

android android-5.0-lollipop

7
推荐指数
1
解决办法
2万
查看次数

用于打开移动数据的意图?

我可以android.settings.WIRELESS_SETTINGS用来打开无线和蓝牙,但我应该用什么打开移动互联网?

android android-intent

5
推荐指数
2
解决办法
5313
查看次数