Android 在特定日期和时间使用Alarmmanager启动服务并重复

Android 在特定日期和时间使用Alarmmanager启动服务并重复,android,alarmmanager,repeat,Android,Alarmmanager,Repeat,我想创建提醒应用程序 我已经搜索了很多地方,但是找不到关于如何在每天特定时间使用AlarmManager启动服务(或者如果不可能,则启动活动)的清晰的顺序说明 我希望在特定日期和时间开始服务,例如,在该日期六个月后再次开始服务并继续此周期 Calendar cur_cal = new GregorianCalendar(); cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date fo

我想创建提醒应用程序

我已经搜索了很多地方,但是找不到关于如何在每天特定时间使用AlarmManager启动服务(或者如果不可能,则启动活动)的清晰的顺序说明

我希望在特定日期和时间开始服务,例如,在该日期六个月后再次开始服务并继续此周期

Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

Calendar cal = new GregorianCalendar();
cal.add(Calendar.YEAR, 2018);
cal.set(Calendar.MONTH, 2);
cal.set(Calendar.DAY_OF_MONTH, 12);
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

Intent intent = new Intent(ProfileList.this, BroadcastedReceiver.class);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000, pintent);
我在2018-02/12 9:00尝试了此代码以激活警报。。。这是在这个日期工作,但我有问题重复后,例如六个月…我如何更改代码为开始服务每六个月


注意:日期存储在数据库中,并将从数据库中调用日期

最好根据需要使用作业调度程序或firebase调度程序

    public class AddService extends Service
    {
        InterstitialAd mInterstitialAd;
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
           //Your Logic
            stopSelf();
            return START_NOT_STICKY;
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onDestroy() {
            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.set(alarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000*60*60),
                    PendingIntent.getService(this,0,new Intent(this,AddService.class),0));
        }
    }

// Here your service will call every 1 hour, you can change time according to that
请参阅此链接


6个月对于重复报警来说太长了。我并不是说这是不可能的,但用户可能会在这段时间内卸载应用程序。您可以使用另一种更好的方法。此应用程序不面向用户,只使用一个人…谢谢,什么是stopSelf()函数,抱歉,我是初学者,你能给我发送MainActivity和service以及mainfest的完整代码吗?stopself用于结束服务它将停止,然后在1小时后@MarGinIn manifest和MainActivity startService中再次启动(new Intent(this,AddService.class));那么我可以在哪里设置具体日期呢?我不能使用firebase,因为这项服务在我的国家是禁止的。我想使用alarmswhat about job schedulerA程序来提醒您何时切换过滤水净化器。我可以在特定日期运行作业计划程序吗?谢谢,所以我应该使用作业计划程序。这比Alarmmanager更好