2013-08-05 40 views
8

我想在应用打开时检查Google Play上的应用版本。如果应用的版本高于安装的应用,我想通知用户更新应用。我发现从here的“android-query”jar,在这个我不能动态检查版本,我想设置Major,Minor或者Revision。任何人都请帮助我,我该怎么办?Android应用内版本检查

在此先感谢

+0

嗨乔纳森莱因哈特, 对不起,我真的不明白。你能解释我吗? – Sathish

+0

这一直是由应用程序甚至谷歌自己的应用程序完成。有时,后端服务的更新方式与旧版本的应用程序不再兼容。因此,如果您不想支持3年前的版本,可能需要在您的应用中执行此操作,因为有些人从未更新过任何内容。 –

+0

在这里找到 http://stackoverflow.com/questions/7298106/android-app-check-for-latest-app-version – 2014-09-03 07:12:56

回答

9

基本上,你应该检查最新版本在市场上的应用程序和设备上的应用程序的版本,并决定是否有可用的更新。这样做,请试试这个:

你应该用这个获得最新的版本(在设备上的应用程序的版本):

private String getCurrentVersion(){ 
PackageManager pm = this.getPackageManager(); 
PackageInfo pInfo = null; 

     try { 
      pInfo = pm.getPackageInfo(this.getPackageName(),0); 

     } catch (PackageManager.NameNotFoundException e1) { 
      e1.printStackTrace(); 
     } 
     String currentVersion = pInfo.versionName; 

     return currentVersion; 
    } 

,并使用这个在谷歌获得最新版本的播放(从https://stackoverflow.com/a/32942785/444991拍摄):

private class GetLatestVersion extends AsyncTask<String, String, String> { 
String latestVersion; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     try { 
      //It retrieves the latest version by scraping the content of current version from play store at runtime 
      String urlOfAppFromPlayStore = "https://play.google.com/store/apps/details?id= your app package address"; 
      Document doc = Jsoup.connect(urlOfAppFromPlayStore).get(); 
      latestVersion = doc.getElementsByAttributeValue("itemprop","softwareVersion").first().text(); 

     }catch (Exception e){ 
      e.printStackTrace(); 

     } 

     return latestVersion; 
    } 
} 

然后,您的应用程序启动时,检查他们对每个人是这样的:

String latestVersion = ""; 
     String currentVersion = getCurrentVersion(); 
     Log.d(LOG_TAG, "Current version = " + currentVersion); 
     try { 
      latestVersion = new GetLatestVersion().execute().get(); 
      Log.d(LOG_TAG, "Latest version = " + latestVersion); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      e.printStackTrace(); 
     } 

     //If the versions are not the same 
     if(!currentVersion.equals(latestVersion)){ 
      final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("An Update is Available"); 
      builder.setPositiveButton("Update", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        //Click button action 
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=your app package address"))); 
        dialog.dismiss(); 
       } 
      }); 

      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        //Cancel button action 
       } 
      }); 

      builder.setCancelable(false); 
      builder.show(); 
     } 

并显示用户更新对话框。但要确保你已经导入了jsoup库。

超:对于导入jsoup库,请按照下列步骤操作:

1转到文件菜单
2-项目结构
3-左侧点击应用
4-选择依赖标签
5 - 点击+
6-点击库的依赖
7搜索 “jsoup”
8选择org.jsoup:jsoup,然后点击确定

+0

有什么方法可以不使用jsoup lib \ –

+0

是的,有,但这种方法是现代和直接的。为了添加jsoup,你甚至不需要找到jar文件。你只需打开Android工作室进入文件菜单,按照我的8个步骤,然后你可以通过只搜索名称jsoup添加jsoup。这是1,2,3方法:)请让我知道如果你需要更多的解释。 – Mohammad

+0

未来,ddo不会从没有明确归属的地方复制内容。它被视为剽窃。请参阅http://stackoverflow.com/help/referencing – Matt

5

如果你不想使用像Jsoup库,你可以使用这样的事情得到谷歌当前的版本号玩:

import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.net.MalformedURLException; 
import java.net.URL; 

public class StackAppVersion { 

    public static void main(String[] args) { 
     try { 
      System.out.println(currentVersion()); 
     } catch (IOException ex) { 
      System.out.println("Failed to read Google Play page!"); 
     } 
    } 

    private static String currentVersion() throws IOException { 
     StringBuilder sb = new StringBuilder(); 

     try (Reader reader = new InputStreamReader(
       new URL("https://play.google.com/store/apps/details?id=com.stackexchange.marvin&hl=en") 
         .openConnection() 
         .getInputStream() 
       , "UTF-8" 
     )) { 
      while (true) { 
       int ch = reader.read(); 
       if (ch < 0) { 
        break; 
       } 
       sb.append((char) ch); 
      } 
     } catch (MalformedURLException ex) { 
      // Can swallow this exception if your static URL tests OK. 
     } 

     String parts[] = sb.toString().split("softwareVersion"); 

     return parts[1].substring(
       parts[1].indexOf('>') + 1, parts[1].indexOf('<') 
     ).trim(); 
    } 
} 

如果在保持“& HL = EN”网址,UTF-8字符编码应该没问题。