android-datasaver https://www.e-learn.cn/tag/android-datasaver zh-hans Is there a way to know if Data Saver is enabled? https://www.e-learn.cn/topic/4120256 <span>Is there a way to know if Data Saver is enabled?</span> <span><span lang="" about="/user/113" typeof="schema:Person" property="schema:name" datatype="">╄→尐↘猪︶ㄣ</span></span> <span>2021-02-20 10:26:34</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Android 7.0 Nougat added Data Saver feature allowing users to restrict background data of certain apps (including push notifications). When Data Saver is ON, only the apps on the list found in</p> <blockquote> <p>Settings → Data Saver → Unrestricted data access</p> </blockquote> <p>are allowed to receive push notifications and execute background network calls. If Data Saver is OFF and your app is not on the unrestricted list, it's pretty much like setting push notifications disabled.</p> <p>There is a use case in my app where it's waiting for a push notification to come.</p> <p>I wonder if there is a way to find out if Data Saver is enabled and potentially if my app is on the 'Unrestricted data access' list to know if push notifications are enabled for my app and therefore if there is a point in waiting for the push and a chance to execute any network calls while the app is in the background at a certain time.</p> <br /><h3>回答1:</h3><br /><p>Checking if Data Saver is enabled and if your app is whitelisted is possible via <code>ConnectivityManager.getRestrictBackgroundStatus()</code></p> <pre><code>public boolean checkBackgroundDataRestricted() { ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); switch (connMgr.getRestrictBackgroundStatus()) { case RESTRICT_BACKGROUND_STATUS_ENABLED: // Background data usage and push notifications are blocked for this app return true; case RESTRICT_BACKGROUND_STATUS_WHITELISTED: case RESTRICT_BACKGROUND_STATUS_DISABLED: // Data Saver is disabled or the app is whitelisted return false; } } </code></pre> <p>If Data Saver is enabled and your app is not whitelisted, push notifications will only be delivered when your app is in the foreground.</p> <p>You can also check <code>ConnectivityManager.isActiveNetworkMetered()</code> if you should limit data usage no matter if Data Saver is enabled or disabled or if your app is whitelisted.</p> <p>Complete example in the docs where you can also learn how to request whitelist permission and listen to changes to Data Saver preferences.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/43390930/is-there-a-way-to-know-if-data-saver-is-enabled</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/android-datasaver" hreflang="zh-hans">android-datasaver</a></div> <div class="field--item"><a href="/tag/android-push-notification" hreflang="zh-hans">android-push-notification</a></div> </div> </div> Sat, 20 Feb 2021 02:26:34 +0000 ╄→尐↘猪︶ㄣ 4120256 at https://www.e-learn.cn Is there a way to know if Data Saver is enabled? https://www.e-learn.cn/topic/4120221 <span>Is there a way to know if Data Saver is enabled?</span> <span><span lang="" about="/user/27" typeof="schema:Person" property="schema:name" datatype="">我只是一个虾纸丫</span></span> <span>2021-02-20 10:14:11</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Android 7.0 Nougat added Data Saver feature allowing users to restrict background data of certain apps (including push notifications). When Data Saver is ON, only the apps on the list found in</p> <blockquote> <p>Settings → Data Saver → Unrestricted data access</p> </blockquote> <p>are allowed to receive push notifications and execute background network calls. If Data Saver is OFF and your app is not on the unrestricted list, it's pretty much like setting push notifications disabled.</p> <p>There is a use case in my app where it's waiting for a push notification to come.</p> <p>I wonder if there is a way to find out if Data Saver is enabled and potentially if my app is on the 'Unrestricted data access' list to know if push notifications are enabled for my app and therefore if there is a point in waiting for the push and a chance to execute any network calls while the app is in the background at a certain time.</p> <br /><h3>回答1:</h3><br /><p>Checking if Data Saver is enabled and if your app is whitelisted is possible via <code>ConnectivityManager.getRestrictBackgroundStatus()</code></p> <pre><code>public boolean checkBackgroundDataRestricted() { ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); switch (connMgr.getRestrictBackgroundStatus()) { case RESTRICT_BACKGROUND_STATUS_ENABLED: // Background data usage and push notifications are blocked for this app return true; case RESTRICT_BACKGROUND_STATUS_WHITELISTED: case RESTRICT_BACKGROUND_STATUS_DISABLED: // Data Saver is disabled or the app is whitelisted return false; } } </code></pre> <p>If Data Saver is enabled and your app is not whitelisted, push notifications will only be delivered when your app is in the foreground.</p> <p>You can also check <code>ConnectivityManager.isActiveNetworkMetered()</code> if you should limit data usage no matter if Data Saver is enabled or disabled or if your app is whitelisted.</p> <p>Complete example in the docs where you can also learn how to request whitelist permission and listen to changes to Data Saver preferences.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/43390930/is-there-a-way-to-know-if-data-saver-is-enabled</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/android" hreflang="zh-hans">android</a></div> <div class="field--item"><a href="/tag/android-datasaver" hreflang="zh-hans">android-datasaver</a></div> <div class="field--item"><a href="/tag/android-push-notification" hreflang="zh-hans">android-push-notification</a></div> </div> </div> Sat, 20 Feb 2021 02:14:11 +0000 我只是一个虾纸丫 4120221 at https://www.e-learn.cn