1

I am developing an android app.

I am confused on how to store mysql data until a connection is restored. I want to have something in place if a user does not have a connection then data is stored in a database locally on the android device, but when connection is restored it would add data to the database.

How can I constantly check for a connection? Even when the app is off and then the data is added online to the mysql database. Ideally the connection would be checked every 30mins.

Are there any online examples for this please?(Checking for connection then doing a task?) I have checked but did not get many links.

Many thanks

anb123
  • 33
  • 1
  • 5
  • I think you should refer to this answer: http://stackoverflow.com/questions/1783117/network-listener-android – zwebie Nov 20 '14 at 12:59
  • Thanks, also want to know how and where should I store the information until the connection is restored and how can I check network connection after a certain period of time? – anb123 Nov 20 '14 at 13:43

1 Answers1

1

Using the ConnectivityManager class you can receive network information. With the AlarmManager you can set a recurring task that will check periodically the network state with the aforementioned class. In the meanwhile you can store your data in a sqlite database locally.

you can refer to this example:

https://developer.android.com/training/scheduling/alarms.html

Since the Alarmmanager needs a Context I would recommend using a service.

zwebie
  • 2,349
  • 1
  • 27
  • 43
  • I have never worked with Alarm manager class is it please possible if you could guide me to any tutorials online? Thanks Also would this run in a service or a thread? Which would be best? – anb123 Nov 20 '14 at 21:24
  • @anb123 I have edited my answer with a tutorial on how to use the alarmmanager – zwebie Nov 23 '14 at 09:18
  • by the way I think you should remove the php tag in this question since it's not really related – zwebie Nov 23 '14 at 09:19
  • Android needs php connections i therefore added the tags for php – anb123 Nov 27 '14 at 22:23