0

I am using the Volley library to pull data from a remote db.

I have locked the fetching process inside a ProgressDialog because, if not, I noticed the data could or could not reach the app. By locking it, I make sure the data is there to be shown to the users.

Now, I have implemented a local db so the user doesn't need to fetch from the remote db the same information every time.

The information fetched, only comes when a certain condition is met. This is the SQL:

SELECT *
FROM user_meals
WHERE CURDATE() BETWEEN date_of_meal AND DATE_ADD(date_of_meal, INTERVAL 14 DAY)
AND email = '$email'
ORDER BY date_of_meal DESC
LIMIT 1

The process goes like: Android app sends petition to PHP file -> PHP file runs SQL -> PHP file answers -> Android app listens to the answer.

So my question is:

How can I make the fetching process run in the background, and make it run every given amount of time?

Thank you.

Edit:

Apparently it is a bad practice to do periodic requests to a server. So instead, I am going to handle them on demand. Thank you all.

herrmartell
  • 3,057
  • 4
  • 18
  • 27

3 Answers3

1

By definition making periodic fetching is a very bad practice

So you should really do it only on demand - and if you already activate the radio for sending so at least piggy back everything else. Anyway the correct approach would be to create a service ( or intent service) where you post you data and it syncs it to the network ( I use it between realm and remote backend succesfully)

Dan Kuida
  • 1,017
  • 10
  • 18
  • But my question remains: assuming I put everything on the service, I would still need to make the service work every n amount of time. How? – herrmartell Feb 17 '16 at 21:18
  • as Doug Stevenson wrote - alarm manager would probably be the best option - if it is a location based so geofence works well also – Dan Kuida Feb 17 '16 at 21:20
  • Okay. I will research on this, and try to come up with a solution. Thanks. – herrmartell Feb 17 '16 at 21:23
  • again - try to avoid making periodic request - and if you already do them so bring all the rest of the data and precache - like on DB. the data transfer take only several seconds but it takes for the device to shut down the TRANSMITER around 40-70 seconds – Dan Kuida Feb 17 '16 at 21:25
  • I'm thinking on just adding a refresh button, or a pull-to-refresh action. Is that what would it be called "good practice"? – herrmartell Feb 17 '16 at 21:30
  • 1
    if you have no choice you have no choice - right ?like if you need to send something for immediate response - but if you can wait ? wait. if you can "guess" what the user will want - cache it on previous or next request. it is better to take a bit more data (with smaller images for example) than to open request frquently - and if you open - sync all the rest, analytics, data not synced and more – Dan Kuida Feb 17 '16 at 21:33
0

You can use a Handler to run it repeatedly:

final Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
  @Override
  public void run() {
    if (!isStopped){

        //Do your work here

        mHandler.postDelayed(this, 5000);
    }
  }
}, 0);

See here.

Community
  • 1
  • 1
SoroushA
  • 2,043
  • 1
  • 13
  • 29
  • But, as far as I know, Volley was created so people could stop using HttpRequest + AsyncTask. Isn´t this like a contradiction? – herrmartell Feb 17 '16 at 21:17
  • My mistake, I didn't notice that you were using Volley. I guess you can replace the ASynkTask with Volley. My point was that you can take advantage of a Handler to repeat requests – SoroushA Feb 17 '16 at 21:33
0

There are many many ways to do this, but one solution might be to use AlarmManager to schedule a repeating intent to be delivered to a Service you create in your app. The Service can then do whatever you need. It will take some research to best assemble these components.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441