Intent intent =
new
Intent(MainActivity.
this
, AlarmReceiver.
class
);
PendingIntent sender = PendingIntent.getBroadcast(MainActivity.
this
,
0
, intent,
0
);
long
firstTime = SystemClock.elapsedRealtime();
long
systemTime = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.setTimeZone(TimeZone.getTimeZone(
"GMT+8"
));
calendar.set(Calendar.MINUTE, mMinute);
calendar.set(Calendar.HOUR_OF_DAY, mHour);
calendar.set(Calendar.SECOND,
0
);
calendar.set(Calendar.MILLISECOND,
0
);
long
selectTime = calendar.getTimeInMillis();
if
(systemTime > selectTime) {
Toast.makeText(MainActivity.
this
,
"设置的时间小于当前时间"
, Toast.LENGTH_SHORT).show();
calendar.add(Calendar.DAY_OF_MONTH,
1
);
selectTime = calendar.getTimeInMillis();
}
long
time = selectTime - systemTime;
firstTime += time;
AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, DAY, sender);
Log.i(TAG,
"time ==== "
+ time +
", selectTime ===== "
+ selectTime +
", systemTime ==== "
+ systemTime +
", firstTime === "
+ firstTime);
Toast.makeText(MainActivity.
this
,
"设置重复闹铃成功! "
, Toast.LENGTH_LONG).show();