LoginSignup
10
18

More than 5 years have passed since last update.

最新バージョンか判断する

Last updated at Posted at 2015-01-05

marketでアプリのバージョンが上がった時に1度だけアラートを出して最新バージョンへアップデートを促す事がしたかったので、データ保存機能を使ってみました。

MainActivity.java

public class MainActivity extends Activity {

//アプリID 仮
    private static final String APP_ID = "xxxxxx";


@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//アラートを出すのでここでnewしておく
        alertDialog = new AlertDialog.Builder(this);

//現在インストールされているアプリのバージョンを調べる
PackageInfo packInfo = null;
        try {
            packInfo = this.getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        currentVersion = packInfo.versionName;


//androidqueryを利用してmarketからアプリの情報を取得する
URL url = null;
        try {
            url = new URL("https://androidquery.appspot.com/api/market?app=" + APP_ID + "&locale=ja");
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return;
        }
new HttpGetTask().execute(url);


}

public class HttpGetTask extends AsyncTask<URL, Void, String> {
        // HttpURLConnectionを使ったデータ取得 (バックグラウンド)
        @Override
        protected String doInBackground(URL... url) {
            String result = "";
            HttpURLConnection urlConnection = null;
            try {
                urlConnection = (HttpURLConnection) url[0].openConnection();
                result = IOUtils.toString(urlConnection.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
            return result;
        }

        // データ取得結果
        @Override
        protected void onPostExecute(String response) {
            try {
                //keyがある時、最新バージョンとkeyを比べる。
                JSONObject rootObject = new JSONObject(response);
                String latestV = rootObject.getString("version");

                SharedPreferences getpref = getSharedPreferences("pref", MODE_PRIVATE);
                String appLatestVersion = getpref.getString("version", "");
                SharedPreferences.Editor e = getpref.edit();

// latestVが最新のversion
// currentVersionが現在のversion
// appLatestVersionがデータ保存しているversion
                if (latestV.equals(currentVersion)){
                    //最新の時
                } else {
                    //最新が入っていない時
                    if(latestV.equals(appLatestVersion)) {
                        //最新バージョンとprefに保存しているバージョンが同じ時。バージョンアップのタイミングで1度通知を見ている。

                    } else {
                        //最新バージョンとprefに保存しているバージョンが違う時。
                        //最新のバージョンをprefに保存する
                        e.putString("version", latestV).commit();


                        alertDialog.setTitle("お知らせ");
                        alertDialog.setMessage("最新バージョンが入手可能です。アップデートしますか?");

                        alertDialog.setPositiveButton("アップデート", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {

                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_VIEW);
                                intent.setData(Uri.parse("market://details?id=" + APP_ID));
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);

                            }
                        });
                        alertDialog.setNegativeButton("後で", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {


                            }
                        });

                        // ダイアログの作成と表示
                        alertDialog.create();
                        alertDialog.show();
                    }
                }

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

アプリIDの見方
id=の所になります。

10
18
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
18