zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

【Google Play】从 Android 应用中跳转到 Google Play 中 ( 跳转代码示例 | Google Play 页面的链接格式 | Google Play 免安装体验 )

2023-09-27 14:28:11 时间

前言


本博客参考资料





一、从 Android 应用跳转到 Google Play 代码



首先 , 创建 Intent , 设置其 Action 为 “android.intent.action.VIEW” ,

	/**
	 * 活动操作:向用户显示数据。这是最常见的
	 * 对数据执行的操作——这是可以对数据执行的通用操作
	 * 获取最合理事件发生的一段数据。例如
	 * 在联系人条目上使用时,它将查看该条目;当在机器上使用时
	 * mailto:URI 它将弹出一个充满信息的由URI提供的撰写窗口.
	 * 当与 tel:URI 一起使用时,它将调用拨号器。
	 * <p>输入:{@link#getData}是从中检索数据的URI。
	 * <p>输出:无。
	 */
    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
    public static final String ACTION_VIEW = "android.intent.action.VIEW";

然后 , 设置 Uri 操作数据 , 该 Uri 就是对应的 Google Play 页面链接 ;

	/**
	 * 设置此意图操作的数据。这种方法是自动的
	 * 清除以前由{@link#setType} 或
	 * {@link#setTypeAndNormalize} 设置的内容。
	 * 
	 * <p><em>注意:Android框架中的方案匹配是
	 * 区分大小写,与正式RFC不同。因此
	 * 您应该始终使用小写方案编写Uri,
	 * 或者使用{@linkuri#normalizeScheme}或者
	 * {@link#setDataAndNormalize}
	 * 确保方案转换为小写</em>
	 * 
	 * @param data 此意图现在针对的数据的Uri。
	 * 
	 * @return 返回相同的Intent对象,用于链接多个调用
	 * 变成一句话。
	 * 
     * @see #getData
     * @see #setDataAndNormalize
     * @see android.net.Uri#normalizeScheme()
	 */
    public @NonNull Intent setData(@Nullable Uri data) {
        mData = data;
        mType = null;
        return this;
    }

最后 , 设置一个明确的应用程序包名 , 设置 “com.android.vending” 包名可以在跳转后直接显示 Google Play 对应的页面 , 不再弹出选择器 ; 否则打开时 , 会显示应用的选择器 ;

	/**
	 * (通常是可选的)设置一个明确的应用程序包名称,该名称限制此意图将解析到的组件。
	 * 如果保留默认值null,则将考虑所有应用程序中的所有组件。
	 * 如果非null,则意图只能匹配给定应用程序包中的组件。
	 * 
	 * @param packageName要处理的应用程序包的名称
	 * 或null以允许任何应用程序包。
	 * 
	 * @return返回相同的Intent对象,用于链接多个调用
	 * 变成一句话。
	 * 
	 * @see#getPackage
	 * @see#resolveActivity
	 */
    public @NonNull Intent setPackage(@Nullable String packageName) {
        if (packageName != null && mSelector != null) {
            throw new IllegalArgumentException(
                    "Can't set package name when selector is already set");
        }
        mPackage = packageName;
        return this;
    }

代码示例 :

  • Kotlin :
val intent = Intent(Intent.ACTION_VIEW).apply {
    data = Uri.parse(
            "https://play.google.com/store/apps/details?id=com.example.android")
    setPackage("com.android.vending")
}
startActivity(intent)
  • Java :
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
        "https://play.google.com/store/apps/details?id=com.example.android"));
intent.setPackage("com.android.vending");
startActivity(intent);

上述代码来自 链接到 Google Play - 从 Android 应用提供链接 地址 ;





二、Google Play 页面的链接格式



Google Play 页面的链接格式 :

  • 应用的商品详情页面 :
https://play.google.com/store/apps/details?id=<package_name>
http://play.google.com/store/apps/details?id=com.google.android.apps.maps
  • 指定开发者页面 : 5700313618786177705 是指定的开发者 id ;
https://play.google.com/store/apps/dev?id=<developer_id>
https://play.google.com/store/apps/dev?id=5700313618786177705
  • 搜索结果页面 : 搜索 maps 内容 , c=apps 表示只在 Google Play 中搜索应用 ;
https://play.google.com/store/search?q=<query>
http://play.google.com/store/search?q=maps&c=apps
https://play.google.com/store/apps/collection/<collection_name>
http://play.google.com/store/apps/collection/topselling_free
https://play.google.com/store/apps/details?id=<package_name>&launch=true




三、Google Play 免安装体验



参考 Google Play Instant 文档 ;