App如何在background状态下存活

一般来说,如果不进行后台申请,在iOS系统上,当应用退到后台后,只有5s的时间去执行代码,之后将进入挂起状态。只有像音频播放、定位、newsstand、VoIP等功能才能持续在后台运行。但是开发其它应用是我们可以通过申请后台,来获得3分钟的后台执行代码时间(iOS7以前是10分钟)。

程序有一下几种状态。

  • Not running 未运行 程序没启动
  • Inactive        未激活 程序在前台运行,不过没有接收到事件。在没有事件处理情况下程序通常停留在这个状态
  • Active           激活 程序在前台运行而且接收到了事件。这也是前台的一个正常的模式
  • Backgroud   后台 程序在后台而且能执行代码,大多数程序进入这个状态后会在在这个状态上停留一会。时间到之后会进入挂起状态
  • Suspended 挂起 程序不执行代码。系统会自动把程序变成这个状态而且不会发出通知。当 挂起时,程序还是停留在内存中的,当系统内存低时,系统就把挂起的程序清除掉,为前台程序提供更多的内存。

 我们可以通过下面的code,让程序一直保持在“Backgroud”状态。

class AppDelegate: UIResponder, UIApplicationDelegate {

    var bgTask : UIBackgroundTaskIdentifier?
    var timer : Timer?
    var remaining : Int = 0

    func applicationDidEnterBackground(_ application: UIApplication) {
        comeToBackGround(name: "A")
    }
/* beginBackgroundTaskWithExpirationHandler 方法只能让App在background状态下存在30s,
然后系统会强制挂起app,或者开发者自己调用 endBackgroundTask 方法 ,
所以我们要让app做一些任务比如播放音乐,定位...才能持续申请background持续时间 */
    func comeToBackGround(name:String){
        print("ktkt: Start!!!")
        let app = UIApplication.shared
        self.bgTask = app.beginBackgroundTask(withName: name, expirationHandler: {
            print("ktkt:Reamining_ 0 :\(self.remaining)")
            if let bgTsk = self.bgTask{//大概30s,掉用一次
                AudioPlayTool.shared.playAudio(with:“1秒的无声音乐”)
                app.endBackgroundTask(bgTsk)
                self.comeToBackGround(name: "BGTask:\(self.remaining)")
            }
        })
        if self.timer != nil {
            self.timer?.invalidate()
            self.timer = nil
        }
        self.timer = Timer.scheduledTimer(timeInterval:1, target: self, selector: #selector(applyForMoreTime), userInfo: nil, repeats: true)
        self.timer?.fire()
    }
    
    @objc func applyForMoreTime(){
        let app = UIApplication.shared
        let state = app.applicationState
        print("ktkt:Reamining_ 1 :\(remaining) | \(state.rawValue)")
        
        remaining += 1
        
        if remaining >= 3 * 60 {//其实可以持续很久,这里写了3min
            exit(0) //kill app
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在uni-app中,可以通过Vuex来引入状态。 1. 在src目录下新建一个store文件夹,并在store文件夹中新建一个index.js文件,代码如下: ``` import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ }, decrement (state) { state.count-- } }, actions: { incrementAsync ({ commit }) { setTimeout(() => { commit('increment') }, 1000) } }, getters: { getCount: state => { return state.count } } }) export default store ``` 2. 在main.js中引入store并挂载到Vue实例上,代码如下: ``` import Vue from 'vue' import App from './App' import store from './store' Vue.config.productionTip = false App.mpType = 'app' const app = new Vue({ ...App, store }) app.$mount() ``` 3. 在需要使用状态的组件中,使用mapState、mapMutations、mapActions、mapGetters等函数来获取状态,代码如下: ``` <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> <button @click="decrement">Decrement</button> <button @click="incrementAsync">Increment Async</button> <p>GetCount: {{ getCount }}</p> </div> </template> <script> import { mapState, mapMutations, mapActions, mapGetters } from 'vuex' export default { computed: { ...mapState({ count: state => state.count }), ...mapGetters([ 'getCount' ]) }, methods: { ...mapMutations([ 'increment', 'decrement' ]), ...mapActions([ 'incrementAsync' ]) } } </script> ``` 以上代码中,mapState函数将state中的count映射到组件的计算属性中,mapMutations函数将mutations中的increment和decrement映射到组件的方法中,mapActions函数将actions中的incrementAsync映射到组件的方法中,mapGetters函数将getters中的getCount映射到组件的计算属性中。 通过以上步骤,即可在uni-app中引入状态

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值