获取点击通知操作的当前时间

时间:2017-03-27 19:39:12

标签: android android-intent time android-pendingintent setalarmclock

为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点。所以当我点击时可以获得当前时间通知中的行动?我需要用什么?

1 个答案:

答案 0 :(得分:0)

您可以从SimpleDateFormat获取当前时间(以小时和分钟为单位):

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