服务会暂停在锁屏 [英] Service pauses on screen lock
问题描述
有关测试的目的,我已经取得了蜂鸣服务 每1分钟。 (无客户端 - 服务器接口还)。它会发出蜂鸣声好时 在屏幕上,但是当它进入睡眠状态的蜂鸣声停止。
For testing purposes i have made a service that beeps every 1 minute. (No client-server interface yet). It beeps okay when the screen in on, but when it goes to sleep the beeping stops.
我在做,有定期轮询服务器的应用程序 什么东西。
I am making an application that has to periodically poll the a server for something.
对于这一点,我想创建一个会不断地将服务 在后台运行,轮询服务器,每1分钟,然后根据 从服务器的应答应当产生一个任务栏通知。
For this, I am trying to create a service that'll constantly be running in the background, poll the server every 1 min and then based on the reply from server it shall generate a task bar notification.
我有一个测试活动有两个按钮,1开始,另一个用于 停止该服务。并命名为S_PS_PollService一个服务类
I have a test activity with two buttons, 1 to start and the other to stop the service. And one service class named S_PS_PollService
的启动活动按钮setOnClickListener包括:
The setOnClickListener of 'Start Activity' button contains:
Thread pollServiceThread = new Thread() {
public void run() {
startService(new Intent(MM_MainMenu.this,
S_PS_PollService.class));
}
};
pollServiceThread.start();
在停止活动按钮,只需有:
The 'Stop Activity' button simply has:
stopService(new Intent(MM_MainMenu.this, S_PS_PollService.class));
以下是从S_PS_PollService类的方法:
Following are the methods from S_PS_PollService class:
public void onCreate() {
pollSound = MediaPlayer.create(this, R.raw.chirp);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(this, S_PS_PollService.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
// for wake lock
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag")
// for calendar
calendar = Calendar.getInstance();
}
ONSTART:
Onstart:
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
wl.acquire();
pollSound.start();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MILLISECOND, 60000);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
wl.release();
}
每当警报序幕ONSTART()方法被执行,使得 发出蜂鸣声和设置新的警报。但它仅只要屏幕上。
Whenever the alarm kicks off onStart() method is executed, making the beep and setting new alarm. But it works only as long as screen is on.
我曾尝试 https://github.com/commonsguy/cwac-wakeful 但didnt 得到它。相对较新到Android ...
I have tried for https://github.com/commonsguy/cwac-wakeful but didnt get it. Relatively new to android ...
请帮我而言,我很绝望:)谢谢!
Please help me, im very desperate :) Thanks, !
推荐答案
这终于为我工作。 从 https://github.com/commonsguy/cwac-wakeful 下载CWAC,WakefulIntentService.jar
This finally worked for me. Download the CWAC-WakefulIntentService.jar from https://github.com/commonsguy/cwac-wakeful
在你的项目中添加一个类
add a class in your project
import com.commonsware.cwac.wakeful.WakefulIntentService;
public class WakeService extends WakefulIntentService {
public WakeService(String name) {
super(name);
}
@Override
protected void doWakefulWork(Intent intent) {
}
}
现在在code以下行添加在任何你想重复循环,唤醒设备
now add the following line in your code where ever you want to repeat the loop and wake the device up
WakefulIntentService.sendWakefulWork(这一点,S_WS_WakeService.class);
这篇关于服务会暂停在锁屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!