Giter VIP home page Giter VIP logo

backgroundtask's Introduction

BackgroundTask

swift实现iOS应用在进入到后台之后,依旧可以执行任务,并不受时间的限制

思路:

  1. 使用UIApplication对象的beginBackgroundTaskWithExpirationHandler申请后台执行任务,该任务只有大概3分钟的运行时间

  2. 应用申请到后台执行任务后,使用NSTimer开启一个定时任务,主要负责监控应用剩余的后台可执行时间,当可用的时间少于一个值时,播放一段默声音乐,然后调用UIApplication对象的endBackgroundTask方法将之前申请的后台执行任务结束掉,最后再重新申请一个后台执行任务,这样就可以实现后台不限时执行任务了

  3. 应用在后台播放音乐,需要开启Background Modes,然后勾选Audio and AirPlay即可

注:应用在后台运行的过程中重新申请后台执行任务是无效的,通过在网上查找资料,播放音乐可以让应用进入到一个假前台的状态,此时重新申请后台执行任务是有效的,如此循环n次,就可以获得大约3n的后台执行时间,从而达到后台无限时执行任务

backgroundtask's People

Watchers

 avatar  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.