Android进阶之路 - 跳转应用商店下载、更新app

写项目时,遇到版本升级、更新功能的地方太多了~ 针对不同的需求方,有的在项目内实时下载安装,有的则直接跳转应用商店让用户自行下载 ~

版本更新、升级方式

实现效果

Effect

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

  • 应用商店有我们的app(机型:红米5 Plus)
    这里写图片描述
  • 应用商店没我们的app(机型:魅蓝E2)
    这里写图片描述
方法归纳
跳转到应用商店 - 应用详情页
	/**
     * 跳转到应用详情页
     * @param activity 上下文
     */
    private static void toMarket(Activity activity) {
    	//获取应用详情
        Intent intent = getIntent(activity);
        //判断是否存在应用市场
        boolean b = judgeMarket(activity, intent);
        if (!b) {
            try {
                activity.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Log.e("tag", "有应用市场,但是跳转失败");
            }
        } else {
            Log.e("tag", "手机当前没有应用市场");
        }
    }
获取应用详情
 /**
     * 获取跳转意图
     * @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 judgeMarket(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;
        }
    }
完整实践

·关于上半部分主要借鉴了 这儿 的跳转方式 ~

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 judgeMarket(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 = judgeMarket(activity, intent);
        if (!b) {
            try {
                activity.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Log.e("tag", "有应用市场,但是跳转失败");
            }
        } else {
            Log.e("tag", "手机当前没有应用市场");
        }
    }
}

activity_main

<?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>
2021年 新增方法
判断软件是否存在
    /**
     * 判断软件是否存在(应用市场本质也是app)
     */
    public static boolean isExist(Context context, String packageName) {
        try {
            context.getPackageManager().getPackageInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
启动到app详情界面
    /**
     * 启动到app详情界面
     */
    public static void startAppStore(Context context, 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);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
跳转到应用商店下载app
    /**
     * 跳转到应用商店下载app
     *
     * @param appPkg    跳转包名(项目包名)
     * @param marketPkg 应用商店包名
     * 一般应用商店的包名以market结尾
     */
    public void goThirdApp(Context context, String appPkg, String marketPkg) {
        // 应用市场-存在
        if (isExist(context, marketPkg)) {
            startAppStore(context, appPkg, marketPkg);
        } else {
            //应用市场- 不存在(以下这行代码借鉴自别人,酌情使用!)
            Uri uri = Uri.parse("http://a.app.qq.com/o/simple.jsp?pkgname=" + "项目包名");
            Intent it = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(it);
        }
    }
跳转应用商店 - app详情页(完整示例)
    /**
     * 跳转到应用商店下载app
     *
     * @param appPkg    跳转包名(项目包名)
     * @param marketPkg 应用商店包名
     * 一般应用商店的包名以market结尾
     */
    public void goThirdApp(Context context, String appPkg, String marketPkg) {
        // 应用市场-存在
        if (isExist(context, marketPkg)) {
            startAppStore(context, appPkg, marketPkg);
        } else {
            //应用市场- 不存在(以下这行代码借鉴自别人,酌情使用!)
            Uri uri = Uri.parse("http://a.app.qq.com/o/simple.jsp?pkgname=" + "项目包名");
            Intent it = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(it);
        }
    }

    /**
     * 判断软件是否存在(应用市场本质也是app)
     */
    public static boolean isExist(Context context, String packageName) {
        try {
            context.getPackageManager().getPackageInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    /**
     * 启动到app详情界面
     */
    public static void startAppStore(Context context, 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);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

远方那座山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值