AlarmManager或处理程序 [英] AlarmManager or Handler

查看:107
本文介绍了AlarmManager或处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有特定的情况,每10-60分钟一次

I have specific case to ping my server every 10-60 minutes (it still depends) but only when app is opened. This feature is created to inform that session is still open where session is defined as period from app open to app close. I don't have to worry about process kill.

什么是更好使用的? AlarmManager Handler .postDelayed()吗?

What is better to use? AlarmManager or Handler.postDelayed() ?

目标平台是android电视,所以可以想象情况是在观看时

The targeted platform is android tv so imagine the case is when watching film in context of my app for example.

我个人最初想到使用AlarmManager,但我意识到与情况相比,这是产生更多代码的方式。
处理程序是否导致更多 CPU 使用量增加

Personally I first thought to use AlarmManager but I realized it's way more code to produce compared the circumstances. Is handler causing more CPU usage increase ?

推荐答案

AlarmManager 在将来未运行时启动您的应用程序。因此,我认为 Handler.postDelayed()是一个更有效的选择,如果您仅在应用程序打开时对服务器进行ping操作。

AlarmManager starts your app in the future when it is not running. So I think Handler.postDelayed() is a more efficient choice if you will only ping the server when the app is open.


注:警报管理器用于希望在特定时间运行应用程序代码的情况,即使您的应用程序当前未运行。对于正常的计时操作(滴答声,超时等),使用Handler会更轻松,更有效。

Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.

请参见 AlarmManager

这篇关于AlarmManager或处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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