Question for beginners in checking for updates for Android Android

I am trying to use the code below. Suppose he checks the new version of my application once a day when the application starts. Got the code from http://www.androidsnippets.com/check-for-updates-once-a-day . The thing I canโ€™t understand is in the URL URLURL = new URL ("http://my.company.com/update"); What should be http://mycompany.com/update ? File xml, txt or ... I tried the xml and txt file with the latest version code and named it version.txt, versionCode.txt and .xml.

It just doesnโ€™t work and there should only be a URL โ€œhttp://mycompany.com/updateโ€ or with an extension, i.e. "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); /* 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(); } }; 

}

+4
source share
3 answers

Firstly, this is a kind of redundancy, since the Android Market automatically notifies the user about the update, but for this you need a server, put the txt file on this server containing the information that your code looks like since in this case your code is looking for the version code. This code is here:

 int curVersion = getPackageManager().getPackageInfo("your.app.id", 0).versionCode; int newVersion = Integer.valueOf(s); 

Then your application will have to put these two together, and if newVersion is bigger than curVersion, open the market on the page of your application where the user can update it.

The code you posted did all this, but for a more specific answer to your question in the url, you need to put the url in your "currentVersion.txt file" or what ever was the file name.

I hope I helped!

+4
source
  • The actual URL does not matter as long as you have the file to be there from there.
  • If you look at the code, the file itself should be a text / open (.txt) file containing the current version code of your application without a final new line.
+1
source

I found this code to be very useful, but it requires the name versionName, for which minor updates can be checked (for example, 1.01) Modified 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.

+1
source

Source: https://habr.com/ru/post/1342511/


All Articles