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

Passing Values Details is

Current time is: 16:07 hrs=16 min=10

but

tmemills is coming 1377772800393

I am not able to find where the problem is because I am beginner for Android. please help me

4

4 回答 4

0

我使用下面的代码来创建有点像警报的..希望它有所帮助。`public class AlarmManagerActivity extends Activity { PendingIntent pi; 广播接收器 br; 警报管理器是;最终静态私有 long ONE_SECOND = 1000; 最终静态私有 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)); }}`

于 2013-08-30T11:14:22.320 回答
0

做这个

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

和这个

am.setRepeating(AlarmManager.RTC_WAKEUP,tmemills +    System.currentTimeMillis(),AlarmManager.INTERVAL_DAY, sender);
于 2013-08-29T12:51:42.673 回答
0

If you want the difference between two times you can use this ::

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

the diff will give you the difference in millisecs.

Also the value cal_alarm.getTimeInMillis() will give u the value of the date in milliseconds, it will not give the difference of two dates.

于 2013-08-29T12:07:12.960 回答
0

cal_alarm.getTimeInMillis()为您提供自 1970 年 1 月 1 日午夜以来的毫秒数。这不是你的两个日期之间的差异。

要获得差异,请cal_now.getTimeInMillis()像这样减去:

long tmemills = cal_alarm.getTimeInMillis() - cal_now.getTimeInMillis();
于 2013-08-29T11:18:48.837 回答