0

I am trying to program a service that sends GPS location data to a server at settable time intervals. Being fairly new to android, this is not turning out as easy as I was hoping and the internet doesn't really seem to be helping me either. The service should...

  • Start at boot
  • be able to read settings, that are anytime changeable from an activity/app and also apply these settings in real time without having to reboot the phone
  • be able to upload the data to the internet (httpRequest)
  • Not cause my phone to crash (previous attempts caused my phone to crash)

What is the best solution? What direction should I be heading in?

In terms of the LocationListener itself, what is here the best solution?

Thank you for your help.

  • "the internet doesn't really seem to be helping me either": Really? Have you tried the search feature on this site? – verybadalloc Jun 20 '13 at 12:11
  • Yeah, there are a LOT of articles reagarding each and every part of your requirements. – g00dy Jun 20 '13 at 12:14
  • I guess you're not looking for the right thing. In Android, "Service"s are not quite the same as in Windows. – Fildor Jun 20 '13 at 12:19
  • I tried using a broadcastreceiver (which didn't actually work), and I have to correct myself as I have found several articles, but implementing the ideas mentioned has been a little tricky. I'm just not sure how to gather the GPS data- whether to do it in a thread of some sort. – user2505059 Jun 20 '13 at 13:32

1 Answers1

2

After 7 minutes of search, here are a few articles to help you out:

Start at boot

This is done using a BroadcastReceiver. Check out this answer for the exact way to implement it: Android -Starting Service at Boot Time

be able to read settings, that are anytime changeable from an activity/app and also apply these settings in real time without having to reboot the phone

EDIT As per the OP's comment, one can use PreferenceActivity to create a Settings page for an app. This article shows basic implementation of a PreferenceActivity, while this excellent question shows how to read these preferences from the SharedPreferences. And, yes SharedPreferencs will persist after a reboot (they don't persist after an app uninstall though).

be able to upload the data to the internet (httpRequest)

On how to make HTTPRequests in android: Make an HTTP request with android

Not cause my phone to crash (previous attempts caused my phone to crash)

This one really depends on you. But, hey, you can always search this site for all and every possible error you get.

In terms of the LocationListener itself, what is here the best solution?

Try this one (which has complete code example), and this one.

Community
  • 1
  • 1
verybadalloc
  • 5,768
  • 2
  • 33
  • 49
  • Under settings I meant things like the time interval between sending of the data, actually enabling/disabling the service and the ID required to register the data when connecting to the server. – user2505059 Jun 20 '13 at 13:33
  • @user2505059 you mean a [Preference Activity](http://viralpatel.net/blogs/android-preferences-activity-example/)? – verybadalloc Jun 20 '13 at 14:30
  • For example, but are the preferences kept after rebooting? – user2505059 Jun 20 '13 at 14:49
  • Thanks the problem is nearly solved! The remaining problem is when changing the preferences- how do I get the changed preferences to be applied immediately? – user2505059 Jun 26 '13 at 14:52
  • @user2505059 You would need to implement a [OnPreferencesChangedListener](http://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html). Your listener gets a reference to the sharedPrefs that were changed, and a key to access the new value. Beware of [this](http://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently) problem though. If you want me to provide you with more details, please amend your question first. – verybadalloc Jun 26 '13 at 16:23