问题描述
您好计算器
我试图开发可运行在特定的时间间隔某些任务的Android应用程序,我使用 AlarmManager
做任务时,code片断如下,
Hello stackoverflow
I'm trying to develop an android application that can run some task at specific time interval, I'm using AlarmManager
to do the task, the code snippet is as follows,
if (radioBtnChecked)
{
MyActivity.this.alarmMgr = (AlarmManager) MyActivity.this.getSystemService(Context.ALARM_SERVICE);
Intent serviceIntent = new Intent(MyActivity.this, MyService.class);
MyActivity.this.pi = PendingIntent.getService(MyActivity.this, 0, serviceIntent, 0);
MyActivity.this.alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, pi);
}//End of if condition
和 MyService.java
public class MyService extends Service
{
@Override
public IBinder onBind(Intent intent)
{
return null;
}//End of onBind method
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Toast.makeText(getApplicationContext(),"Service started", Toast.LENGTH_SHORT).show();
}
}
问题是服务启动
信息将被显示在第一次当我点击单选按钮,但我想在服务启动
消息后显示。请人帮我解决这个问题,请分享你的知识,这样我可以纠正我的错误。
Problem is the Service started
message will be displayed for the very first time when I click the radio button, but I want the Service started
message to be displayed after the 10 seconds
. Please someone help me to solve this problem, please share your knowledge so that I can correct my mistakes.
先谢谢了。
推荐答案
设置AlarmManager是这样的:
Set AlarmManager like this:
private static final int REPEAT_TIME_IN_SECONDS = 60; //repeat every 60 seconds
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
REPEAT_TIME_IN_SECONDS * 1000, pendingIntent);
修改 AlarmManager.RTC
到 AlarmManager.RTC_WAKEUP
如果u想,当它醒来电话
熄灭。更多关于AlarmManager
Change AlarmManager.RTC
to AlarmManager.RTC_WAKEUP
if u want to wake up phone when it goes off. More about AlarmManager here
这两个参数也意味着你的报警时间将是的这是UTC时间。
Those two parameters also means that your alarm time will be System.currentTimeMilis()
which is time in UTC.
编辑:
使用您的解决方案 AlarmManager.ELAPSED_REALTIME
该措施的时间,因为设备启动,包括睡眠。这意味着,如果你想运行10秒后此code,然后要重复它,您的设备运行的还不止这些,的PendingIntent将过去发生开机后触发立即使用,因为10秒。
Your solution using AlarmManager.ELAPSED_REALTIME
which measure time since device boot including sleep. It means that if you want run this code after 10 seconds and then want to repeat it and your device is running for more than that, PendingIntent will be triggered immediately because 10 seconds after boot occurs in the past.
编辑2:
如果ü要运行code只是一次10秒试试这个后:
If u want to run code just once after 10 seconds try this:
private static final int START_AFTER_SECONDS = 10;
...
if (radioBtnChecked)
{
Runnable mRunnable;
Handler mHandler = new Handler();
mRunnable = new Runnable() {
@Override
public void run() {
Intent serviceIntent = new Intent(MyActivity.this, MyService.class);
MyActivity.this.startService(serviceIntent);
}
};
mHandler.postDelayed(mRunnable, START_AFTER_SECONDS * 1000);
}
这篇关于如何使用AlarmManager运行在指定的时间间隔有些任务在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!