Android 立即触发警报
Android alarm fired instantly
public class setAlarm extends AppCompatActivity {
final Calendar setat = Calendar.getInstance();
Calendar[] calendar = new Calendar[8];
public void dimensions(){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * 0.62), (int) (height * 0.4));
}
public void clock(){
final TextView ora = (TextView)findViewById(R.id.clock);
findViewById(R.id.clock).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog;
timePickerDialog = new TimePickerDialog(setAlarm.this, new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute){
ora.setText(selectedHour + ":" + selectedMinute);
setat.set(Calendar.HOUR_OF_DAY, selectedHour);
setat.set(Calendar.MINUTE, selectedMinute);
}
}, hour, minute, true);
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
});
}
public void alarm(){
clock();
findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox[] boxes = new CheckBox[8];
boxes[0] = (CheckBox)findViewById(R.id.check0);
boxes[1] = (CheckBox)findViewById(R.id.check1);
boxes[2] = (CheckBox)findViewById(R.id.check2);
boxes[3] = (CheckBox)findViewById(R.id.check3);
boxes[4] = (CheckBox)findViewById(R.id.check4);
boxes[5] = (CheckBox)findViewById(R.id.check5);
boxes[6] = (CheckBox)findViewById(R.id.check6);
if(boxes[0].isChecked()) {setAlarm(1);}
if(boxes[1].isChecked()) {setAlarm(2);}
if(boxes[2].isChecked()) {setAlarm(3);}
if(boxes[3].isChecked()) {setAlarm(4);}
if(boxes[4].isChecked()) {setAlarm(5);}
if(boxes[5].isChecked()) {setAlarm(6);}
if(boxes[6].isChecked()) {setAlarm(7);}
Toast.makeText(setAlarm.this, "Alarm has been set!", Toast.LENGTH_SHORT).show();
//finish();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
dimensions();
alarm();
}
public void setAlarm(int day){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, setat.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, setat.get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, 0);
long time = calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
Log.d("day", String.valueOf(day));
Log.d("time", String.valueOf(setat.get(Calendar.HOUR_OF_DAY)) + ":" + String.valueOf(setat.get(Calendar.MINUTE)));
Log.d("actual", String.valueOf(Calendar.getInstance().get(Calendar.MINUTE)));
Intent intent = new Intent(getApplicationContext(), NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
我想在指定的时间和分钟设置闹钟,它应该每周在选定的日子重复该闹钟。问题是,在确认警报后,它会立即触发或不会在选定的时间触发。我以前搜索过这个问题,但找不到对我有用的东西。
如果警报立即响起,则问题可能是时间过去了,而不是未来。您应该尝试确保(在您的 alarm
方法中)calendar.getTimeInMillis()
大于 System.currentTimeMillis()
,如果不是,则适当调整日历实例。
public class setAlarm extends AppCompatActivity {
final Calendar setat = Calendar.getInstance();
Calendar[] calendar = new Calendar[8];
public void dimensions(){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * 0.62), (int) (height * 0.4));
}
public void clock(){
final TextView ora = (TextView)findViewById(R.id.clock);
findViewById(R.id.clock).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog;
timePickerDialog = new TimePickerDialog(setAlarm.this, new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute){
ora.setText(selectedHour + ":" + selectedMinute);
setat.set(Calendar.HOUR_OF_DAY, selectedHour);
setat.set(Calendar.MINUTE, selectedMinute);
}
}, hour, minute, true);
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
});
}
public void alarm(){
clock();
findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox[] boxes = new CheckBox[8];
boxes[0] = (CheckBox)findViewById(R.id.check0);
boxes[1] = (CheckBox)findViewById(R.id.check1);
boxes[2] = (CheckBox)findViewById(R.id.check2);
boxes[3] = (CheckBox)findViewById(R.id.check3);
boxes[4] = (CheckBox)findViewById(R.id.check4);
boxes[5] = (CheckBox)findViewById(R.id.check5);
boxes[6] = (CheckBox)findViewById(R.id.check6);
if(boxes[0].isChecked()) {setAlarm(1);}
if(boxes[1].isChecked()) {setAlarm(2);}
if(boxes[2].isChecked()) {setAlarm(3);}
if(boxes[3].isChecked()) {setAlarm(4);}
if(boxes[4].isChecked()) {setAlarm(5);}
if(boxes[5].isChecked()) {setAlarm(6);}
if(boxes[6].isChecked()) {setAlarm(7);}
Toast.makeText(setAlarm.this, "Alarm has been set!", Toast.LENGTH_SHORT).show();
//finish();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
dimensions();
alarm();
}
public void setAlarm(int day){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, setat.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, setat.get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, 0);
long time = calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
Log.d("day", String.valueOf(day));
Log.d("time", String.valueOf(setat.get(Calendar.HOUR_OF_DAY)) + ":" + String.valueOf(setat.get(Calendar.MINUTE)));
Log.d("actual", String.valueOf(Calendar.getInstance().get(Calendar.MINUTE)));
Intent intent = new Intent(getApplicationContext(), NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
我想在指定的时间和分钟设置闹钟,它应该每周在选定的日子重复该闹钟。问题是,在确认警报后,它会立即触发或不会在选定的时间触发。我以前搜索过这个问题,但找不到对我有用的东西。
如果警报立即响起,则问题可能是时间过去了,而不是未来。您应该尝试确保(在您的 alarm
方法中)calendar.getTimeInMillis()
大于 System.currentTimeMillis()
,如果不是,则适当调整日历实例。