启动的应用程序在特定的时间 [英] Start app at a specific time
本文介绍了启动的应用程序在特定的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在想,如果有可能(如果它是如何)启动我的应用程序在特定的时间,像一个闹钟其在特定时间熄灭。 比方说,我想我的应用程序启动在早上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));
}
}
这篇关于启动的应用程序在特定的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文