Android开发者 - 警报经理VS服务 [英] Android Developer - Alarm manager vs service

查看:98
本文介绍了Android开发者 - 警报经理VS服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序,需要执行的函数每一个小时甚至应用程序被关闭。

所有的

首先,我想创建一个服务,但在我的测试中,我认识到,机器人有时会杀死我的服务。所以我一直在寻找另一种解决方案,我发现AlarmManager。我已经实现了它,它似乎工作,但我有疑问,是否会发生同样的服务,否则将永远运行下去? (直到移动的重新启动......)

另一个问题,有必要建立一个新的线程来报警管理执行过程中,或者直接运行在其他线程?

解决方案
  

我已经实现了它,它似乎工作,但我有疑问,是否会发生同样的服务,否则将永远运行下去? (直到移动的重新启动......)

这将持续到:

  • 设备重新启动,为你指出的,或者
  • 用户卸载您的应用程序,或者
  • 您取消该事件自己,或者
  • 在用户进入设置,发现在已安装的应用程序列表中选择您的应用程序,水龙头该条目,并单击强制停止按钮

这可能是报警器将需要重新调度后您的应用程序升级(我忘了...)。

  

有必要建立一个新的线程来报警管理执行过程中,或者直接运行在其他线程??

除非你要做的将只需要几毫秒的工作,你会希望有一个后台线程它。这导致两种可能的方式:

  1. 如果您不使用 _WAKEUP 式的报警器,使用的getService() PendingIntent 发送控制到 IntentService 每隔一小时

  2. 如果您使用的是 _WAKEUP 式的报警器,您将需要使用 getBroadcast() PendingIntent ,并将它要么调用你的 WakefulIntentService ,否则你将需要管理一个 WakeLock 自己保持清醒的设备,而你做你的工作位

I am making an app that needs to execute a function each hour even the app is closed.

First of all, I thought to create a service, but during my tests, I realise that android sometimes kills my service. So I was looking for another solution and I found AlarmManager. I have implemented it and it seems to work but I have the doubt if it will happen the same the service or it will run forever? (Until reboot of the mobile...)

Another question, it is necessary to create a new thread to execute the process in alarm manager or it runs directly in other thread?

解决方案

I have implemented it and it seems to work but I have the doubt if it will happen the same the service or it will run forever? (Until reboot of the mobile...)

It will run until:

  • the device is rebooted, as you noted, or
  • the user uninstalls your app, or
  • you cancel the events yourself, or
  • the user goes into Settings, finds your app in the list of installed apps, taps on that entry, and clicks the Force Stop button

It's possible that alarms will need to be scheduled again after your app is upgraded (I forget...).

it is necessary to create a new thread to execute the process in alarm manager or it runs directly in other thread??

Unless the work you are going to do will take only a couple of milliseconds, you will want a background thread for it. That leads to two possible patterns:

  1. If you are not using a _WAKEUP-style alarm, use a getService() PendingIntent to send control to an IntentService every hour

  2. If you are using a _WAKEUP-style alarm, you will need to use a getBroadcast() PendingIntent, and have it either invoke your subclass of my WakefulIntentService, or you will need to manage a WakeLock yourself to keep the device awake while you do your bit of work

这篇关于Android开发者 - 警报经理VS服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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