4

Android: Run asynctask once per session

 2 years ago
source link: https://www.codesd.com/item/android-run-asynctask-once-per-session.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Android: Run asynctask once per session

advertisements

OK, I have been researching this problem for a little while and I haven't found a good answer. I have an asynctask that checks if there is an update to my db through a web service. I only want to check once per user session on my app (right now it checks everytime i come back to the main screen). So if I close out of the app and the process gets stopped and they open it up again it will check the web service again.

I have read about using shared preferences but I can only think of doing that for the first time ever running the app.

I thought about saving a timestamp in my shared preferences and then checking if more than 30 minutes has passed since my last timestamp. But that seems to complicated for what I need.

Any ideas out there?


Shared preferences is actually really straight forward. You can set a variable to shared preferences. For example "0" When you start the async, that value changes to "1". So async will never run. When you quit, set shared preferences back to "0". The next time user starts the programme, asyncTask would run and set the value back to "1" again. So that when you go back to main screen, asyn will not run. Is this what you are looking for?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK