Android进阶之路 - 版本检测且跳转APP商店详情页

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20451879/article/details/79971978

写项目,版本升级这种功能用到的地方太多了~

一般有俩种升级方式:

Effect(分俩种情况,一种是当前手机的应用商店有我们的app,另外一种情况反之):

  • 应用商店有我们的app(机型:红米5 Plus)

这里写图片描述

  • 应用商店没我们的app(机型:魅蓝E2)

这里写图片描述

方法归纳

  • 获取跳转意图
 /**
     * 获取跳转意图
     * @param context 上下文
     * @return 意图
     */
    public static Intent getIntent(Context context) {
        StringBuilder localStringBuilder = new StringBuilder().append("market://details?id=");
        String str = context.getPackageName();
        localStringBuilder.append(str);
        Uri localUri = Uri.parse(localStringBuilder.toString());
        return new Intent("android.intent.action.VIEW", localUri);
    }
  • 判断是否有应用市场
  /**
     * 判断是否有应用市场
     * @param context 上下文
     * @param marketIntent 跳转意图
     * @return 是否有应用市场true/false
     */
    public static boolean judge(Context context, Intent marketIntent) {
        List<ResolveInfo> localList = context.getPackageManager().queryIntentActivities(marketIntent, PackageManager.GET_RESOLVED_FILTER);
        if ((localList != null) && (localList.size() > 0)) {
            return false;
        } else {
            return true;
        }
    }
  • 跳转到应用详情页
/**
     * 跳转到应用详情页
     * @param activity 上下文
     */
    private static void toMarket(Activity activity) {
        Intent intent = getIntent(activity);
        boolean b = judge(activity, intent);
        if (!b) {
            try {
                activity.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Log.e("tag", "有应用市场,但是跳转失败");
            }
        } else {
            Log.e("tag", "手机当前没有应用市场");
        }
    }

完整代码

MainActivity

package com.example.yongliu.versionupdata;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.debug.hv.ViewServer;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView mUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewServer.get(this).addWindow(this);

        mUpdate = findViewById(R.id.tv_update);

        mUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toMarket(MainActivity.this);
                Toast.makeText(MainActivity.this, "被点击了", Toast.LENGTH_SHORT).show();
            }
        });

    }

    /**
     * 获取跳转意图
     * @param context 上下文
     * @return 意图
     */
    public static Intent getIntent(Context context) {
        StringBuilder localStringBuilder = new StringBuilder().append("market://details?id=");
        String str = context.getPackageName();
        localStringBuilder.append(str);
        Uri localUri = Uri.parse(localStringBuilder.toString());
        return new Intent("android.intent.action.VIEW", localUri);
    }

    /**
     * 判断是否有应用市场
     * @param context 上下文
     * @param marketIntent 跳转意图
     * @return 是否有应用市场true/false
     */
    public static boolean judge(Context context, Intent marketIntent) {
        List<ResolveInfo> localList = context.getPackageManager().queryIntentActivities(marketIntent, PackageManager.GET_RESOLVED_FILTER);
        if ((localList != null) && (localList.size() > 0)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 跳转到应用详情页
     * @param activity 上下文
     */
    private static void toMarket(Activity activity) {
        Intent intent = getIntent(activity);
        boolean b = judge(activity, intent);
        if (!b) {
            try {
                activity.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Log.e("tag", "有应用市场,但是跳转失败");
            }
        } else {
            Log.e("tag", "手机当前没有应用市场");
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yongliu.versionupdata.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="版本检测"
        android:id="@+id/tv_update"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

借鉴文章:
Android跳转到应用商店详情页面

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页