日历的Android值getTimeInMillis()在传递给另一个服务时会被更改

时间:2014-10-17 13:48:42

标签: android android-intent parameters android-calendar

我面临一个奇怪的问题 - 从1个服务传递到另一个服务时,以毫秒为单位的时间会发生变化。

从Service中的AsyncTask对象,这是我用来设置警报的代码:

    protected void onPostExecute(Void result) {
        Log.i("*** SetAlarmsTask", "In onPostExecute...");

        Calendar cal = Calendar.getInstance();
        int hrsPrior = 1;
        int interval = 15;
        boolean repeating = true;
        int[] dates;
        int[] times;
        long endTime;
        long intervalDuration = (1000 * 60 * interval);

        // handle your data
        for (Appointment apt: todaysAptsList) {
            Log.i("*** SetAlarmsTask", "APT DAte - " + apt.getAppointDate() + " Time = " + apt.getAppointTime());

           // DailySetNotifyService.SetAlarm(DailySetNotifyService.this, apt);

            cal = Calendar.getInstance();

            // Parse Date of Appointment
            dates = MainActivity.parseStringToDate(apt.getAppointDate());  
            // Set Calendar Date
            cal.set(Calendar.DAY_OF_MONTH, dates[0]);
            cal.set(Calendar.MONTH, dates[1]-1);
            cal.set(Calendar.YEAR, dates[2]);

            // // Parse Time of Appointment
            times = MainActivity.parseStringToTime(apt.getAppointTime());  
            // Set Calendar Time
            cal.set(Calendar.HOUR_OF_DAY, times[0]);
            cal.set(Calendar.MINUTE, times[1]);
            cal.set(Calendar.SECOND, 0);

            endTime = cal.getTimeInMillis();    // Apt Time
            Log.i("*** SetAlarmsTask", " END Time - " + String.valueOf(endTime) + " Apt Time - " + apt.getAppointTime() );   // END Time - 1413555000396 Apt Time - 10:10

            AlarmManager am = (AlarmManager) DailySetNotifyService.this.getSystemService(Context.ALARM_SERVICE);
            // Pass the endTime to Intent
            PendingIntent pIntent = getPendingIntent(endTime, (int)apt.getAptId(), apt); 
            am.set(AlarmManager.RTC, cal.getTimeInMillis(), pIntent);

            if (repeating) {
                int fromHr = times[0] - hrsPrior;
                int fromMin = times[1];
                Log.i("*** SetAlarmsTask", "Repeating set for Time " + String.valueOf(fromHr) + ":" +
                        String.valueOf(fromMin) + " wITH iNTERVAL : " + interval);  // Repeating set for Time 9:10 wITH iNTERVAL : 15

                // Pass the endTime to Intent
                pIntent = getPendingIntent(endTime, (int) apt.getAptId(), apt);

                cal.set(Calendar.HOUR_OF_DAY, fromHr);
                am.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), intervalDuration, pIntent);
            }
        }

        stopSelf();
        Log.i("*** SetAlarmsTask", "In onPostExecute...FINISHED");
    }

    private PendingIntent getPendingIntent(long endTime, int reqCode, Appointment apt) {
        Intent i = new Intent(DailySetNotifyService.this, NotifyAlarmReceiver.class);
        i.putExtra("Appointment", apt);
        Log.i("*** SetAlarmsTask", " END Time - " + String.valueOf(endTime) );  //  END Time - 1413555000905  i.e 10:10 CORRECT

        // Send the endTime & requestCode to identify the time in Receiver and cancel the alarm
        // if it exceed the appointmentTime
        i.putExtra("endTime", endTime);
        i.putExtra("reqCode", reqCode);
        PendingIntent pIntent = PendingIntent.getBroadcast(DailySetNotifyService.this, reqCode, i, 0);

        return pIntent;
    }

我收到事件代码的接收器NotifyAlarmReceiver是:

    public void onReceive(Context context, Intent intent) {

       Calendar cal = Calendar.getInstance();
       long now = cal.getTimeInMillis(); //System.currentTimeMillis();
       Bundle b = intent.getExtras();
       Appointment apt = (Appointment)b.get("Appointment");
       long end = b.getLong("endTime");
       int reqCode = b.getInt("reqCode");

       Log.i("***NotifyAlarmReceiver", "Now - " + String.valueOf(now) + " END - " + String.valueOf(end) );   // Now - 1413551585985 END - 1413549900410

如上所示,cal实例的日期设置为17/10/2014&时间到10:10。 cal.getTimeInMillis()返回保存在endTime变量中的1413555000396。 endTime传递给getPendingIntent(),最后使用putExtra传递给intent。当意图接收者接收到它时,它将endTime的值显示为1413549900410,即8:45时间。现在的价值是正确的,即09:13。我的endTime是10:10,不应该大于now值。我将nowendTime值进行比较。但是由于这个不正确的值,它现在总是显示大于endTime。

知道为什么endTime变量的值在接收器中会发生变化吗?我相信当我在任何地方使用Calendar.getInstance()并且now也显示正确的结果时,所以它不应该是时区问题。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

我将String.valueOf(endTime);替换为DateFormat.format,并且我在所有地方都获得了适当的endTime值。

嗯..这是否意味着String.valueOf()会更改endTime的值{&1}}由于某种原因。所以我了解到String.valueOf()的使用不适合用于日期和时间。倍转换!!!

相关问题