2014-02-20 104 views
1

嗨Ive得到,即时通讯试图添加在更新一个内置的应用程序。该应用程序包含一个按钮,从我的服务器下载apk文件。服务器上的文件名命名如下:最新文件版本

“的App-1.apk”, “的App-2.apk”, “的App-3.apk” ...

按钮目前的设置是这样的:

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); 
    } 
}); 

什么是检查服务器上最高可用应用程序版本并选择一个下载的好方法?

编辑:我如何使用Java来检查服务器目录编号最高的应用程序?

EDIT2:下面有什么,我终于实现了。不是最好的解决办法,但不够好,现在:

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(); 
     } 
    } 
}); 
+0

您是否询问将在服务器上执行的代码? – NormR

+0

不,我正在寻找应用程序内的Java解决方案。我不打算对服务器做任何修改或者发生任何事情。 – Noob

+0

服务器可以发送应用程序可以扫描的文件列表吗? – NormR

回答

1

您可以添加代码的版本,或在清单中的应用版本,你可以在这里看到:

<?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"> 

你可以检查一下:

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

可以设置的代码名称数目DB表您的服务器上,然后检查该字段来更新您的应用。

你可以尝试这样的事情:

通到服务器这样的请求:youServer/apkUpdate?versionCode={versionCode}

,并在你的服务器,你使用这样的方法:

@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); 

} 

其中API更新服务是:

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(); 
      } 
     } 
    } 
} 
+0

但是,我将如何检查服务器上的文件的版本号是什么? – Noob

+0

我编辑我的答案,有一个更好的方法,或者我希望它是更好的.. – Teo

+0

@Teo我已经看到了这一代码..也许ASCENSORI? ahahaha – gipinani

0

例如,您可以:

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 

在服务器端,你可以这样做:

int installedVersion = ... 
int latestVersion = ... 
if(installedVersion < latestVersion){ 
//return file 
}else{ 
//Return message: you have the latest 
}