简体   繁体   中英

Change link to download every 7 days?

I'm downloading a certain website in html format to my device, so that I can display it in webview in offline mode. The only problem is that the link is dynamic, and it changes once a week. To keep the html item updated as much as possible, I want the app to download it once a week.

Let's say for example that this is the websites address:

www.mywebsite.com/1

Next week, the address will be:

www.mywebsite.com/2

And week after that, the website will be:

www.mywebsite.com/3

I already figured I would do this be declaring a variable that would be changing, something like

int week;
String urlToDownload = "www.mywebsite.com/" + week;

But how do I make it so that this variable will change everyday even if the app is not started, or is there a better way to do this?

You can maybe use AlarmManager class. That allows you to plan something on the background, when app is not even running.

I would use the most simple solution. Do you know what time does the URL change? You can always check the time of previous start of application and when next app is started check it and determine how many weeks is from that.

You can update the variable during the onStart() phase. Make a constant that has the start date, and then get the current date and figure out the offset. This way even if the app hasnt been started in a long time, once it is started you will have the proper link.

Use the java.util.Calendar

Calendar calender = Calendar.getInstance();
MyLog.d("Current Week:", "" + calender.get(Calendar.WEEK_OF_YEAR));

This prints "Current Week: 37"

With that maybe you can write code to get the appropriate page. The week nr are kind of static

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM