开发者

Android alarm manager

Hi i've been researching the alarm manager in android and was wondering if there how to set a specific time for an alarm (or could use notification manager)to go off at a specific time for example 12pm tomorrow . The code below sets the alarm for 5 seconds from now so to 开发者_如何学Cset it for something like 12 pm could you do something like 12:00:00 or something?

 Intent intent = new Intent(this, OnetimeAlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), sender);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();


Java's Date and Time libraries are a huge hassle, but this should give you an idea:

// the time now
Calendar calendar = Calendar.newInstance();

// noon tomorrow
calendar.add(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();


Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());

this should work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜