问题描述
在这里,我将使用警报服务每30分钟一次播放警报.现在我已经将它设置为每10秒钟从每个开始时播放它.
这是代码:
@Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.settings_layout); Intent myIntent = new Intent(SettingsActivity.this, MyAlarmService.class); pendingIntent = PendingIntent.getService(SettingsActivity.this, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent); }现在问题是,我想从12:30开始闹钟从时间应用程序开始,它应该在eftt 30分钟内反复播放.就像1:00,1:30,2:00. . . ETC
那么我在代码中要做什么更改?
推荐答案
在特定时间开始服务. 看到这篇文章可能有用:
其他推荐答案
当警报触发时,然后设置下一个闹钟时间并继续执行此操作,直到您不再想要报警.
您不需要服务来执行这么简单的任务. AlarmManager超出了处理此操作.
其他推荐答案
并使用第一次 -
问题描述
Here i am going to use the alarm service to play the alarm at every 30 minutes. Right now i have set it to play it at every 10 second from the Every start.
Here is the Code:
@Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.settings_layout); Intent myIntent = new Intent(SettingsActivity.this, MyAlarmService.class); pendingIntent = PendingIntent.getService(SettingsActivity.this, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent); }
Now the Problem is, I want to start the alarm from the 12:30 not from the time application start and it should repeatedly play at evert 30 minutes. like 1:00, 1:30, 2:00 . . . etc
So what changes i have to do in my code ?
推荐答案
To start a service at specific time. see this post maybe helpful:
Using Alarmmanager to start a service at specific time
其他推荐答案
Set your initial alarm time for 12:30 using the Set method.
When the alarm fires, then set up your next alarm time and keep doing that until you don't want the alarm any more.
You don't need a service to do such a simple task. AlarmManager is more than capable of handling this.
其他推荐答案
And Use first time -