0

To save battery in my app I decided to use the "new" Fused locations. However I need to pass some parameters into the service which receives GPS updates. The way It's done below would work (putExtras(...)), but I would need to make a lot of classes Serializable/Parseable, which would be a pain.

I have searched around and found some other way using Binder, but can't figure out how to get it to work. Is using Binder the only way or is there another?

If anything is unclear, please tell. Thank you.

public class LocationService extends IntentService {
    ...
    public LocationService(StartActivity startActivity, DatabaseSQLite db, HomeFragment homeFragment) {
        super("Fused Location Service");
        ...
    }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
   db = (DatabaseSQLite) intent.getExtras().get("DatabaseSQLite");

   ...
   return START_REDELIVER_INTENT;

    }
}

And this is how It's used in my activity:

@Override
    public void onConnected(Bundle bundle) {
        mIntentService = new Intent(this, LocationService.class);
        mIntentService.putExtra("DatabaseSQLite", database);
        ...
        mPendingIntent = PendingIntent.getService(this, 1, mIntentService, 0);

}
Jakkra
  • 641
  • 8
  • 25

1 Answers1

2

You should check out https://github.com/greenrobot/EventBus

Examples can be found here: http://awalkingcity.com/blog/2013/02/26/productive-android-eventbus/

Basically lets you do stuff like:

@Override
    public void onConnected(Bundle bundle) {
        mIntentService = new Intent(this, LocationService.class);

        // could be any object
        EventBus.getDefault().postSticky(database);
        ...
        mPendingIntent = PendingIntent.getService(this, 1, mIntentService, 0);

}

And whenever you need the object

public class LocationService extends IntentService {
    ...
    public LocationService(StartActivity startActivity, DatabaseSQLite db, HomeFragment homeFragment) {
        super("Fused Location Service");
        ...
    }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {

   // could also be in Broadcast Receiver etc..
   db = EventBus.getDefault().getStickyEvent(DatabaseSQLite.class); 

   ...
   return START_REDELIVER_INTENT;

    }
}

Not only is it simpler, this link also shows that it outperforms other methods: http://www.stevenmarkford.com/passing-objects-between-android-activities/

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • Looks very good, however can't get it to work. I need to implement "onEventMainThread(DatabaseSQLite db) in my LocationService class and also in that class call EventBus.getDefault().register(this); Did I miss something? – Jakkra Aug 10 '14 at 23:13
  • No need to do the registering just for passing objects (the sticky ones). They allow to 'leave' objects on the eventbus and retrieve them at some later point in the code. The other part you are referring to is great for 'live' events, for example if you want an event to trigger in your activity when a button is clicked a fragment, or the other way around. Then you need to register to listen in for these events. – cYrixmorten Aug 10 '14 at 23:37
  • So I don't need onEvent either? The problem is that I get this error: com.example.RoadTrip D/Event﹕ No subscribers registered for event class database.DatabaseSQLite – Jakkra Aug 10 '14 at 23:39
  • What happens if you ignore the error (i assume it is not crashing the app) Are you able to capture the object in your IntentService? – cYrixmorten Aug 10 '14 at 23:41
  • For some reason my App doesn't want to fire my IntentService right now, might be bad GPS signal or something that is a problem for the LocationClient. However you are probably right that it should work anyway and yeah it's not crashing. Thanks for the help :) Will accept your answer as soon as I get the LocationClient to fire my Intent :p – Jakkra Aug 10 '14 at 23:51
  • No problem and good luck ;) I must admit I am a bit curious as to why you are using IntentService as it only fires once and then stops running (a bit like an AsyncTask). Where did you see examples doing this? – cYrixmorten Aug 10 '14 at 23:54
  • From this guy: http://www.kpbird.com/2013/06/fused-location-provider-example.html if you have a better way, please tell ;) Instead of just using a listener, which drain more battery. – Jakkra Aug 10 '14 at 23:58
  • Ok, I see it is also just used to receive the Location, not to keep the GPS alive :) I recently discarded fused location in favor of relying only on GPS and monitoring when the locations are of decent quality (device has firs-fix of satelites). Just seems simpler to me as it is contained within a single Service, so, a single class and update of the manifest and I am good to go :p http://stackoverflow.com/questions/19365035/location-servise-gps-force-closed/19366773#19366773 SO unfortunately I have no good code to show for fused locations – cYrixmorten Aug 11 '14 at 00:08
  • Will take a look at the code, thanks. Also I found my problem, I renamed the IntentService class yesterday and forgot to change in the manifest file, stupid me. Also everything works now as it should :) – Jakkra Aug 11 '14 at 00:16
  • Awesome! ;) The EventBus is fairly new to me too but I simply love it :) hehe – cYrixmorten Aug 11 '14 at 00:34