如何在后台运行一个线程应用程序被杀害后, [英] How to run a thread in the background after the app is killed

查看:213
本文介绍了如何在后台运行一个线程应用程序被杀害后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要提供Toast通知用户每X分钟,我试着用服务的帮助下做到这一点,但是当应用程序被杀害,该服务停止。      会发生同样的问题,意图服务为好,我应该怎么做,才能让我保持一个线程/服务在内存中的应用程序被杀害,即使?

I want to provide the user with toast notifications every 'x' minutes, I tried to do this with the help of service, but when the application was killed, the service stops. The same issue occurs with intent Service as well, what should I be doing in order for me to keep a thread/service in memory even after the application is killed?

推荐答案

你的问题的条款是有点混乱:

The terms of your question are a bit confusing :

但是,你想要做的是什么,也许更简单:设置重复任务。对于这一点,你可以使用报警经理

But what you want to do is maybe simpler : set a repeating task. For this, you could use the alarm manager.

可以,例如:

  • 提供报警经理挂起的意图
  • 悬而未决的意图会引发你自己的
  • 的服务
  • 将我们唤醒服务,然后运行。这有没有让你的服务始终运行,计时知道什么时候已经醒过来(这会耗尽电池)的优势。在这里,报警管理器将唤醒它需要的时候,你的服务将只执行它的任务和死亡。

如果你把这种方法与答案的第一部分,在不同的进程中运行的服务,那么你就可以实现的东西,这真的重量轻的Andr​​oid设备:只有您的服务(在它自己的进程)将唤醒在给定的时间间隔,以及应用程序的进程将不会由该报警推出,但只有服务的进程。

If you combine this approach with the first part of that answer, running your service in a different process, then you can achieve something that's really light weight for the Android device : only your service (in its own process) will wake up at given interval, and the application's process will not be launched by this alarm, but only the service's process.

和有关Toast通知,是的,服务是一个好地方,但在通知栏可能更适合用来显示通知,并通知一些事件发生内部后台服务的用户。

And about toast notifications, yes, a service is a good place to do it, but the notification bar may be more appropriate to display notifications and notify the user that some event took place inside a background service.

这篇关于如何在后台运行一个线程应用程序被杀害后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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