如何在 android 的特定时间安排服务
How to schedule at particular time service in android
我正在制作演示 android 项目,我想在其中安排每天 8:00 时钟的服务。我从我的启动器 activity 调用这个方法。每当我启动应用程序时,都会调用下面的方法,同时它会启动服务并显示通知。我只希望它应该以这样的方式安排它应该在 8:00 而不是每次我打开应用程序时执行。
public static void scheduleVerseNotificationService(Context mContext) {
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, Notification.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, intent, 0);
// Set the alarm to start at approximately 08:00 morning.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
谢谢
我在理解你的问题时遇到了一些问题,所以:
I only want it should schedule in such a way it should execute at 8:00 instead of everytime I open app.
我理解为 "I only want to execute an Activity at 8:00, and not when I start that Activity"
所以,一个建议是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent YOUR_INTENT = getIntent();
if (YOUR_INTENT!= null) {
if(YOUR_INTENT.getStringExtra("YOUR_PACKAGE.A_VAR_NAME") != null){
if (YOUR_INTENT.getStringExtra("YOUR_PACKAGE.A_VAR_NAME").equals("A VALUE")) {
DO_THE_CODE_YOU_WANT();
}
}
然后,根据您的意图,添加一个值:intent.putExtra("YOUR_PACKAGE.A_VAR_NAME", "A VALUE");
您似乎为已经过去的时间安排了计时器,所以它会在您请求时立即调用 alarmManager.setInexactRepeating
这是解决您的问题的代码:
public static void scheduleVerseNotificationService(Context mContext) {
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, Notification.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, intent, 0);
// reset previous pending intent
alarmManager.cancel(pendingIntent);
// Set the alarm to start at approximately 08:00 morning.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
// if the scheduler date is passed, move scheduler time to tomorrow
if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 1);
}
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
尝试将此行用于 pendingIntent
pendingIntent= PendingIntent.getBroadcast(mContext, 0, intent, 0);
我正在制作演示 android 项目,我想在其中安排每天 8:00 时钟的服务。我从我的启动器 activity 调用这个方法。每当我启动应用程序时,都会调用下面的方法,同时它会启动服务并显示通知。我只希望它应该以这样的方式安排它应该在 8:00 而不是每次我打开应用程序时执行。
public static void scheduleVerseNotificationService(Context mContext) {
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, Notification.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, intent, 0);
// Set the alarm to start at approximately 08:00 morning.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
谢谢
我在理解你的问题时遇到了一些问题,所以:
I only want it should schedule in such a way it should execute at 8:00 instead of everytime I open app.
我理解为 "I only want to execute an Activity at 8:00, and not when I start that Activity" 所以,一个建议是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent YOUR_INTENT = getIntent();
if (YOUR_INTENT!= null) {
if(YOUR_INTENT.getStringExtra("YOUR_PACKAGE.A_VAR_NAME") != null){
if (YOUR_INTENT.getStringExtra("YOUR_PACKAGE.A_VAR_NAME").equals("A VALUE")) {
DO_THE_CODE_YOU_WANT();
}
}
然后,根据您的意图,添加一个值:intent.putExtra("YOUR_PACKAGE.A_VAR_NAME", "A VALUE");
您似乎为已经过去的时间安排了计时器,所以它会在您请求时立即调用 alarmManager.setInexactRepeating
这是解决您的问题的代码:
public static void scheduleVerseNotificationService(Context mContext) {
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, Notification.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, intent, 0);
// reset previous pending intent
alarmManager.cancel(pendingIntent);
// Set the alarm to start at approximately 08:00 morning.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
// if the scheduler date is passed, move scheduler time to tomorrow
if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 1);
}
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
尝试将此行用于 pendingIntent
pendingIntent= PendingIntent.getBroadcast(mContext, 0, intent, 0);