0

I am in the process of making an application. I currently have a database and a method that uploads pictures (along with data corresponding with the picture/s, stored in the database) using a PHP script on the server. Neither the purpose nor the database's schema are important. I haven't really done anything like this before but have looked into it a bit. Basically the user will take a picture with the app. The picture and its data will upload if there is a network connection, but won't if there is no connection (duh). I want to have the pending images upload once a connection exists.

To do this...My current plans are to use a BroadcastReceiver to detect when the user is connected to a network then use a Service to upload the files. Is this the proper approach? How can I basically go about starting a service when connectivity exists if and only if there are pending uploads?

snotyak
  • 3,709
  • 6
  • 35
  • 52

1 Answers1

2

Your approach sounds reasonable to me in general.

You would register a broadcast receiver with your package, listening for android.net.conn.CONNECTIVITY_CHANGE.

If the activities also trigger your service, you may be able to properly handle every case with an IntentService alone, so you won't have to deal with the many questions around when to stop or start a service with a specific life cycle.

Hope this helps; you don't sound like a beginner so you'll be able to make ends meet.

class stacker
  • 5,357
  • 2
  • 32
  • 65
  • @BackpackOnHead In general, yes. You need your IntentService to be stateful; probably you'll need a worker thread in parallel as I think of it. You can try to listen to the BOOT_COMPLETED broadcast. _However_, this will require an additional permission _and_ it will make it impossible to move your app to the SD card, because the SD card is mounted _after_ BOOT_COMPLETED. There are tons of discussions on how to solve this. The question is, how user friendly do you want to be, and how quickly do you want to start the upload after a reboot. Want to search first or discuss now? – class stacker Jan 25 '13 at 08:25
  • sorry, I removed the question because it wasn't completely relevant to the original question and I had quickly found an answer afterwards. Thanks though. Here's my answer - http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android – snotyak Jan 25 '13 at 08:27
  • @BackpackOnHead No problem. Sounds like you're on the right track. HF! – class stacker Jan 25 '13 at 08:31
  • @BackpackOnHead BTW, I'm not happy with this BOOT_COMPLETED enthusiasm. I have an Android with a ddual core 1GHz Tegra and it keeps being at almost full load for ages after a reboot. I think everybody's jumping on that train so it's getting a bit full. Same applies to CONNECTIVITY_CHANGE. Suppose the user needs to send an email while travelling and having bad connectivity. I think those services should be more polite than they usually are. So if you ask me, I'm only setting an alarm after these events occur. With decreasing interval if the task doesn't succeed, though. ;) – class stacker Jan 25 '13 at 08:38
  • don't worry, I'm not doing anything hurtful to the user. I'll most likely add some exponential backoff algorithm in there as well. Thanks for your concern though! – snotyak Jan 25 '13 at 08:41