Recently I need to do the application version update function, because I have written a version update function before, although the request interface is still used HttpUrlConnection, I want to change the request mode used by the application is also quite fast, I began to like it, I can steal the job, hahaha.

Implementation of the time I found that I want to much, before the article is in the application version update, and now need is to jump to the application market version update, this is a face meng force, before really did not do so, there is no way, can only look up the information, found that it is very simple, but also can save a lot of processing.

There are now two ways to handle version updates:

(1) Jump to the App application market (such as App Treasure, Wandoujia, etc.), download, update and install through the App market.

(2) Download Apk in App, update and install after downloading.

I’ve written about the second approach before, and although it’s a little rough, it’s a pretty good process.

This is going to be done in the first way, walking.

Implementation idea:

1. Request background data and check whether the application needs to be updated based on the returned version number.

2. If no, go to the login or application home screen. If you need to click a dialog box, let the user select whether to update the application.

3. If you cancel the update, the login or application main screen is displayed

4. If the user chooses to update, it will determine whether the mobile phone is installed with the APP market APP to be accessed.

5. If the package has been installed, go to the details page of the app Market to download the APK.

6. If it is not installed, open the browser to download it according to the address returned in the background.

Key steps code description:

1. Check whether the APP market to be accessed is installed in the user’s mobile phone

** @param context * @param packageName ** main app store corresponding packageName * com.android.vending -----Google Play * Com. Tencent. Android. Qqdownloader application -- -- -- -- -- treasure * com in qihoo. Appstore -- -- -- -- -- 360 mobile phone assistant * com in baidu. Appsearch -- -- -- -- -- baidu mobile phone * Com.xiaomi. Market ----- xiaomi app store * com.wandoujia. Phoenix2 ----- wandoujia * com.huawei.appmarket ----- huawei appmarket * Com.taobao. Appcenter ----- Taobao mobile assistant * com.hiapk.marketpho ----- android market * cn.goapk.market ----- android market */ public static Boolean isAvilible(Context context, String packageName) {/ / get packagemanager final packagemanager packagemanager = context. GetPackageManager (); / / get all installed application package information List < PackageInfo > pinfo. = packageManager getInstalledPackages (0); List<String> pName = new ArrayList<String>(); // Fetch the package name from pinfoif(pinfo ! = null) {for(int i = 0; i < pinfo.size(); i++) { String pf = pinfo.get(i).packageName; pName.add(pf); }} // Check whether pName contains the package name of the target programtrue, there is nofalse
        return pName.contains(packageName);
    }
Copy the code

2. Access the app store details page to download the APK package

/** * @param appPkg Package name of the target app * @param marketPkg Package name of the app store, if""*/ public static void launchAppDetail(Context mContext, String appPkg, String marketPkg) { try {if (TextUtils.isEmpty(appPkg)) {
                return;
            }

            Uri uri = Uri.parse("market://details? id=" + appPkg);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (!TextUtils.isEmpty(marketPkg)) {
                intent.setPackage(marketPkg);
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Copy the code

3. If the market application is not installed, open the browser to download it according to the address returned in the background

  Uri uri = Uri.parse(url);
  Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  startActivity(intent);
Copy the code

That’s basically it. It’s easier than updating in the app.


The following is our personal public account (LongXuanzhigu). Our posts will be synchronized to this account, which is convenient for exchanging and learning Android knowledge and sharing personal articles. If you have any questions, please leave a message: