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

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

问题描述

我正在构建一个应用程序,当用户启用或禁用他的设备上的移动数据使用时,我想捕获。

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

我阅读了关于使用 android.net.conn.CONNECTIVITY_CHANGE 来监控这些变化,但后来我遇到了各种各样的帖子和问题,表示这个不会 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,你可以添加代码到设置应用程序在移动数据启用/禁用时,将广播一个事件。

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 https://stackoverflow.com/a/12864897/769265
    }
}

有关详情,请参阅 https://stackoverflow.com/a/12864897/769265 关于如何确定移动数据设置的当前状态的信息。

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

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

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