calendar.getTimeInMillis()未正确设置

时间:2013-08-29 10:43:06

标签: android

Date dat = new Date();

Calendar cal_alarm = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal_alarm.setTime(dat);
cal_alarm.set(Calendar.HOUR_OF_DAY, hrs);// set the alarm time
cal_alarm.set(Calendar.MINUTE, min);
cal_alarm.set(Calendar.SECOND, 0);
cal_alarm.set(Calendar.MILLISECOND, 0);
if (cal_alarm.before(cal_now)) {// if its in the past increment
    cal_alarm.add(Calendar.DATE, 1);
}
Intent intent = new Intent(ctx, AlarmReceiver.class);
// intent.putExtra("Reminder to Take Photo", "Pixitch!");
PendingIntent sender = PendingIntent.getBroadcast(ctx, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
Long tmemills = cal_alarm.getTimeInMillis();

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, tmemills,AlarmManager.INTERVAL_DAY, sender);

传递值详细信息

当前时间是:16:07 小时= 16 分= 10

但是

tmemills即将来临1377772800393

我无法找到问题所在 因为我是Android的初学者。 请帮帮我

4 个答案:

答案 0 :(得分:0)

cal_alarm.getTimeInMillis()给你自1970年1月1日午夜以来的毫秒数。这不是你们两个日期之间的区别。

要获得差异,请按以下方式减去cal_now.getTimeInMillis()

long tmemills = cal_alarm.getTimeInMillis() - cal_now.getTimeInMillis();

答案 1 :(得分:0)

如果你想要两次之间的差异,你可以使用这个::

Date date1 = cal.getTime(); String endTime = "2013/29/08 18:59:59"; SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy/dd/MM HH:mm:ss"); Date dtEndTime = dateFormat.parse(endTime); DecimalFormat formatter = new DecimalFormat("###,###"); long diff = dtEndTime.getTime() - date1.getTime();

差异将为您提供毫秒差异。

值cal_alarm.getTimeInMillis()也会以毫秒为单位给出日期的值,它不会给出两个日期的差异。

答案 2 :(得分:0)

制作本

long tmemills = cal_alarm.getTimeInMillis() - cal_now.getTimeInMillis();

和这个

am.setRepeating(AlarmManager.RTC_WAKEUP,tmemills +    System.currentTimeMillis(),AlarmManager.INTERVAL_DAY, sender);

答案 3 :(得分:0)

我使用以下代码创建有点像警报..希望它有帮助.public class AlarmManagerActivity extends Activity {     PendingIntent pi;     BroadcastReceiver br;     AlarmManager am;     final static private long ONE_SECOND = 1000;     final static private long Five_SECONDS = ONE_SECOND * 5;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    setup();
    findViewById(R.id.tv).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 
                     Five_SECONDS, pi );
        }       });     

}    
@Override
protected void onDestroy() {
       am.cancel(pi);
       unregisterReceiver(br);
       super.onDestroy();
}    
private void setup() {
    br = new BroadcastReceiver() {
           @Override
           public void onReceive(Context c, Intent i) {
                  Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
                  }
           };
    registerReceiver(br, new IntentFilter("abc") );
    pi = PendingIntent.getBroadcast( this, 0, new Intent("abc"),

0);         am =(AlarmManager)(this.getSystemService(Context.ALARM_SERVICE)); }}`