私のアプリが開いているときにGoogle Playでアプリのバージョンを確認したいと思います。 Appのバージョンがインストールされているアプリのバージョンより高い場合は、アプリを更新するようユーザーに通知したいと考えています。私は "android-query" jarをhereから見つけましたが、これでバージョンを動的にチェックすることはできません。メジャー、マイナー、リビジョンを設定すると思います。誰でも助けてどうすればいいですか?Androidアプリ内バージョンチェック
答えて
基本的には事前に
おかげで、あなたはあなたの市場でのアプリやデバイス上のアプリのバージョンの最新バージョンを確認し、利用可能なすべてのアップデートがあるかどうかを決定する必要があります。 (
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;
}
とGoogleのプレイで、最新バージョンを取得するためにこれを使用して:あなたは、現在のバージョン(デバイス上のアプリのバージョン)を取得するためにこれを使用する必要があります
:これを行うために、これを試してみてください)https://stackoverflow.com/a/32942785/444991から撮影:
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();
}
:
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;
}
}
次に、あなたのアプリケーションの起動時に、このようなお互いに対してそれらをチェック
ユーザーの更新ダイアログを表示します。しかし、すでにjsoupライブラリをインポートしていることを確認してください。
エクストラ:メニュー
2 - プロジェクト構造
アプリ上の3-左側クリック
、4-選択して依存関係]タブ
5をファイルに
の1- GO:ライブラリをjsoupインポートする場合は、次の手順に従ってください - ライブラリの依存関係の上に+
、6-クリックして上のクリック
、7-検索 "jsoup"
8 org.jsoup選択:jsoupをし、[OK]をクリックし
にはjsoup lib \ –
を使用しない方法があります。はい、ありますが、このアプローチは現代的で簡単です。 jsoupを追加するためには、jarファイルを見つける必要さえありません。あなたはAndroidのスタジオを開くだけでファイルメニューに行き、私の8つのステップに従ってください、そして、名前jsoupを検索するだけでjsoupを追加することができます。 1、2、3のアプローチです:)詳しい説明が必要な場合はお知らせください。 – Mohammad
将来、ddoは明確な帰属なしで他の場所からコンテンツをコピーしません。それは盗作とみなされます。 http://stackoverflow.com/help/referencingを参照してください。 – Matt
あなたは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();
}
}
あなたはに「& HL = EN」を維持する場合UTF-8の文字エンコーディングはOKです。
こんにちは、Jonathon Reinhart、 申し訳ありませんが、私は本当に理解していません。あなたは私を説明できますか? – Sathish
これは、アプリでもGoogleの独自のアプリでも常に行われます。バックエンドサービスは、野生の古いバージョンのアプリとの互換性がなくなるように更新されることがあります。このため、3年前のバージョンをサポートしたくない場合は、アプリでこれを行う必要があるかもしれません。 –
は私が正しくあなたの質問に答えた場合、私は疑問に思うここに解決策 http://stackoverflow.com/questions/7298106/android-app-check-for-latest-app-version –