我试图用日期和时间设置闹钟。一切都很好,但在我设置闹钟之后,我想要的通知出现在我之前设置的时间(分钟)之前。
* ViewNote.java:
private DatePicker datePicker;
private TimePicker timePicker;
private Calendar dateTime;
private final static int REMINDER_RQS_CODE = 1;
...
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_view_note, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.set_reminder:
datePicker = (DatePicker) findViewById(R.id.datePicker);
timePicker = (TimePicker) findViewById(R.id.timePicker);
Calendar now = Calendar.getInstance();
if(datePicker != null)
{
datePicker.init(Calendar.YEAR,
Calendar.MONTH,
Calendar.DAY_OF_MONTH,
null);
}
if(timePicker != null)
{
timePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(now.get(Calendar.MINUTE));
}
AlertDialog.Builder setDateDialog = new AlertDialog.Builder(this)
.setView(R.layout.date_picker_dialog)
.setTitle("Pick date")
.setIcon(R.drawable.context_menu)
.setPositiveButton("Next", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dateTime = Calendar.getInstance();
if(datePicker != null)
{
dateTime.set(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth());
}
AlertDialog.Builder setTimeDialog = new AlertDialog.Builder(ViewNote.this)
.setView(R.layout.time_picker_dialog)
.setTitle("Pick time")
.setIcon(R.drawable.context_menu)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dateTime = Calendar.getInstance();
if(timePicker != null)
{
dateTime.set(timePicker.getCurrentHour(),
timePicker.getCurrentMinute(),
00);
}
setReminder(dateTime);
}
});
setTimeDialog.show();
}
});
setDateDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
private void setReminder(Calendar dateTime)
{
Intent intent = new Intent(ViewNote.this, RemainderReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ViewNote.this, REMINDER_RQS_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, dateTime.getTimeInMillis(), pendingIntent);
}
* date_picker_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/datePicker"
android:layout_gravity="center_horizontal" />
</LinearLayout>
* time_picker_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timePicker"
android:layout_gravity="center_horizontal" />
</LinearLayout>
* ReminderReceiver.java:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class RemainderReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/*Intent intent1 = new Intent(context, MyReminderService.class);
context.startService(intent1);*/
Log.i("ReminderReceiver", "onReceive method called");
try
{
Utils.generateNotification(context);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
* MyReminderService.java:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
public class MyReminderService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
* AndroidManifest.xml:
...
...
<uses-permission android:name="android.permission.WAKE_LOCK" />
...
...
<service android:name=".MyReminderService"
android:enabled="true" />
<receiver android:name=".RemainderReceiver"/>
</application>
</manifest>
答案 0 :(得分:0)
对我来说,似乎你在onClick的“PositiveButton”中调用了Calendar的错误方法。你打电话给:
dateTime.set(
timePicker.getCurrentHour(),
timePicker.getCurrentMinute(),
00);
它没有设置小时,分钟和毫秒。相反,这种java.util.Calendar方法被调用,导致无意义的值:
public final void set(int year,
int month,
int date)
见这里:https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#set%28int,%20int,%20int%29
您可以通过将TimePicker的时间转换为毫秒来修复此问题,并将其添加到“dateTime”的时间,如下所示。
dateTime = Calendar.getInstance();
if(timePicker != null)
{
dateTime.setTimeInMillis(
dateTime.getTimeInMillis()
+ (timePicker.getCurrentHour() * 360000)
+ (timePicker.getCurrentMinute() * 60000));
}
setReminder(dateTime);
当然1小时是60分钟,360秒是360000毫秒,1分钟是60秒,是60000毫秒。