为什么 android (6.0) 系统在深度睡眠约 1 小时后禁止网络连接

Why the android (6.0) system forbit a network connection after ~1 hour in deep sleep

我在深度睡眠一小时后无法使用 android 的深度睡眠模式。 该问题仅发生在 Android 6+ 上。在 Android <5 上,问题没有发生。未使用 Android 5 进行测试,因为没有可用的设备。

设备配置:
我有两台 Android 6 设备,Google Nexus 5 和 HTC One M9。 两个设备都配置为仅 Wifi(没有 sim 卡)并且 wifi 策略配置为始终开启(即使在睡眠模式下)。

情况:
我有一个清醒的广播接收器,它在 AlarmManager.ELAPSED_REALTIME_WAKEUP 上注册,当应用程序进入后台时每 2 分钟触发一次。当应用程序暂停和释放时,如果应用程序进入前台,也会获取 wifi 锁。

因为 Android KitKat AlarmManager.setRepeating(...) 是 inexat,在 Android 6 我使用 AlarmManager.setWindow(...) 和 window 只有 1 秒。
每次接收器触发时,都会重新注册接收器 (PendingIntent)。

接收者的工作很简单。他应该只调用一个 weburl(获取请求)。请求成功、超时或抛出异常后释放wakelock。

在清单中,WAKE_LOCK 权限也存在。

问题:
当我将应用程序置于后台(启用​​接收器)然后关闭屏幕时,接收器每 2 分钟被正确调用一次,但大约 1 小时后网络请求失败。

日志显示1h后receiver也被调用,只是网络请求失败

源代码示例:

public class TestTools {
    private static final String LOG_TAG = TestTools.class.getSimpleName();

    public static String excuteGet(String targetURL) {
        try {
            URL obj = new URL(targetURL);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setConnectTimeout(10*1000);
            con.setReadTimeout(5*1000);

            int responseCode = con.getResponseCode();
            Log.d(LOG_TAG, "GET Response Code :: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) { // success
                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // print result
                Log.d(LOG_TAG, response.toString());

                return response.toString();
            } else {
                Log.d(LOG_TAG, String.format("GET request not worked (response code :: %s)", responseCode));
            }
        }
        catch (ProtocolException e) {
            Log.d(LOG_TAG, "ProtocolException: " + e.getMessage());
        }
        catch (MalformedURLException e) {
            Log.d(LOG_TAG, "MalformedURLException: " + e.getMessage());
        }
        catch (IOException e) {
            Log.d(LOG_TAG, "IOException: " + e.getMessage());
        }

        return null;
    }

}

public class Receiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        Log.d(LOG_TAG, "onReceive");

        final Thread test = new Thread(new Runnable() {
            @Override
            public void run() {
                TestTools.excuteGet("http://www.google.de/");
            }
        });
        test.start();

        try {
            test.join();
        } catch (InterruptedException e) {
            Log.d(LOG_TAG, e.getMessage());
        }

        // here the receiver is reregistered

        WakefulBroadcastReceiver.completeWakefulIntent(intent);
    }
}

您知道出了什么问题以及如何解决吗?


更新: 要使应用程序在 Android 休眠模式下工作,您需要采用此 https://developer.android.com/training/monitoring-device-state/doze-standby.html#assessing_your_app

but after ~1 hour the network request fails

据推测,设备进入 Doze mode

Do you have an idea what is going wrong

从用户的角度,以及 Android 的角度来看,都没有问题。一切都按预期工作。

请理解,添加 Doze 模式是专门为了防止开发人员执行您正在尝试执行的操作。每两分钟进行一次网络 I/O 对电池寿命来说非常糟糕。太多开发者不关心用户和那些用户的电池,因此 Android 正在从这些开发者手中夺走控制权,以帮助用户。

how to fix it?

在很大程度上,没有什么可以解决的。同样,一切都按照规范进行。您可能需要调整您的期望值。

现在,如果用户认为用户想让您的应用消耗更多电池,用户可以将您的应用添加到电池优化白名单。这是在“设置”>“应用程序”>(操作栏中的齿轮图标)> "Ignore battery optimizations" 中。任何设置了 "ignore battery optimizations" 的应用程序将更像旧设备。

也欢迎用户将他们的设备放在充电器上;充电时,设备不会进入打瞌睡模式。