Question

I have a somewhat specific workflow in my android application, so that I don't know where I should bind the Billing service.

Initially my app starts the first background task which determines location of a user and afterwards calls the second background task, because it can run only after location has been determined.

Now I want the second background task to execute only if certain subscription is active.

The problem is that both events - start of the second task and obtaining service connection - are undetermined in time, so I don't know how to synchronize between them.

Example activity:

public class MyActivity extends Activity
{
    public void onLocationChanged(Location location)
    {
        // callback is called when location is obtained
    }

    public void onServiceConnected(ComponentName name, IBinder service)
    {
        // called when service is connected
        mService = IInAppBillingService.Stub.asInterface(service);
    }

}
Was it helpful?

Solution

You can use a synchronized int counter, for each of steps you've completed, increase it by one. And every time you complete a step, call a method to see if the task is complete and the next task can be started, psudo code:

public synchroized boolean executeNext() {
   completedCount++;
   if(completedCount == TOTAL_STEPS) {
       //starts the next task here
       return true;
   }
   return false;
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top