1

Hi Ive got an app that im trying to add a built in updater to. The app contains a button that downloads an apk file from my server. The file names on the server are named as follows:

"App-1.apk", "App-2.apk", "App-3.apk" ...

The button is currently setup like this:

download = (Button) findViewById(R.id.bDownload);
versionNum = 3;

download.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent downloadFromServer = new Intent();
        downloadFromServer.setAction(Intent.ACTION_VIEW);
        downloadFromServer.addCategory(Intent.CATEGORY_BROWSABLE);
        downloadFromServer.setData(Uri.parse("http://server.com/Files/App-" + versionNum + ".apk"));
        startActivity(downloadFromServer);
    }
}); 

What would be a good way to check for the highest available app version on the server, and pick that one to download?

Edit: How can I use java to check the server directory for the highest numbered app?

Edit2: Heres what i ended up doing. Not the best solution, but good enough for now:

try {
    PackageInfo appInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    installedVersion = appInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
    // Handle exception
}   

//Latest version available on my server. Must update this value for each new release
latestVersion = 3.1;

//Convert string value of installed version to double so that it can be compared with value of latest version       
installedVersionValue = Double.parseDouble(installedVersion); 

download = (Button) findViewById(R.id.bDownload);

download.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        if (installedVersionValue<latestVersion) { //If latest version available on server is greater than installed version, download the latest
            Intent downloadFromServer = new Intent();
            downloadFromServer.setAction(Intent.ACTION_VIEW);
            downloadFromServer.addCategory(Intent.CATEGORY_BROWSABLE);
            downloadFromServer.setData(Uri.parse("http://server.com/Files//App-" + latestVersion + ".apk"));
            startActivity(downloadFromServer);
        }
        else if (installedVersionValue==latestVersion) { //If user clicks the update button while they already have the latest, let them no what's up
            Toast.makeText(getApplicationContext(), "You are already running the latest version (" + installedVersionValue +")",
            Toast.LENGTH_LONG).show();  
        }
    }
}); 
Noob
  • 534
  • 1
  • 8
  • 16
  • Are you asking about code that will execute on the server? – NormR Feb 20 '14 at 22:12
  • No, im looking for a java solution within the app. I dont plan to make any modifications to the server or have anything happening on its side. – Noob Feb 20 '14 at 22:13
  • Can the server send a list of the files it contains that the app can scan? – NormR Feb 21 '14 at 02:22

3 Answers3

1

You can add the version of code, or the version of your app in manifest, as you can see here:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.package.name"
      android:versionCode="2"
      android:versionName="1.1">

And you can check it:

PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
int versionNumber = pinfo.versionCode;

You can set the number of code as name in a table of db on your server, and then you check that field to update your app.

You can try something like this:

pass to server a request like this: youServer/apkUpdate?versionCode={versionCode}

and in your server you use a method like this:

@RequestMapping(method = RequestMethod.GET)
public void getUpdatedApk(
        @RequestParam(value = "versionCode", required = true) final Integer versionCode,
        @RequestParam(value = "androidVersion", required = false) final Integer androidVersion,
        final HttpServletRequest request, 
        final HttpServletResponse response) throws Exception{

    apkUpdateService.getUpdate(new Long(versionCode), response);

}

Where the api update service is:

public void getUpdate(Long currentVersionCode, HttpServletResponse response) throws Exception {
            //get latest number of apk from a db field
    Long dbVersionCode = apkUpdateRepository.findLastVersion();
    ServletOutputStream servletOutputStream = null;

    if (currentVersionCode == dbVersionCode){
        LOG.info("You have the last version");
        return;
    }

    if (currentVersionCode < dbVersionCode){
        FileInputStream inputStream = null;
        String filename = String.format(pathToApk, dbVersionCode);

        try{
        inputStream = new FileInputStream(filename);


        servletOutputStream = response.getOutputStream();

        byte[] buffer = new byte[1024];
        int bytesRead = 0;

        while ((bytesRead = inputStream.read(buffer)) != -1) {
            servletOutputStream.write(buffer, 0, bytesRead);
        }

        servletOutputStream.flush();
        LOG.info("File streamed");

        LOG.info("Download "+filename);
        }finally{
            if (inputStream!=null){
                inputStream.close();
            }
            if (servletOutputStream != null){
                servletOutputStream.close();
            }
        }
    }
}
Teo
  • 3,143
  • 2
  • 29
  • 59
0

You could for example:

String installedVersion = "";
        try {
            PackageInfo manager = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);
            installedVersion = manager.versionCode + "";
        } catch (PackageManager.NameNotFoundException e) {
            // Handle exception
        }    
"http://server.com/Files/update?installed=" + installedVersion

On the server side you could do something like:

int installedVersion = ...
int latestVersion = ...
if(installedVersion < latestVersion){
//return file
}else{
 //Return message: you have the latest
}
unify
  • 6,161
  • 4
  • 33
  • 34
0

I think the easiest would be checking file existence in ascending order and than downloading the last present one. File existence checking was already answered here: Check if file exists on remote server using its URL

Community
  • 1
  • 1
esoyitaque
  • 61
  • 2