GSMTaskManager onRunTask calls MainActivity method?

44 views Asked by At

I have an app that the MainActivity has a method (called doUpdate()) that is called from a button hit. This uses the MainActivity's public variables to access a database, perform some updates, and update some records. This works well.

We now need to automate this with a PeriodicTask as well.

I created a GCMTaskManager service as follows:

public class MyPeriodicTaskService extends GcmTaskService {
  public MyPeriodicTaskService() {
  }

  @Override
  public int onRunTask(TaskParams taskParams) {
    Log.i("MYLOG","Task Running...");
    return 0;
  }
}

In my MainActivity, onCreate(), I setup the PeriodicTask as follows:

 GcmNetworkManager networkManager=GcmNetworkManager.getInstance(this);
 PeriodicTask task=new PeriodicTask.Builder()
    .setService(MyPeriodicTaskService.class)
    .setPeriod(60)
    .setFlex(30)
    .setRequiresCharging(true)
    .setTag("UpdateSchedule")
    .build();
 networkManager.schedule(task);

By watching the LOG, I know that the onRunTask() fires periodically as I hoped.

Now I need to call my MainActivity method... doUpdate(). Because this method is declared PUBLIC VOID and not STATIC, I can't call it from the services doRunTask(). If I attempt to make it a STATIC PUBLIC VOID then the MainActivity variables can't be accessed properly as needed for the internal processing steps.

How do I get around this... any recommendations?

0

There are 0 answers