2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

強制アップデートが実装できなかった話

Posted at

書いてあること

  1. 強制アップデートができなかった理由
  2. 直接プレイストアからバージョンを取得する方法(代替案)

強制アップデートができなかった理由

  • 内部テストで作成したapkを実機にインストールしたときに、
    ダイアログが出るときと出ない時があった。
  • この事象の規則性を見つけることができなかった。
  • 唯一言えることは、内部テストにapkをアップしてから13時間経過したものは
    ダイアログが表示される場合が多かった。

直接プレイストアからバージョンを取得する方法(代替案)

MainActivity.java
public class MainActivit {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // httpリクエストを入れる変数
        Uri.Builder builder = new Uri.Builder();
        AsyncHttpRequest task = new AsyncHttpRequest(this);
        //ストアのアプリのバージョンを調べる
        task.execute(builder);
}
AsyncHttpRequest.java
//非同期通信クラスはわけないとエラーになる
public class AsyncHttpRequest extends AsyncTask<Uri.Builder, Void, String> {
    /**
     * 呼び出し元のActivity
     */
    private Activity mainActivity;
    /**
     * ストアのアプリのバージョン
     */
    private String newVersion;
    /**
     * 共有リソース
     */
    private SharedPreferences sharedPreferences;

    public AsyncHttpRequest(Activity activity) {
        // 呼び出し元のアクティビティ
        this.mainActivity = activity;
    }

    @Override
    protected String doInBackground(Uri.Builder... builder) {
        // httpリクエスト投げる処理
        this.newVersion = "";
        String lang = getLocaleLang();
        try {
            //通信時の情報は定数クラスにまとめている
            this.newVersion = Jsoup.connect(PlayStoreConstants.STORE_BASE_URL + PlayStoreConstants.STORE_ID + "&locale=" + lang)
                    .timeout(PlayStoreConstants.TIMEOUT)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer(PlayStoreConstants.REFERRER)
                    .get()
                    .select(PlayStoreConstants.CSS_QUERY)
                    .get(7)
                    .ownText();
            //ストアのバージョンを保存してActivityで利用している
            sharedPreferences = this.mainActivity.getSharedPreferences("version", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("storeVersion", this.newVersion);
            editor.apply();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this.newVersion;
    }
   /**
     * 端末の言語を取得するメソッド
     */
    public String getLocaleLang() {
        String localeLanguage = Locale.getDefault().getLanguage();
        return localeLanguage;
    }
}
PlayStoreConstants.java
/**
 * プレイストア通信定数クラス
 */
public class PlayStoreConstants {
    /**
     * ストアURL
     */
    public static final String STORE_BASE_URL = "https://play.google.com/store/apps/details?id=";
    /**
     * ストアID
     */
    //https://play.google.com/store/apps/details?id=hogeのhogeの部分
    public static final String STORE_ID = "hoge";
    /**
     * ストア詳細画面URL
     */
    public static final String STORE_DETAIL_URL = "market://details?id=";
    /**
     * ストア通信タイムアウト時間
     */
    public static final int TIMEOUT = 30000;
    /**
     * リファラーヘッダー
     * (HTTP 403エラーを防ぐため)
     */
    public static final String REFERRER = "http://www.google.com";
    /**
     * CSSクエリー
     */
    public static final String CSS_QUERY = ".hAyfc .htlgb";
}
2
1
1

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?