将格式时间(00:00)转换为毫秒

时间:2017-08-19 13:21:58

标签: android

我正在开发一个Alarm clock类型的应用程序,该应用程序在用户使用Time picker小部件设置的时间显示通知栏,为此我使用Alarm Manager,如下所示:

    TimePicker tm=(TimePicker)findViewById(R.id.timePicker);
    int hour=tm.getHour();
    int min=tm.getMinute();
    Intent intent = new Intent(this, my_broadcast.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            this.getApplicationContext(),234324243, intent, 0);
        Calendar cSchedStartCal1 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
    int h=cSchedStartCal1.HOUR;
    int m=cSchedStartCal1.MINUTE;
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP,X, pendingIntent);

我想知道 X 的价值,以便当时通知

2 个答案:

答案 0 :(得分:0)

然后尝试这样

Calendar calendar = Calendar.getInstance();
calendar.set(year, month,day, 
         hour, minute, 0);
long startTime = calendar.getTimeInMillis();

此处将年份设置为当前年份,当前月份,当前日期,所选小时和分钟

答案 1 :(得分:0)

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
calendar.setHour(HOUR_OF_DAY, hour);
calendar.setMinute(MINUTE, minute);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

此外,您的应用程序将无法在android marshmallow及更高版本上正常运行。您应该考虑使用AlarmManagersetAlarmClock(),如here

所述