监视,当用户启用/禁用移动数据 [英] Monitor when the user enables/disables mobile data

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

问题描述

我要建一个应用程序,关于这一点我想捕捉,当用户启用或禁用了设备上的移动数据使用量。

I'm building an app, on which I want to capture when a user enables or disables mobile data usage on his device.

我读到关于使用<一个href="http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges"相对=nofollow> android.net.conn.CONNECTIVITY_CHANGE 监测这种变化,但我碰到的各种文章和问题在这里是说,这<一个href="http://stackoverflow.com/questions/17082812/do-not-receive-mobile-state-state-change-event-when-wi-fi-is-enabled">wouldn't <一href="http://stackoverflow.com/questions/12806709/android-how-to-tell-if-mobile-network-data-is-enabled-or-disabled-even-when?lq=1">work,和大多数的答案建议使用反射。

I read about using android.net.conn.CONNECTIVITY_CHANGE to monitor such changes, but then I came across various posts and questions here that said that this wouldn't work, and the majority of the answers suggested to use reflection.

这些使我相信,有没有当用户改变移动数据,我可以使用的状态下被解雇特定的事件。

These led me to believe that there is no specific event being fired when the user changes the state of mobile data that I can use.

有没有可能是一个自定义事件可以创建处理呢?如果没有,你会建议最好的做法是监测移动数据的状态呢?

Is it possible that a custom event can be created to handle it? If not, what would you suggest the best practice is for monitoring the state of mobile data?

在此先感谢

推荐答案

有没有当用户改变设置启用移动数据广播任何事件。关于要做到这一点(要比监听 CONNECTIVITY_CHANGE 事件,如你所描述等)将是检查启用的设置定期对移动数据的状态的唯一途径。如果你这样做太频繁,不过,你只会耗尽电池。

There aren't any events broadcast when the user changes the mobile data enabled setting. About the only way to do this (other than listening for CONNECTIVITY_CHANGE events, as you've described) would be to check the state of the mobile data enabled setting on a periodic basis. If you do this too frequently, though, you will just drain the battery.

如果您要创建自己的自定义ROM,可以肯定会增加code的设置应用程序,将广播一个事件时,移动数据已启用/禁用。

If you are creating your own custom ROM, you could certainly add code to the Settings app that would broadcast an event when mobile data is enabled/disabled.

修改添加一个方法来获得通知时设置被修改

其实,我已经找到一种方法来做到这一点。你可以随时移动数据设置更改的通知。不幸的是,要做到这一点的唯一方法是注册通知,每当任何设置被改变。但是,这不会发生,往往与至少你不必轮询,每隔一段时间,耗尽你的电池。你想要做的是注册一个 ContentObserver 安全设置,像这样的:

Actually, I've found a way to do this. You can get a notification whenever the mobile data setting is changed. Unfortunately, the only way to do this is to register for notifications whenever any setting is changed. However, this doesn't happen that often and at least you don't have to poll every so often and drain your battery. What you want to do is to register a ContentObserver on the Secure settings, like this:

contentObserver = new SettingsObserver();
getApplicationContext().getContentResolver().registerContentObserver(
            Settings.Secure.CONTENT_URI, true, contentObserver);

您应该落实 SettingsObserver 是这样的:

public class SettingsObserver extends ContentObserver {

    public SettingsObserver() {
        super(new Handler());
    }

    @Override
    public void onChange(boolean selfChange) {
        // Here you want to check the state of mobileDataEnabled using reflection to
        //  see if it has changed - see http://stackoverflow.com/a/12864897/769265
    }
}

请参阅 http://stackoverflow.com/a/12864897/769265 有关如何确定当前的更多信息移动数据设置状态。

See http://stackoverflow.com/a/12864897/769265 for more information on how to determine the current state of the mobile data setting.

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

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