Check for Updates Once a Day

5 votes · 19 comments

This code checks for updates of the Activity once a day and in the background. If an update (higher version than current) is found, it opens a Dialog and asks the user to open the market.

raw ·
copy
· download
public class Test extends Activity { private Handler mHandler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.front); mHandler = new Handler(); /* Get Last Update Time from Preferences */ SharedPreferences prefs = getPreferences(0); lastUpdateTime = prefs.getLong("lastUpdateTime", 0); /* Should Activity Check for Updates Now? */ if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) { /* Save current timestamp for next Check*/ lastUpdateTime = System.currentTimeMillis(); SharedPreferences.Editor editor = getPreferences(0).edit(); editor.putLong("lastUpdateTime", lastUpdateTime); editor.commit(); /* Start Update */ checkUpdate.start(); } } /* This Thread checks for Updates in the Background */ private Thread checkUpdate = new Thread() { public void run() { try { URL updateURL = new URL("http://my.company.com/update"); URLConnection conn = updateURL.openConnection(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while((current = bis.read()) != -1){ baf.append((byte)current); } /* Convert the Bytes read to a String. */ final String s = new String(baf.toByteArray()); /* Get current Version Number */ int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode; int newVersion = Integer.valueOf(s); /* Is a higher version than the current already out? */ if (newVersion > curVersion) { /* Post a Handler for the UI to pick up and open the Dialog */ mHandler.post(showUpdate); } } catch (Exception e) { } } }; /* This Runnable creates a Dialog and asks the user to open the Market */ private Runnable showUpdate = new Runnable(){ public void run(){ new AlertDialog.Builder(Test.this) .setIcon(R.drawable.icon) .setTitle("Update Available") .setMessage("An update for is available!\\n\\nOpen Android Market and see the details?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked OK so do some stuff */ Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:your.app.id")); startActivity(intent); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Cancel */ } }) .show(); } }; }
Add a comment

19 Comments

What if the user leaves this Activity while the thread is running? When the thread completes, won't it notify the handler and try to show the dialog on top of the now paused Activity?

Perhaps in the Test activity, it should set the mHandler reference to null in onPause(). And then perhaps the showUpdate Runnable should check if the mHandler is null before showing the dialog? This way you won't get the alert if the originating Activity is no longer active.

I'm not sure if this is a problem, just posing the question.

Reply · April 13, 2009, 8:41 a.m.

Re: Mr. Burke's comment, I don't think Handler messages get processed while the activity is paused, since Handler messages go on the main message loop, no different than any UI operations. It would be worth testing, though.

Reply · April 13, 2009, 8:56 p.m.

Re: Eric's comment: It's true, if the Activity closes between checking and posting the Handler, it will crash with the log message "is your activity running?".

As Eric suggested, resetting the mHandler in an onPause() function and checking it in the Thread does the trick!

Reply · April 15, 2009, 3:45 a.m.

So this Test activity has to be open/running in order for it to run the checking? And the checking only kicks off in the onCreate() method, meaning it will just run once.

I was expecting to see an Alarm Manager to manage the scheduling with a Service implementation that does the actual background checking in a separate threads, and checking/setting the next check update.

Reply · April 28, 2009, 4:55 a.m.

I think it is a better idea to check first if there is an active internet connection, then perform the update and if it succeeds to set the lastUpdateTime variable.

Reply · June 26, 2009, 5:03 p.m.

Hi I know this is a newb question, but on the update site, what should we put, just the version, and is there any specific format the site should be in (html, htm, xml...). Thanks for any help.

-Sean

Reply · July 16, 2009, 9:47 a.m.

also I forgot to mention that for some reason eclipse is saying there is a problem with this line:

new AlertDialog.Builder(Test.this)

-Sean

Reply · July 16, 2009, 9:49 a.m.

Ok never mind about the first question, I figured it out, but still fighting with new AlertDialog.Builder(Test.this)

-Sean

Reply · July 16, 2009, 10:08 a.m.

At "http://my.company.com/update", should i put an xml file? If yes whats the format like? I tried putting my AndroidManifest file of the updated version code and version name but it doesn't work.. newbie

Reply · March 6, 2011, 10:49 a.m.

@cyben76: the file should only contain an integer, the version number of the currently most up-to-date version. If the app which reads that file has a lower version-number it will show the dialog.

Reply · March 6, 2011, 4:31 p.m.

What a fantastic story. I have followed your story from another article you have written till here. I like to share Beauty products advertising , beauty products public relations services for all.

Reply · April 9, 2011, 8:17 a.m.

It is very well information about on this page. All people always want to know all about things that you have been described. It is very valuable & very nice posting! I will bookmark this blog as I have web hosting , jigsaw puzzle game services for all. Thanks.

Reply · April 12, 2011, 9:59 a.m.

Thank you. I just found this blog and what a great resource! It is a good blog and their posts are effective. This is very nice post! I will bookmark this blog. I like to share Printing Miami , Printing ft lauderdale service for all.

Reply · April 15, 2011, 6:04 a.m.

I think you posted so effective content about of checks for update. Thanks for your informative posting. As I have carpet modesto , carpet lodi and carpet merced services for all. Thanks.

Reply · April 21, 2011, 4:06 p.m.

I feel so good to read your article of check for pdates once a day. Because here included so effective information. It may help me about related matter. I highly recommend it & like to share flooring salida , flooring elk grove and flooring stockton services for all. Thanks.

Reply · April 22, 2011, 5:31 p.m.

Here has so alternative posting about of update. I like this article for the importance of it as I want to share Furnace Filter , Air Conditioner Filter and Filter Delivery Service services for all. Thanks.

Reply · May 3, 2011, 7:57 a.m.

Here has so alternative posting about of update. I like this article for the importance of it as I want to share carpet salida , carpet elk grove and carpet stockton services for all. Thanks.

Reply · May 4, 2011, 11:15 a.m.

It helps me to know about of this page. You have been shown importance topic in your content. It will inspire me always. This is very nice post! I will bookmark this blog as I have cpr certification new york , cpr certification san diego and free cpr certification services for all. Thanks.

Reply · May 6, 2011, 9:07 a.m.

It is very well information about updating. All people always want to know all about things that you have been described. It is very valuable & very nice posting! I will bookmark this blog as I have seo company michigan , Link Building Package & Skype ID: shamir.devnath, Y messenger: shamirdevnath@yahoo.com, E-mail: support@linkbuildingcompanyseo.com to all.

Reply · June 12, 2011, 5:31 a.m.

I feel very lucky can find this page. Finally i find something that i want to know.Thank you for this usefull informations.. Website design company

Reply · June 29, 2011, 5:50 a.m.

Very true, I only started discovering this a few years ago. It can show me a best way on this idea. This is very nice post! I will bookmark this blog. Looking for Affordable health insurance quotes? Free affordable health insurance plans, health insurance quotes, Insurance Quotes for you Guaranteed. We care you, Quotes on individual or family. Thanks.

Reply · July 6, 2011, 5:06 p.m.

This is a really good read for me Facebook Eid Status , Must admit that you are one of the best bloggers I ever saw. Eid Status for facebook Thanks for posting this informative article.

Reply · Aug. 15, 2011, 6:34 a.m.