0

In my app, a user will request location updates. When this happens, the main activity starts a Service that will start a location request.

My problem is, if the services is started, say 3 times in a two minute period, then it does not operate properly.

So, basically, I need the service to put the start request on hold until the current start is completely finished.

Each time my service is started, it can take up to ten minutes for it to complete, and the service responds to each start request differently based on the intent data that was passed to it.

I would be happy with (actually prefer) there being one location request for each of the three start requests that happened in the two minute period, but the intent data might be different from each of the three start requests.

So, I tried using IntentService to overcome this and handle all three requests one at a time, but then

LocationManager.requestLocationUpdates(...)

is not called. I understand this is likely due to onHandleIntent(...) pretty much immediately finishing and essentially not giving the location request time to respond. I have a handler that stops the location request after 7 minutes (3 mins for testing), and then location information is passed to other methods that return UI updates to the user. UI updates return, they are just null because the location updates fail.

I'm thinking I could use the onHandleIntent in an IntentService to start my service and pass the intent data to it, but that seems like a really bad practice, and there's probably a better way to do it. EDIT: That definitely won't work because onHandleIntent will immediately start my locationService anyway, so there would be no waiting period.

Also, any Toasts called within onHandleIntent do not show at all, though everything from my onCreate() works like it should.

I'm having a really hard time finding any answer to this, and any help would be greatly appreciated. Here's the gist of my code:

public class myService extends IntentService {

public findMyDroidService() { 
                super("findMyDroidService"); 
            }

@Override
 protected void onHandleIntent(Intent intent){

        intent.getExtras();
        fromIntentString = intent.getStringExtra("toIntentStringExtra");
        Toast.makeText(this, fromIntentString, Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Intent Handled", Toast.LENGTH_SHORT).show();
                    locMan.requestLocationUpdates(locListener,...);      
 }
@Override
public void onCreate(){
    super.onCreate();
     locListener = new LocationListener(){
              //code goes here
     }
}
}

Also, my location listener is instantiated within the onCreate();

Reed
  • 14,703
  • 8
  • 66
  • 110
  • For my toast problem I found http://stackoverflow.com/questions/3955410/create-toast-from-intentservice but I don't really care about the toast thing too much, so I haven't tried it. – Reed Jul 05 '11 at 02:10

1 Answers1

0

I was definitely way overthinking it.

In my situation, the issue was that I needed to keep all the data separate, so the string fromIntentString would not get overriden with new start requests. I used an array to do this, easily enough.

public class myClass extends Service {
      int arrayInt;
      ArrayList<String> arrayStringList;
      int myInt;

      @Override
      public void onCreate() {
            super.onCreate()
      arrayStringList = new ArrayList<String>();
      myInt = 0;
  // additional code not important for this example
      }
@Override
public void onStart(...){
handleCommand(intent);
}
@Override
public void onStartCommand(...){
handleCommand(intent);
return START_STICKY;
}
//I call both onstart and onStartCommand for backwards compatibility
public void handleCommand(Intent intent){
   intent.getExtras();
   arrayStringList.add(intent.getStringExtra("fromIntentString"));
// so I know it worked
   Toast.makeText(this, arrayStringList.get(arrayInt).toString(), 
   Toast.LENGTH_SHORT).show();
//So I can keep track of the size of arrayStringList
   ++arrayInt;
//code for getting the location
useMyData();
}
public void useMyData(){
   // do location getting code
  // Here's where I actually use my array. For this answer, I will just show a toast.
Toast.makeText(getApplicationContext(), arrayStringList.get(myInt).toString(), 
Toast.LENGTH_SHORT).show();
    ++myInt;
    if (myInt < arrayInt) useMyData();
    //I was going to use a for loop, but I couldn't get it to work, and I like this method
    //better 

EDITS: I was previously using an Array[String] instead of the ArrayList, but that didn't work, because I had to be able to continuously grow the size of my array, which is not possible. Array's have a fixed size, so I used an ArrayList, which now works.

Reed
  • 14,703
  • 8
  • 66
  • 110