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

  • SEARCH
  • Home
  • 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 9012713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:04:32+00:00 2026-06-16T03:04:32+00:00

I have an Activity (called StartActivity) that starts a Service. The Service calls methods

  • 0

I have an Activity (called StartActivity) that starts a Service. The Service calls methods in my MainActivity class. The process is supposed to repeat on a timer which is inside the Service class. The program stops after one execution however, it is apparently not rescheduled. I know the onStart command is given to the Service when the Activity is started because the program functions the first time, when the activity is clicked. Neither task runs again. Any advice would be welcome.
TLDR: Timer is not calling method repeatedly. It only works the first time.

Service Class

import java.util.TimerTask;
import java.util.Calendar;
import java.util.Timer;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class SvcActivity extends Service {

    TimerTask questionTask;
    TimerTask gpsTask;
    Handler handler = new Handler();
    Timer t = new Timer();

    @Override
    public int onStartCommand(Intent i,int flags,int startID) {
        Log.i("AAA","---In service---");
        questionTask = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        if(getHour()>=10&&getHour()<=22)
                        {
                            Intent i=new Intent(SvcActivity.this,MainActivity.class);
                            i.putExtra("key", "setup");
                            i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            Log.i("AAA","---right before startActivity---");
                            startActivity(i);
                        }
                        else
                            Log.i("AAA","---not between 10am and 10pm. Correct!");
                    }
                });
            }
        };

        gpsTask = new TimerTask() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        Log.i("AAA","---in async task");
                        if(MainActivity.con!=null)
                        {
                        Log.i("AAA","---about to call async task");
                        BackgroundActivity objAsyncTask = new BackgroundActivity();            
                        objAsyncTask.execute();
                        }
                    }
                });
            }
        };

        int q=(int)(Math.random() * 2);
        if(q<1)
        {
        // first run, subsequent time delay. in miliseconds
            t.schedule(questionTask, 300, (3600000+2700000)); // 1 hour 45 min  
        }
        else
            t.schedule(questionTask, 300, (7200000+900000)); // 2 hour 15 min   

        t.schedule(gpsTask, 300, 300000);// 5 min

        return START_STICKY;
    }

    //-----------------------------get hour------------------------------------------
    public int getHour()
    {
        Calendar c=Calendar.getInstance();
        int hour=c.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }


}
  • 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-16T03:04:34+00:00Added an answer on June 16, 2026 at 3:04 am

    The problem was that the activity was being killed. Even though it was restarting this was messing up the timing. My eventual solution was to use AlarmManager to set a repeating alarm which was not affected by the activity being killed. I then caught this with a BroadcastReceiver and executed my code from there.

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

Sidebar

Related Questions

I currently have a class called clientActivity that contains a finger paintig activity that
I have a list of objects called Activity: class Activity { public Date activityDate;
I have one class called Global and two other activities. In each activity I
In my activity oncreate method, i have called a service using OnStartCommand(). My requirement
Specfically, I have an activity that can be called from the homescreen, OR can
suppose that you have 3 Activities (A, B and C) and Activity A starts
I have the following code in an Activity that starts a dialog for a
Lets say you have one main activity called main, and other sub-activities that do
Going round in circles here i think. I have an activity called Locate; public
I have created an activity called viewActivity. It displays the shared files and folders

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.