Git Product home page Git Product logo

timerinservice's Introduction

在服务中运行定时任务

    需要实现一个定时处理任务的功能,你会如何处理?
    
    这功能很简单不是吗,没错就是Foregound Service + Timer,啪啪啪,一顿乱敲功能实现了。然后开测试,
也很顺利,不管是真机,还是模拟器,只要在设备不休眠的情况下,都很顺利完成测试。一旦设备休眠,噩梦
也随之开始。

    于是开始google防止设备休眠的方法,像什么WakeLock、电池白名单、后台运行白名单一一上了一遍,结
果呢。比较好的结果只能在部分设备休眠后还能正常运行,并不能真正解决。

    继续探索,直到有一天想到AlarmManager。于是便有了今天这事,记录并分享AlarmManager实现定时执行
任务功能。

代码很少,也很简单,就没必要去可以分析。

需要注意三点:

* 省电模式会影响后台运行
* app加入电池优化白名单
* app加入系统厂商白名单

不能说此方法一定能适配所有手机:
目前通过系统有:magic3.0.1|emui 10.0.1|miui 11 9.9.27
资源有限,其他暂时未进行测试

timerinservice's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.