google app版本更新

简介

我们做app开发时,大部分都是要上传到第三方的市场的,那么在上传到像google这种比较特殊的市场的时候,我们在程序内怎么做版本更新判断呢?假如你的app有自己的后台服务,那还可以用后台服务器做版本判断,那对于没有服务的同学来说该怎么办呢?下面我们就来说说这种没有后台服务器的解决办法。

检测版本更新

对于google play市场我们要怎么检测版本更新呢?google 没有提供直接的api或者比较友好的链接获取版本信息,但是它有提供一个app的地址,例如:https://play.google.com/store/apps/details?id=com.xxx.xxx 这里com.xxx.xxx是你应用的包名。那么,我们就可以根据这个地址做文章了,我们直接用浏览器打开这个地址,就得到是APP的详细信息,包括了version版本号。相信到这里大家也知道该怎么做了,直接在代码里请求这个地址,获取里面的内容,并检索出version。哈哈,思路就是这样,看具体实现吧,代码如下:

public class CheckVersionAsyncTask extends AsyncTask<String, Void, Boolean> {

    Context context;
    String currentVersion; // Google Play 上的版本

    String oldVersion;     // 目前的 app 版本


    public CheckVersionAsyncTask(Context context, String version) {
        this.context = context;
        this.oldVersion = version;
    }

    @Override
    protected Boolean doInBackground(String... params) {
        boolean result = false;
        String url = params[0];   //請在 excecute 時傳入你 google play 的 url 位置,不是 market 喔,要 http


        HttpPost post = new HttpPost(url);

        AndroidHttpClient client = AndroidHttpClient.newInstance("android");


        BufferedReader reader = null; 
        try {
            HttpResponse reponse = client.execute(post);

            reader = new BufferedReader(new InputStreamReader(reponse.getEntity().getContent()));
            String line;
            String content = "";
            Pattern p = Pattern.compile("\"softwareVersion\"\\W*([\\d\\.]+)");
            while( ( line = reader.readLine() ) != null ){
                Matcher matcher = p.matcher(line);
                if( matcher.find() ){
                    Log.v("ids", "ver.:" + matcher.group(1));

                    currentVersion = matcher.group(1);
                }
                content += line;
            }
            /*
            * 檢查版本,在此使用的是我們自定的版本名稱 <app version name>,並不是版本號 <app version code>
            * 請特別注意這一點
            */

            if (currentVersion.compareTo(oldVersion) > 0)
                result = true;
            Log.v("ids", content);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            Log.v("ids", "close reader");
            try {
                if( reader != null ) {
                    reader.close();
                }
                if (client != null) {
                    client.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if (result) {
            // 檢查到有新的版本,做下一個動作
            new AlertDialog.Builder(context, R.style.MyDialog).setTitle("Update").setMessage("App have update,please update!").setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface dialog) {
                    dialog.dismiss();
                }
            }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).setPositiveButton("update", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    updateApp();
                }
            }).create().show();
        }
    }

    private void updateApp() {
        String packageName = context.getPackageName();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id="+packageName));
        context.startActivity(intent);
    }
}

链接到Play Store

版本判断的方法我们有了,现在只差链接到Play Store更新APP了,那么怎么链接到Play Store呢?看代码:

private void updateApp() {
        String packageName = context.getPackageName();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id="+packageName));
        context.startActivity(intent);
    }

intent.setData(Uri.parse(“market://details?id=”+packageName));

这就是解决链接的关键,为什么这么做就不多说了。

实现

前面已经把方法都现实了,剩下的就是调用了,我们可以在任意地方调用如下方法获取版本比较,代码如下:

private void checkUpdateApp() {
        try {
            String packageName = getPackageName();
            //因为这个是google的网址,所以请求的时候需要翻墙在能正常请求到数据(注:这个请求要保证本app已经上传到google play)
            String url = "https://play.google.com/store/apps/details?id="+packageName;

            PackageInfo info = getPackageManager().getPackageInfo(packageName, 0);

            new CheckVersionAsyncTask(MainActivity.this, info.versionName).execute(url);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }

好,重要的部分都讲完啦,over~!!!

参考文章

Android 上架至 Google Play 版本更新檢查

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值