Android Dev:TimerTask和手机睡眠异常吗? [英] Android Dev: TimerTask and phone sleep weirdness maybe?

查看:89
本文介绍了Android Dev:TimerTask和手机睡眠异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了我的第二个android应用程序,该应用程序根据特定的时间表(仅现在的日期和时间)切换音频配置文件....如果我通过eclipse使用模拟器运行代码,则代码可以正常工作...但是我最近导出了apk并将其安装在我的At& t Samsung Galaxy S2上,并添加了与模拟器相同的配置文件时间表,但是由于某些奇怪的原因,timertask在第一次之后就没有执行...

i have coded my 2nd android application that switches audio profiles based on a certain schedule (date and time only as of now)....the code is working without any issues if i run the code using the emulator via eclipse...however i recently exported the apk and installed it on my At&t Samsung Galaxy S2 and added the same profile schedule as the emulator, but for some odd reason the timertask is not executing after the 1st time...

例如:这是我的手机和模拟器中的配置文件时间表:

For Example: Here is the Profile Schedule from my phone as well as the emulator:

8AM-5:30 PM-工作

8AM - 5:30PM -- Work

5:30 PM-10PM-主页

5:30PM - 10PM -- Home

晚上10点-上午8点-睡眠

10PM - 8AM -- Sleep

一旦安排好上述操作,然后在主要活动上按激活按钮..后台服务会从一个配置文件切换到下一个配置文件,直到被停用...

once the above is scheduled and i press the activate button on the main activity..a background service runs switching from one profile to the next until it is de-activated...

我发现模拟器和手机之间的唯一区别是,模拟器屏幕永远不会关闭...所以我猜想模拟器永远不会进入睡眠状态?相反,除了锁定屏幕外,手机的屏幕在闲置约30秒钟后关闭...此外,我打开了手机上的USB调试功能,并将其直接插入计算机,从外观上看,代码是执行,将一个配置文件切换到手机上的下一个配置文件,没有任何问题...(请注意,在进行此测试时,我在配置文件时间表中使用了更短的时间间隔)...其他详细信息:我还添加了startForeground()在我的服务代码中,并确保该服务不会被终止...

the only difference i have noticed between the emulator and my phone is that, the emulator screen never shuts off...so im guessing that the emulator doesnt ever go on sleep? whereas, my phone's screen turns off after about 30 seconds of inactivity, in addition to the lock screen...furthermore, i turned on USB debugging on the phone and plugged it directly into the computer and from what it seemed like, the code was executing, switching one profile to the next, on my phone without any issues...(Note that, when i was doing this testing, i used smaller time intervals in the profile schedules)...Additional details: i have also added startForeground() in my service code and ensured that the service is not getting terminated...

有人对我失踪了什么有什么想法,或者我可以做些什么来解决这个奇怪的问题?...我在发布此消息之前做了一些研究,发现了一些建议使用的帖子: Android的PowerManager ...

does anyone have any idea on what im missing, or what i can do to get around this bizarre problem?...i did do some research before posting this, and found out few posts that recommended using: Android's PowerManager...

感谢您的输入

推荐答案

这没什么奇怪的:如果电话处于睡眠状态,则CPU处于关闭状态,并且您无法执行代码.不要在Android上使用 TimerTask ,而是使用 AlarmManager 安排重复执行的任务.它在内核中实现,可以在必要时唤醒手机(这可能会对电池寿命产生负面影响,因此请谨慎使用).同时查看WakeLock.

Nothing weird about this: if the phone is asleep, the CPU is off and you cannot execute code. Don't use TimerTask on Android, use AlarmManager to schedule recurring tasks. It's implemented in the kernel and can wake up the phone if necessary (that might have a negative impact on battery life, so use with caution). Also look into WakeLock's.

http://developer.android.com/reference/android/app/AlarmManager.html

这篇关于Android Dev:TimerTask和手机睡眠异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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