由网友(相惜.莫相忘)分享简介:我在想,如果有可能(如果它是如何)启动我的应用程序在特定的时间,像一个闹钟其在特定时间熄灭。比方说,我想我的应用程序启动在早上8,是feasable?I was wondering if it's possible (and if it is how) to start up my app at a specifi...
我在想,如果有可能(如果它是如何)启动我的应用程序在特定的时间,像一个闹钟其在特定时间熄灭。 比方说,我想我的应用程序启动在早上8,是feasable?
I was wondering if it's possible (and if it is how) to start up my app at a specific time, something like an alarmclock which goes off at a specific time. Let's say I want my app to start up at 8 in the morning, is that feasable ?
推荐答案
您可以用AlarmManager做到这一点,继承人一个简短的例子。首先,您需要设置报警:
You can do it with AlarmManager, heres a short example. First you need to set the alarm:
AlarmManager am = (AlarmManager) con
.getSystemService(Context.ALARM_SERVICE);
Date futureDate = new Date(new Date().getTime() + 86400000);
futureDate.setHours(8);
futureDate.setMinutes(0);
futureDate.setSeconds(0);
Intent intent = new Intent(con, MyAppReciever.class);
PendingIntent sender = PendingIntent.getBroadcast(con, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, futureDate.getTimeInMillis(), sender);
接下来,你需要创建一些code进行reciever来执行应用程序:(即 - 开始你的应用程序):
Next, You need to create a reciever with some code to execute your application: (ie- starting your app):
public class MyAppReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
startActivity(new Intent(context, MyApp.class));
}
}
相关推荐
最新文章