当我的应用程序打开时,我想在Google Play上查看应用程序版本.如果App的版本高于已安装的应用程序,我想通知用户更新应用程序.我从这里发现了"android-query"jar ,在此我无法动态检查版本,我想设置Major,Minor或Revision.有人请帮帮我怎么办?
提前致谢
Vah*_*hid 13
基本上,您应该检查市场中应用程序的最新版本以及设备上的应用程序版本,并确定是否有可用的更新.为此,请尝试这样做:
您应该使用它来获取当前版本(设备上的应用程序版本):
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;
}
Run Code Online (Sandbox Code Playgroud)
并使用它来获取Google Play中的最新版本(取自/sf/answers/2305994981/):
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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,当您的应用启动时,请检查彼此,如下所示:
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();
}
Run Code Online (Sandbox Code Playgroud)
并显示用户更新对话框.但请确保您已导入jsoup库.
额外:要导入jsoup库,请按照下列步骤操作:
1-转到文件菜单
2-项目结构
3-左侧单击应用程序
4-选择依赖项选项卡
5-单击+
6-单击库依赖项
7-搜索"jsoup"
8-选择org.jsoup:jsoup并单击确定
如果您不想使用像Jsoup这样的库,可以使用类似的东西从Google Play获取当前版本号:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
如果在URL中保留"&hl = en",则UTF-8的字符编码应该没问题.
| 归档时间: |
|
| 查看次数: |
7933 次 |
| 最近记录: |