如何在特定时间开始活动? [英] How to start an activity on a certain time?
本文介绍了如何在特定时间开始活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此问题可能与此有关和这个问题,但与我想要的那些问题不同在特定时间启动它们(例如,2014年3月15日11:12:13).
This question might be related to this and this question, but unlike those question I want to start them on a specific time, (say 11:12:13 on 15/03/2014).
我实际上正在研究一个在给定时间发送消息的项目(SMS).
I am actually working on a project (SMS) that sends messages on a given time.
推荐答案
尝试以下代码,我对其进行了测试:
try this code, I tested it:
Calendar calendar = Calendar.getInstance();
-
并通过以下一种方式设置时间:
And set time by one of this ways:
calendar.set(year, month, day, hourOfDay, minute); // be careful month start form "0" so for "May" put "4"
-
或:
or:
calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute);
Intent intent = new Intent(MainActivity.this, NextActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT); ((AlarmManager) getSystemService(ALARM_SERVICE)).set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
注意:如果需要计时器,可以在上面的代码中将
alendar.getTimeInMillis()
替换为System.currentTimeMillis() + x
. (x
是您要等待的毫秒数int
)Note: If you need a timer, you can replace
alendar.getTimeInMillis()
toSystem.currentTimeMillis() + x
in above code. (x
is anint
of milliseconds that you want make wait)这篇关于如何在特定时间开始活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文