屏幕锁定时服务暂停
出于测试目的,我制作了一项会发出蜂鸣声的服务 每 1 分钟一次。 (还没有客户端-服务器接口)。正常时会发出蜂鸣声 屏幕打开,但当它进入睡眠状态时,蜂鸣声停止。
我正在制作一个必须定期轮询服务器的应用程序 为了某事。
为此,我正在尝试创建一项持续不断的服务 在后台运行,每 1 分钟轮询一次服务器,然后基于 根据服务器的回复,它将生成一个任务栏通知。
我有一个带有两个按钮的测试活动,一个用于启动,另一个用于 停止服务。一个名为 S_PS_PollService 的服务类
“启动活动”按钮的 setOnClickListener 包含:
Thread pollServiceThread = new Thread() {
public void run() {
startService(new Intent(MM_MainMenu.this,
S_PS_PollService.class));
}
};
pollServiceThread.start();
“停止活动”按钮仅具有:
stopService(new Intent(MM_MainMenu.this, S_PS_PollService.class));
以下是 S_PS_PollService 类中的方法:
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:
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() 方法,使 蜂鸣声并设置新闹钟。但它仅在屏幕打开时才起作用。
我已经尝试过 https://github.com/commonsguy/cwac-wakeful 但没有 得到它。对android来说相对较新......
请帮助我,我非常绝望:)谢谢,!
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.
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.
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
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));
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:
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();
}
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.
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, !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你必须使用 AlarmManager,stackoverflow 上有很多帖子。
You have to use the AlarmManager, there are plenty of posts here on stackoverflow.
您希望按照代码的建议获取部分唤醒锁(只要设备进入睡眠状态,CPU 就保持运行)。
问题是您可能在开始时重写释放唤醒锁。一旦你的服务完成运行,你想在 onDestroy 中释放你的wakeLock。
You want to acquire a partial wake lock (leaving the CPU running whenever sleep is entered on the device) as suggested by your code.
The issue is your presumably overriden on start releases the wake lock. You want to release your wakeLock in onDestroy .. once your service is finished running.
这终于对我有用了。
从 https://github.com/commonsguy/cwac-wakeful 下载 CWAC-WakefulIntentService.jar
在您的项目中添加一个类,
现在在您想要重复循环并唤醒设备的代码中添加以下行
WakefulIntentService.sendWakefulWork(this, S_WS_WakeService.class);
This finally worked for me.
Download the CWAC-WakefulIntentService.jar from https://github.com/commonsguy/cwac-wakeful
add a class in your project
now add the following line in your code where ever you want to repeat the loop and wake the device up
WakefulIntentService.sendWakefulWork(this, S_WS_WakeService.class);