保持我的应用程序还活着 - Android电子 [英] Keep My App alive - Android

查看:113
本文介绍了保持我的应用程序还活着 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使一个应用程序在Android 2.2具有运行屏幕是否开启或关闭。

I'm trying to make an App on Android 2.2 that has to run whether the screen is on or off.

我试图通过服务实现它,但是当我的手机关闭屏幕,工作服务stopps,我不明白为什么。

I tried realizing it via a Service, but when my phone turns the screen off the service stopps working and I don't understand why.

在我的应用程序使用

startService(new Intent(this, MyService.class));

当用户presses一个按钮

when the User presses a button

Notification notification = new Notification();
startForeground(1, notification);

在服务类的OnCreate法

in the onCreate-Method of the Service class

(我想它在onStart()方法中,太)

(I tried it within the onStart() Method, too)

(我也找不到什么startForeground()的id字段预计,所以我用了1)

(I also could not find out what the id-field of startForeground() expects so I used the 1)

所以我知道它是否正在运行或不然后服务应该启动手机的无限震荡格局。
但是,当我关闭屏幕,手机立即停止振动

The service then should start an infinite vibration pattern of the phone so I know whether it is running or not. But when I turn off the screen, the phone stops vibration immediately

请帮助我。我不知道我怎么能解决这个问题(和谷歌是不是一个很大的帮助)

Please help me. I don't know how I can fix that (and google was not a big help)

真诚
ZED

Sincerely zed

推荐答案

Android设备进入睡眠模式空闲时(例如,​​当屏幕关闭),以节省电池。

Android devices go to a sleep mode when idle (e.g. when the screen is off), in order to conserve the battery.

要保持你的服务运行,您需要将收购一个激活锁定。有很多教程如何使用它,就像这个

To keep your service running you need to aquire a WakeLock. There are plenty of tutorials how to use it, like this one.

另外,具有运行所有的时间服务会耗尽你的电池。同时绝对保证释放的时候不需要激活锁定,否则你的手机永远是清醒的。

Note that having a service running all the time will drain your battery. Also make absolutely sure to release the wakelock when not needed, otherwise you're phone will always be awake.

这篇关于保持我的应用程序还活着 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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