“BLOCKED”网络状态在Android的数据节省功能关闭时出现。

huangapple go评论79阅读模式
英文:

Getting "BLOCKED" network state when Android's data saver is off

问题

我正在开发一个用于特定运行Android 7.0的平板电脑的应用程序。我正在使用Service来定期发送DatagramPacket我只能使用移动网络。我使用以下代码检查连接状态:

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
DetailedState networkState = activeNetwork.getDetailedState();
  • 如果平板电脑连接电源并且屏幕打开或关闭,我会得到CONNECTED状态。
  • 如果平板电脑没有连接电源并且屏幕打开,我会得到CONNECTED状态。
  • 如果平板电脑没有连接电源并且屏幕关闭,我会得到BLOCKED状态。

在我的平板电脑上,数据节省模式已关闭。以防万一,我已运行测试以验证屏幕关闭时没有限制,cm.getRestrictBackgroundStatus()始终返回1,这是RESTRICT_BACKGROUND_STATUS_DISABLED,即使网络被阻止。

那么,为什么网络被阻止?我的应用程序需要特殊权限吗?我的平板电脑需要特殊配置吗?

英文:

I am developing an app to be used on a specific tablet running Android 7.0. I'm using a Service to send periodic DatagramPackets. I can only use mobile networks. I check the connectivity status using ConnectivityManager, NetworkInfo and DetailedState. I get the network state with this code:

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
DetailedState networkState = activeNetwork.getDetailedState();
  • If the tablet is plugged in and the screen is on or off, I get CONNECTED state.
  • If the tabled is unplugged and the screen is on, I get CONNECTED state.
  • If the tabled is unplugged and the screen is off, I get BLOCKED state.

In my tablet the Data Saver is off. Just in case, I have run tests to verify there are no restrictions when the screen turns off, cm.getRestrictBackgroundStatus() always returns 1, which is RESTRICT_BACKGROUND_STATUS_DISABLED, even when the network is blocked.

So, why is the network blocked? Do my app need special permissions? Do my tablet need special configuration?

答案1

得分: 2

为了提供一个完整的解决方案,我将回答我的问题。

> 网络为什么被阻止?

根据Stanislav Kireev提供的文档,这个被阻止的网络状态是因为设备处于Doze模式。从Android 6.0(API级别23)开始,这种设备状态不允许访问互联网。

但文档还说,“系统提供了一个可配置的应用程序白名单,部分免除Doze和应用程序待机优化”。有关更详细的解释,您可以阅读支持其他用例部分。您可以从代码中将您的应用添加到白名单,或者配置系统。

> 我的应用需要特殊权限吗?

如果您需要从代码中将您的应用添加到白名单中,您需要在清单文件中添加一个权限:

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

并将以下代码添加到您的活动中:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    String packageName = getPackageName();
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    if (!pm.isIgnoringBatteryOptimizations(packageName)) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
        startActivity(intent);
    }
}

我从这个答案中得到了这个解决方案。

> 我的平板需要特殊配置吗?

如果您愿意,您可以从设置 > 电池 > 三个点 > 电池优化中添加您的应用程序。在选择列表中选择所有应用程序,选择您的应用程序,然后选择不优化选项。

我从这个答案中得到了这个解决方案(它有更详细的解释)。

英文:

In order to give a complete solution I will answer my question.

> why is the network blocked?

As the documentation provided by Stanislav Kireev pointed, this BLOCKED network state happens because the device is in Doze. This device state does not allow internet access starting from Android 6.0 (API level 23).

But the documentation also says that "the system provides a configurable whitelist of apps that are partially exempt from Doze and App Standby optimizations". For more detailed explanation you can read Support for other use cases section. You can add your app to the whitelist from code or configuring the system.

> Do my app need special permissions?

If you need to add your app to the whitelist from code, you need to add a permission in the manifest file:

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

And add this code to your activity:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    String packageName = getPackageName();
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    if (!pm.isIgnoringBatteryOptimizations(packageName)) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
        startActivity(intent);
    }
}

I got this solution from this answer.

> Do my tablet need special configuration?

If you want, you can add your app from Settings > Battery > 3 dots > Battery optimisation. Choose All apps in the pick list, select your app and choose Don’t optimise option.

I got this solution from this answer (it has more detailed explanation).

答案2

得分: 1

这是正常行为。当您的屏幕被锁定时,它会使用待机模式。待机模式会关闭互联网通信和所有后台服务。您需要唤醒您的设备,然后才能进行与互联网相关的所有操作。文档链接

英文:

It's normal behaviour. When your screen is locked, it use doze mode. And Doze mode turn off internet communication and all background services. You need wakeup your device and after you can make all actions with internet. Documentations

huangapple
  • 本文由 发表于 2020年7月22日 22:40:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63036867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定