如果测试后台数据和分组数据已启用 [英] Test if background data and packet data is enabled or not

查看:195
本文介绍了如果测试后台数据和分组数据已启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测用户是否启用了后台数据(设置 - >账户和SYNC->背景资料)和分组数据(设置 - >无线和网络 - >移动网络 - >使用数据包),这样我就可以告知用户如何启用它们。

I wish to detect if a user has enabled both background data (settings->accounts and sync->background data) and packet data (settings->wireless and network->mobile networks->use packet data) so I can inform the user how to enable them.

这<一href="http://stackoverflow.com/questions/4494483/how-to-check-if-background-data-is-enabled-on-the-android">link说如何测试后台的数据,但它已被德precated。该建议说,使用getActiveNetworkInfo(),但是这可能会返回WIFI连接,如果后台数据已启用,因此无法显示。

This link says how to test the background data but it has been deprecated. The recommendation says to use getActiveNetworkInfo() but this might return the WIFI connection and therefore not display if background data is enabled or not.

我还没有找到如何在分组数据已启用或不检测任何链接。

I have not found any links on how to detect if packet data is enabled or not.

推荐答案

我有这个确切同样的问题,我不得不开始赏金得到答案。我花了三分之一我的声誉,但非常值得。

I had this exact same question and I had to start a bounty to get the answer. Cost me a third of my reputation, but well worth it.

boolean mobileDataEnabled = false; // Assume disabled
        ConnectivityManager cm1 = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            Class cmClass = Class.forName(cm1.getClass().getName());
            Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true); // Make the method callable
            // get the setting for "mobile data"
            mobileDataEnabled = (Boolean)method.invoke(cm1);
        } catch (Exception e) {
            // Some problem accessible private API
            // TODO do whatever error handling you want here
        }

正如我说的,我不能居功知道这一点,我的问题是由<一个回答href="http://stackoverflow.com/users/769265/david-wasser">http://stackoverflow.com/users/769265/david-wasser

但它的成本我,所以,如果你想接受这个作为一个答案,我可以开始我的一些信誉分回给我! :)

but it cost me, so if you want to accept this as an answer I can start to get me some of my reputation points back! :)

这篇关于如果测试后台数据和分组数据已启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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