设置Android Studio中AlarmManager的日期和时间。

huangapple go评论114阅读模式
英文:

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();
    }
});

这段代码有点混乱,因此可能需要为打开TimePickerDatePicker创建单独的方法。

英文:

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 .

huangapple
  • 本文由 发表于 2020年8月11日 19:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63357085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定