英文:
Set both Date and Time for AlarmManager in Android Studio
问题
以下是您提供的代码的翻译部分:
我想为闹钟管理器设置特定的日期和时间以触发通知。我找到了一个有用的代码,可以显示日期片段和时间片段。我的问题是,当我设置闹钟时,通知没有在指定的日期和时间弹出,而是立即弹出。请帮助我,各位前辈们。
这是我的代码。
用于打开时间选择器
mOpenTimePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar date;
final Calendar currentDate = Calendar.getInstance();
date = Calendar.getInstance();
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date.set(year, monthOfYear, dayOfMonth);
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
Log.v(TAG, "所选时间:" + date.getTime());
}
}, currentDate.get(Calendar.HOUR_OF_DAY), currentDate.get(Calendar.MINUTE), false).show();
}
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTextView.setText(dateFormat.format(date.getTime()));
startAlarm(date);
}
});
启动闹钟
private void startAlarm(Calendar c) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// 传递数据
String title = mTitle.getText().toString();
String message = mMessage.getText().toString();
id = (int) System.currentTimeMillis();
Intent intent = new Intent(this, AlertReceiver.class);
intent.putExtra("Title", title);
intent.putExtra("Message", message);
intent.putExtra("ID", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
请注意,我已经将代码中的HTML实体字符 "
替换为双引号 "
以更正翻译。如果您需要更多帮助或有其他问题,请随时提出。
英文:
I want to set specific Date and time to fire notification for Alarm Manager. I found a useful code that can show both Date Fragment and Time Fragment.My problem is when I set the alarm,the notification didn't pop up at the specified date and time and instead it pop up immediately. Please help me Seniors out there.
Here is my codes.
For open time picker
mOpenTimePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar date;
final Calendar currentDate = Calendar.getInstance();
date = Calendar.getInstance();
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date.set(year, monthOfYear, dayOfMonth);
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
Log.v(TAG, "The choosen one " + date.getTime());
}
}, currentDate.get(Calendar.HOUR_OF_DAY), currentDate.get(Calendar.MINUTE), false).show();
}
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTextView.setText(dateFormat.format(date.getTime()));
startAlarm(date);
}
});
For Start Alarm
private void startAlarm(Calendar c) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//Passing data
String title = mTitle.getText().toString();
String message = mMessage.getText().toString();
id = (int) System.currentTimeMillis();
Intent intent = new Intent(this, AlertReceiver.class);
intent.putExtra("Title", title);
intent.putExtra("Message", message);
intent.putExtra("ID", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
答案1
得分: 0
你在onClick(View v)
中设置闹钟的代码会立即调用,因为它是同步编写的。可以通过以下方式解决:
mOpenTimePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar date;
final Calendar currentDate = Calendar.getInstance();
date = Calendar.getInstance();
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date.set(year, monthOfYear, dayOfMonth);
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
// 在此设置闹钟
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTextView.setText(dateFormat.format(date.getTime()));
startAlarm(date);
}
}, currentDate.get(Calendar.HOUR_OF_DAY), currentDate.get(Calendar.MINUTE), false).show();
}
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
}
});
这段代码有点混乱,因此可能需要为打开TimePicker
和DatePicker
创建单独的方法。
英文:
Your code for setting Alarm inside onClick(View v)
will call immediately because its written synchronously. It can be solved by :-
mOpenTimePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar date;
final Calendar currentDate = Calendar.getInstance();
date = Calendar.getInstance();
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date.set(year, monthOfYear, dayOfMonth);
new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
// Set the alarm here
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTextView.setText(dateFormat.format(date.getTime()));
startAlarm(date);
}
}, currentDate.get(Calendar.HOUR_OF_DAY), currentDate.get(Calendar.MINUTE), false).show();
}
}, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE)).show();
}
});
This code is bit messy So probably create a separate method for opening TimePicker
and DatePicker
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论