在后台运行一个服务永远..?安卓 [英] Run a service in the background forever..? Android

查看:118
本文介绍了在后台运行一个服务永远..?安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做对Android手机的电池消耗的研究。我想运行一个电池检查每10分钟,直到电池完全死亡。我一直有问题,使其工作。

在我的第一次尝试,我在服务类中使用一个计时器,并安排电池检查每10分钟。但很快我发现,该服务得到了暂停时,屏幕熄灭。

然后我尝试使用AlarmService,我用一个报警电话唤醒我的服务,每10分钟,并检查电池电量和数据保存到SD卡中的文件。它的工作原理与屏幕关闭。不过,我只得到了9个小时的数据......似乎AlarmService几个小时后,停在某一点。我不知道为什么是这样的,有系统,把它打死了内存问题?

我的问题是,有没有人写了一些服务来运行(如永远)的背景下过吗?你是怎么做到这一点,我倒是AP preciate非常多的样本code?

我目前正在看一些帖子说,有一个局部唤醒锁,我可以用它来保持服务活着......是这样做的正确方法?

感谢了很多,我希望我能在这里得到一些有用的答案。

解决方案
  

在我的第一次尝试,我在使用计时器   服务类和时间表   检查电池每隔10分钟。但很快   我发现,该服务得到了暂停   当屏幕熄灭。

您可能没有持有 WakeLock ,所以设备睡着了。

  

似乎AlarmService停在   几个小时后,某一点

我相当怀疑。

  

我的问题是,有没有人写   一些服务运行(如永远)的   之前的背景是什么?

这是不可能的创建将永远运行的服务。它应该是可以创建通过 AlarmManager 计划任务会被调用永远。

  

我目前正在读一些职位   他说,有一个局部唤醒   锁我可以用它来保持服务   活着..这是做了正确的方法   它?

我不知道什么是它。但是,如果你想保持清醒的设备 - 无论是你的第一个方法或只是当你正在做由工作引发的 AlarmManager - 你需要持有 WakeLock

这里是一个项目,做几乎正是您所描述的 AlarmManager ,减去检查电池电量,但使用 WakefulIntentService ,以确保设备保持清醒。如果你不能得到这个code运行,直到电池关闭,加入 CW-谷歌Android集团并报告您的发现,我带你去看看吧。

I am doing a Battery Consuming research on the Android phone. I want to run a Battery Check every 10 min till the battery totally dies. I have been having problems to make it work.

At my first try, I use a timer in a service class, and schedule the battery check every 10 mins. But soon I found that the service got paused when the screen goes off.

Then I try to use AlarmService, I use a alarm call to wake my service every 10 mins and to check the battery level and save the data to a file on the sdcard. It works with the screen off. However, I only got data of 9 hours...it seems that the AlarmService stop at some point after several hours. I don't know why it is like this, has the system killed it for memory issues?

So my question is, has anyone written some service to run (like forever) in the background before? How do you do it and I'd appreciate very much for a sample code?

I am currently reading some posts saying that there's a partial wake lock I can use to keep the service alive.. is this the correct way to do it?

Thanks a lot and I hope I can get some useful answers here.

解决方案

At my first try, I use a timer in a service class, and schedule the battery check every 10 mins. But soon I found that the service got paused when the screen goes off.

You probably did not hold a WakeLock, so the device fell asleep.

it seems that the AlarmService stop at some point after several hours

I rather doubt it.

So my question is, has anyone written some service to run (like forever) in the background before?

It is not possible to create a service that will run forever. It should be possible to create a scheduled task via AlarmManager that will be invoked "forever".

I am currently reading some posts saying that there's a partial wake lock I can use to keep the service alive.. is this the correct way to do it?

I'm not sure what "it" is. But, if you want to keep the device awake -- whether for your first approach or just while you are doing work triggered by an AlarmManager -- you need to hold a WakeLock.

Here is a project that does almost exactly what you describe for the AlarmManager, minus checking the battery level, but using a WakefulIntentService to ensure the device stays awake. If you cannot get this code to run until the battery shuts down, join the cw-android Google Group and report your findings, and I'll take a look at it.

这篇关于在后台运行一个服务永远..?安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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