Android:使重复的后台任务Android Oreo就绪的最佳方法是什么? [英] Android: What is the best way to make repetitive Background Tasks Android Oreo ready?

查看:55
本文介绍了Android:使重复的后台任务Android Oreo就绪的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说Background Services无法在android Oreo上免费运行. 我有点困惑应该如何重写我的代码. 我现在正使用android工作一个月左右,因此请尝试 回答尽可能简单.

I heard Background Services won't work that free on android Oreo. I'm kinda confused how I should rewrite my Code. I'm working with android for a month or so now, so please try to answer as simple as possible.

我在MainActivity的onCreate()中有一个称为启动时"的服务. 在服务的onStartCommand中,它正在调用Handler. 该处理程序将每半分钟postDelay()本身并调用一个函数. 此功能会执行一些api请求,并在应用某些条件时推送通知.

I have a Service that is called On Boot and in the onCreate() of my MainActivity. In the service's onStartCommand it is calling a Handler. This handler will postDelay() itself every half minute and call a function. This function does some api requests and will push a notification when certain conditions were applied.

让此代码在android O +上运行的最佳方法是什么?

What is the best way to let this code work on android O+?

我考虑过使用前台服务并显示无用的ongoing通知,用户可以 看不见,但这个想法听起来不好.

I thought about using a Foreground Service and display a useless ongoing notification, the user can make invisible but that idea does not sound good.

推荐答案

此处理程序将每半分钟发布一次postDelay()并调用一个函数

This handler will postDelay() itself every half minute and call a function

在Doze模式和应用待机状态下,此功能无法在Android 6.0+上可靠地运行.特别是在几乎经常出现的任何地方工作都会对电池造成不良影响,因此Google会竭尽全力防止这种行为.

This will not work reliably on Android 6.0+, courtesy of Doze mode and app standby. In particular doing work anywhere nearly that frequently will be bad for the battery, and so Google is going to great lengths to prevent this sort of behavior.

让此代码在android O +上运行的最佳方法是什么?

What is the best way to let this code work on android O+?

最好的办法是完全摆脱它.使用JobScheduler并减少定期工作的频率(例如,每15分钟一次).

The best thing to do is to get rid of it entirely. Use JobScheduler and do periodic work less frequently (e.g., every 15 minutes).

使用前台服务将使您的应用在Android 8.0+上的运行方式与在Android 6.0+上的运行方式相同(即仍然不可靠,但至少可以运行一分钟以上).

Using a foreground service will have your app behave on Android 8.0+ the way that it did on Android 6.0+ (i.e., still unreliable, but at least working for more than one minute).

我考虑过使用前台服务并显示无用的正在进行中的通知,用户可以使其不可见,但这种想法听起来不太好.

I thought about using a Foreground Service and display a useless ongoing notification, the user can make invisible but that idea does not sound good.

发出有用的通知,允许用户控制服务的行为.

Make a useful notification, allowing the user to control the behavior of the service.

这篇关于Android:使重复的后台任务Android Oreo就绪的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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