Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:37:26+00:00 2026-06-15T07:37:26+00:00

I need my Service to do tasks every night at 02:00, and if failed

  • 0

I need my Service to do tasks every night at 02:00, and if failed in task then schedule itself to start again after 30min, but if succeeded then stop itself and start again the other night at 02:00. I managed to get it start at 02:00 but after it is triggered it just triggeres itself continiously.

My code is in Service, and here is the code itself:

@Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        //doTasks();

        MediaPlayer player = MediaPlayer.create(this,
                Settings.System.DEFAULT_NOTIFICATION_URI);
        player.start();

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 2);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
    }

doTasks() is the tasks I need to run but because they are slow I use notification sound for testing purposes. And pendingIntent is this Service itself.

Any ideas what is it that makes my AlarmManager trigger constantly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:37:27+00:00Added an answer on June 15, 2026 at 7:37 am

    If your pendingIntent is set to start the Service then basically you’re repeatedly creating a new alarm each time in the onStart(...) method.

    AlarmManager has a mechanism where it will trigger an alarm “late” if the device was asleep when it should have triggered. In your case, because you constantly re-create the alarm, AlarmManager constantly thinks it should be triggered immediately even if the time is past 02:00:00.

    Create the alarm somewhere else to prevent it being constantly re-created each time the Service starts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to do some recursive tasks, say do something after every five seconds.
I need to schedule a task to be run periodicily (e.g every 6 hours,
i need an sms service that can gives me a phone number and then
All the service calls in my application are implemented as tasks.When ever a task
We need a service that we can use to schedule events. For instance, we
I have a service that will send out notifications with timed tasks. I need
I'm providing free wifi service and need an ad to be added to all
I am new to Java web service and need to be familiar with REST
In a route (that exposes a REST service) i need to get the local
I have a web app (MVC) where I need some automatic service to run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.