I'm trying the below code. It's suppose to check for newer version of my app once a day when the app starts. Got the code from http://www.androidsnippets.com/check-for-updates-once-a-day. The thing i can't figure out is, in the line URL updateURL = new URL("http://my.company.com/update"); whats should the http://mycompany.com/update point to? An xml, txt or .. file. I tried an xml and txt file with just the latest versionCode and named it version.txt, versionCode.txt and .xml.
It just doesn't work and should the url be just "http://mycompany.com/update" or with extension ie, "http://mycompany.com/update/version.txt" or something else.
Thanks in advance.
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);
开发者_如何学C /* 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();
}
};
}
First off, this is sort of redundant as the Android Market automaticaly notifies the user of an update, but, in order to do this you will need a server, place a txt file on that server that contains the information your code looks for, in this case your code looks for a version code. That code is here:
int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode;
int newVersion = Integer.valueOf(s);
Your app will then have to compair the two together and if the newVersion is greater than the curVersion open the market to the page of your app where the user can update it.
The code you posted did all this, but to more specifically answer your question in the url you need to put the url to your "currentVersion.txt file" or what ever the file name is.
Hope I helped!
- The actual URL doesn't matter as long as you can have a file served from there.
- Looking at the code, the file itself needs to be a text/plain ( .txt ) file containing the current versionCode of your application with no trailing newline.
I found this code very useful but required versionName so minor updates could be checked for (i.e. 1.01) Altered checkUpdate:
/* Get current Version Number */
String curVersionStr = getPackageManager().getPackageInfo("package id", 0).versionName;
float curVersion = Float.parseFloat(curVersionStr);
float newVersion = Float.parseFloat(s);
Hope this helps someone.
精彩评论