0

我在我的应用程序中创建了两个后台服务。我必须每隔一段时间就开始我的后台服务。所以我为此使用警报。一项服务必须每 15 分钟启动一次,另一项服务每天启动一次。我的代码在这里。

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, 15);
        Intent intent = new Intent(this, TestService.class);

        pintent = PendingIntent.getService(this, 0, intent, 0);
        alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);;

        i=15;                                 
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), i*1000, pintent);

         if (networkInfo != null && networkInfo.isConnected()) 
         {

                startService(new Intent(getBaseContext(), TestService.class));

         }
        else
        {


        }

我是这样用的。它在第一次工作得很好。这意味着我的闹钟第一次在 15 分钟后再次响起。然后它每 15 秒重复一次。我不知道如何为此正确设置时间。任何机构都可以告诉我实现这一目标吗?提前致谢。

4

1 回答 1

3
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hr);
cal.set(Calendar.MINUTE, minutes+15);//
cal.set(Calendar.SECOND, 0);

改变这一行...

alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), i*1000, pintent);

长重复时间=15*60*1000;

alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),repeatingTime, pintent);
于 2014-03-05T05:30:41.777 回答