在特定时间启动应用程序 [英] Start app at a specific time

查看:38
本文介绍了在特定时间启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能(以及如何)在特定时间启动我的应用程序,例如在特定时间响起的闹钟.假设我希望我的应用在早上 8 点启动,这可行吗?

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); 

接下来,您需要使用一些代码创建一个接收器来执行您的应用程序:(即启动您的应用程序):

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, MyAppMainActivity.class));
   }
}

这篇关于在特定时间启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆