为android构建我自己的闹钟应用程序并且在通知方面遇到了一些麻烦。我想从通知中设置午睡时间,所以我用行动构建通知
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addNotification();
}
private void addNotification() {
SimpleDateFormat sdfHour = new SimpleDateFormat("HH");
SimpleDateFormat sdfMinute = new SimpleDateFormat("mm");
int timeHour = Integer.parseInt(sdfHour.format(getInstance().getTime()));
int timeMinute = Integer.parseInt(sdfMinute.format(getInstance().getTime()));
Intent a15 = new Intent(AlarmClock.ACTION_SET_ALARM);
a15.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap");
a15.putExtra(AlarmClock.EXTRA_HOUR, timeHour);
a15.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute+15);
a15.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
PendingIntent nap15 = PendingIntent.getActivity(this,1,a15,PendingIntent.FLAG_UPDATE_CURRENT);
Intent a30 = new Intent(AlarmClock.ACTION_SET_ALARM);
a30.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap");
a30.putExtra(AlarmClock.EXTRA_HOUR, timeHour);
a30.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute+30);
a30.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
PendingIntent nap30 = PendingIntent.getActivity(this,2,a30,PendingIntent.FLAG_UPDATE_CURRENT);
Intent a45 = new Intent(AlarmClock.ACTION_SET_ALARM);
a45.putExtra(AlarmClock.EXTRA_MESSAGE, "Nap");
a45.putExtra(AlarmClock.EXTRA_HOUR, timeHour);
a45.putExtra(AlarmClock.EXTRA_MINUTES, timeMinute + 3);
a45.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
PendingIntent nap45 = PendingIntent.getActivity(this,3,a45,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_alarm_white_24dp)
.setContentTitle("Take a nap.")
.setContentText("Select nap time.")
.addAction(R.drawable.ic_alarm_add_white_24dp, "15min",nap15)
.addAction(R.drawable.ic_alarm_add_white_24dp, "30min",nap30)
.addAction(R.drawable.ic_alarm_add_white_24dp, "45min",nap45);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, mBuilder.build());
}
但是当我选择小睡时间为 15 分钟时,闹钟设置为从我创建通知而不是当前系统时间开始的 15 分钟。例如,如果我在下午 5 点运行应用程序,并且在 10 分钟(下午 5:10)之后我从通知中选择操作,它设置为下午 5 点 + 15 点而不是下午 5:10 点 + 15 点的警报。所以当我点击时可以获得当前时间通知中的操作?我需要用什么?