检查是否启用了后台限制数据? [英] Check if background restriction data is enabled or not?

查看:206
本文介绍了检查是否启用了后台限制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在主线程上运行的服务,而不是在后台运行.在服务中,我正在检查通过broadcastreciver的网络连接.当我在设置"中启用了启用限制数据时,broadcastreciver可以很好地捕获意图,但是虽然我的应用程序已在我的应用程序上禁用了Internet连接(移动数据).我已经看到这个问题 Android文档.

intentFilter.addAction("android.net.conn.ACTION_BACKGROUND_DATA_SETTING_CHANGED");
    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(this.connectionStatusReceiver, intentFilter);


private BroadcastReceiver connectionStatusReceiver = new BroadcastReceiver() {
    @Override
    public synchronized void onReceive(Context context, Intent intent) {
        log.info("connection state changed");            
        checkConnectionState();
    }
};

 getActiveNetworkInfo() - *will always display CONNECTED when i switch on *restriction**

还有一件事,当我打开后台数据限制时,我检查了intent.getAction.它总是返回android.net.conn.CONNECTIVITY_CHANGE. 如何检查背景限制数据是否已打开/关闭?

解决方案

您可以从Android 7.0(API 24)及更高版本中进行检查. Google在其Android开发人员网站中添加了howto.这是链接优化网络数据使用

I have a service which is running on the main thread not in the background. In the service I'm checking net connection via broadcastreciver. When I enable restriction data enabled in Settings, broadcastreciver is catching intent well, but internet connection(Mobile data) is disabled to my app although it has on my device. I've seen this question, and android docs.

intentFilter.addAction("android.net.conn.ACTION_BACKGROUND_DATA_SETTING_CHANGED");
    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(this.connectionStatusReceiver, intentFilter);


private BroadcastReceiver connectionStatusReceiver = new BroadcastReceiver() {
    @Override
    public synchronized void onReceive(Context context, Intent intent) {
        log.info("connection state changed");            
        checkConnectionState();
    }
};

 getActiveNetworkInfo() - *will always display CONNECTED when i switch on *restriction**

One more thing, when I switch on background data restrict, I checked intent.getAction. It always returns android.net.conn.CONNECTIVITY_CHANGE. How can I check background restriction data is switched on/off?

解决方案

You can check that from Android 7.0 (API 24) and above. Google added the howto in their android developer site. Here is the link Optimizing Network Data Usage

这篇关于检查是否启用了后台限制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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