Android跳转到应用商店详情页面(更全面)

2018-07-04  本文已影响0人  kuwork

换到新的公司有一段日子,最近有个新需求,优先跳到OS的应用市场的详情页,引导用户进行评价。
这里囊括了大部分的国内市场,如果没有(有可能是包名更新了),最终采用原始的方式,让用户选择。
(fixed:修复某些老手机会因为找不到任何市场而报错)

/**
* Created by K.W. on 2018/6/11.
*/

public class AppMarketUtils {
   //小米应用商店
   public static final String PACKAGE_MI_MARKET = "com.xiaomi.market";
   public static final String MI_MARKET_PAGE = "com.xiaomi.market.ui.AppDetailActivity";
   //魅族应用商店
   public static final String PACKAGE_MEIZU_MARKET = "com.meizu.mstore";
   public static final String MEIZU_MARKET_PAGE = "com.meizu.flyme.appcenter.activitys.AppMainActivity";
   //VIVO应用商店
   public static final String PACKAGE_VIVO_MARKET = "com.bbk.appstore";
   public static final String VIVO_MARKET_PAGE = "com.bbk.appstore.ui.AppStoreTabActivity";
   //OPPO应用商店
   public static final String PACKAGE_OPPO_MARKET = "com.oppo.market";
   public static final String OPPO_MARKET_PAGE = "a.a.a.aoz";
   //华为应用商店
   public static final String PACKAGE_HUAWEI_MARKET = "com.huawei.appmarket";
   public static final String HUAWEI_MARKET_PAGE = "com.huawei.appmarket.service.externalapi.view.ThirdApiActivity";
   //ZTE应用商店
   public static final String PACKAGE_ZTE_MARKET = "zte.com.market";
   public static final String ZTE_MARKET_PAGE = "zte.com.market.view.zte.drain.ZtDrainTrafficActivity";
   //360手机助手
   public static final String PACKAGE_360_MARKET = "com.qihoo.appstore";
   public static final String PACKAGE_360_PAGE = "com.qihoo.appstore.distribute.SearchDistributionActivity";
   //酷市场 -- 酷安网
   public static final String PACKAGE_COOL_MARKET = "com.coolapk.market";
   public static final String COOL_MARKET_PAGE = "com.coolapk.market.activity.AppViewActivity";
   //应用宝
   public static final String PACKAGE_TENCENT_MARKET = "com.tencent.android.qqdownloader";
   public static final String TENCENT_MARKET_PAGE = "com.tencent.pangu.link.LinkProxyActivity";
   //PP助手
   public static final String PACKAGE_ALI_MARKET = "com.pp.assistant";
   public static final String ALI_MARKET_PAGE = "com.pp.assistant.activity.MainActivity";
   //豌豆荚
   public static final String PACKAGE_WANDOUJIA_MARKET = "com.wandoujia.phoenix2";
   // 低版本可能是 com.wandoujia.jupiter.activity.DetailActivity
   public static final String WANDOUJIA_MARKET_PAGE = "com.pp.assistant.activity.PPMainActivity";
   //UCWEB
   public static final String PACKAGE_UCWEB_MARKET = "com.UCMobile";
   public static final String UCWEB_MARKET_PAGE = "com.pp.assistant.activity.PPMainActivity";

   // 弹起评价弹窗
   public static void showDialog(final Context context) {
       new IntentUtils.Builder(context)
               .to(MarketAlertActivity.class)
               .anim(R.anim.stay, R.anim.stay)
               .build().start();
   }
 
   // 进入应用市场详情页
   public static void gotoMarket(Context context) {
       Intent intent = new Intent(Intent.ACTION_VIEW);
       intent.setData(Uri.parse("market://details?id=" + AppUtils.getPackageName(context)));
       String[] keys = getKeys(context);
       if (keys != null) {
           intent.setClassName(keys[0], keys[1]);
       }
       //修复某些老手机会因为找不到任何市场而报错
      if (AppUtils.isIntentAvailable(context, intent)) {
           context.startActivity(intent);
       } else {
           ToastUitls.show(context, context.getString(R.string.no_market));
       }
   }

   private static String[] getKeys(Context context) {
       String[] keys = new String[2];
       if (AppUtils.isPackageExist(context, PACKAGE_MI_MARKET)) {
           keys[0] = PACKAGE_MI_MARKET;
           keys[1] = MI_MARKET_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_VIVO_MARKET)) {
           keys[0] = PACKAGE_VIVO_MARKET;
           keys[1] = VIVO_MARKET_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_OPPO_MARKET)) {
           keys[0] = PACKAGE_OPPO_MARKET;
           keys[1] = OPPO_MARKET_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_HUAWEI_MARKET)) {
           keys[0] = PACKAGE_HUAWEI_MARKET;
           keys[1] = HUAWEI_MARKET_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_ZTE_MARKET)) {
           keys[0] = PACKAGE_ZTE_MARKET;
           keys[1] = ZTE_MARKET_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_COOL_MARKET)) {
           keys[0] = PACKAGE_COOL_MARKET;
           keys[1] = COOL_MARKET_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_360_MARKET)) {
           keys[0] = PACKAGE_360_MARKET;
           keys[1] = PACKAGE_360_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_MEIZU_MARKET)) {
           keys[0] = PACKAGE_MEIZU_MARKET;
           keys[1] = MEIZU_MARKET_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_TENCENT_MARKET)) {
           keys[0] = PACKAGE_TENCENT_MARKET;
           keys[1] = TENCENT_MARKET_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_ALI_MARKET)) {
           keys[0] = PACKAGE_ALI_MARKET;
           keys[1] = ALI_MARKET_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_WANDOUJIA_MARKET)) {
           keys[0] = PACKAGE_WANDOUJIA_MARKET;
           keys[1] = WANDOUJIA_MARKET_PAGE;
       } else if (AppUtils.isPackageExist(context, PACKAGE_UCWEB_MARKET)) {
           keys[0] = PACKAGE_UCWEB_MARKET;
           keys[1] = UCWEB_MARKET_PAGE;
       }
       if (TextUtils.isEmpty(keys[0])) {
           return null;
       } else {
           return keys;
       }
   }
}

AppUtils 相关方法:

/**
     * 获取app包名
     *
     * @return 返回包名
     */
    public static String getPackageName(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(),
                    0);
            return info.packageName;
        } catch (NameNotFoundException e) {
            return null;
        }
    }


    /**
     * @param context
     * @param packageName
     * @return
     * @Title isPackageExist
     * @Description .判断package是否存在
     * @date 2013年12月31日 上午9:49:59
     */
    public static boolean isPackageExist(Context context, String packageName) {
        PackageManager manager = context.getPackageManager();
        Intent intent = new Intent().setPackage(packageName);
        List<ResolveInfo> infos = manager.queryIntentActivities(intent,
                PackageManager.GET_INTENT_FILTERS);
        if (infos == null || infos.size() < 1) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 检测 响应某个Intent的Activity 是否存在
     * @param context
     * @param intent
     * @return
     */
    @SuppressLint("WrongConstant")
    public static boolean isIntentAvailable(Context context, Intent intent) {
        final PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                PackageManager.GET_ACTIVITIES);
        return list.size() > 0;
    }

PS: 如果没有在相应的市场上线,需要相应屏蔽一些IF判断,如果该市场没有这个APP,一般会进入空白页。

上一篇下一篇

猜你喜欢

热点阅读