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;
    }


}
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • This isnt an answer but I have answered something similar, using a `Handler` is always better: http://stackoverflow.com/questions/13100196/making-a-interval-timer-in-java-android/13100626#13100626 – Chris.Jenkins Nov 18 '12 at 18:45
  • `if(MainActivity.con!=null)` - Don't do this. If I've read it right then `MainActivity` extends the Android `Activity` class. If that is the case NEVER expose any methods or fields in an `Activity` and try to access them from any other application component. An `Activity` should be completely self-contained. – Squonk Nov 18 '12 at 18:52
  • Thank you both for your responses, but I still don't see why the calls are stopping after only a few minutes.@Chris.jenkins I thought i was already using a handler. I looked at your other post, did I miss something? I was hoping to keep the calls running for quite a while, could I use a countdown timer set for a very long time? It isn't a good solution but it is the only one I can think of to try. – Rilcon42 Nov 25 '12 at 21:33

1 Answers1

0

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.

Rilcon42
  • 9,584
  • 18
  • 83
  • 167