我想制作一个在特定时间调用意图的程序,早餐8点,午餐1点,每天6点半吃晚餐。
我该怎么做?这是因为如果我使用AlarmManager.INTERVAL_DAY / 3
,则无法给出具体时间。
或者我可以使用其他课程吗?
这是我的代码:
package com.makansehat;
import java.util.Calendar;
import android.app.*;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
// import android.widget.EditText;
import android.widget.Toast;
/**
* @author Prabu
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startAlert(View view) {
Calendar skr = Calendar.getInstance();
pagi.set(Calendar.HOUR_OF_DAY, 8);
pagi.set(Calendar.MINUTE, 53);
Calendar siang = Calendar.getInstance();
siang.set(Calendar.HOUR_OF_DAY, 8);
siang.set(Calendar.MINUTE, 55);
Calendar malam = Calendar.getInstance();
malam.set(Calendar.HOUR_OF_DAY, 8);
malam.set(Calendar.MINUTE, 59);
// EditText text = (EditText) findViewById(R.id.time);
// int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, pagi.getTimeInMillis(), siang.getTimeInMillis(), pi);
}
}
答案 0 :(得分:4)
您应该创建3个警报:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, pagi.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, siang.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, malam.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
答案 1 :(得分:1)
在这里,我附上Code以唤醒设备,准时在上午8:30准时发出警报,此后每20分钟发出一次警报:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
我希望你能理解我能说什么只是设置你的第一次calander然后在手动使用for循环后给出间隔例如: 这里的时间表是触发警报的第二,第三等时间间隔小数
for(int i=0;i<timelist.size();i++){
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
timelist[i], alarmIntent);
}
或完成任务的第二种方式拿三个警报管理器并设定你的时间。
希望它能帮到你......