android里面给应用评分前提是用户手机已经安装了一款软件商城,同时对应商场上应该有该软件,才能进行跳转评分,下面提供一个案例:
package com.jiutouxiang.util;
/**
* Created by hasee on 2017/2/9.
*/
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.TextUtils;
import com.jiutouxiang.base.MyApplication;
import static com.umeng.socialize.utils.ContextUtil.getPackageName;
/**
* Caige
* <p>
* 2015-11-1
* </p>
* <p>
* 应用商店评分工具类
* </p>
*/
public class MarketUtils {
/**
* 获取已安装应用商店的包名列表
*
* @param context
* @return
*/
public static ArrayList<String> queryInstalledMarketPkgs(Context context) {
ArrayList<String> pkgs = new ArrayList<String>();
if (context == null)
return pkgs;
Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.APP_MARKET");
PackageManager pm = context.getPackageManager();
List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
if (infos == null || infos.size() == 0)
return pkgs;
int size = infos.size();
for (int i = 0; i < size; i++) {
String pkgName = "";
try {
ActivityInfo activityInfo = infos.get(i).activityInfo;
pkgName = activityInfo.packageName;
} catch (Exception e) {
e.printStackTrace();
}
if (!TextUtils.isEmpty(pkgName))
pkgs.add(pkgName);
}
return pkgs;
}
/**
* 过滤出已经安装的包名集合
*
* @param context
* @param pkgs
* 待过滤包名集合
* @return 已安装的包名集合
*/
public static ArrayList<String> filterInstalledPkgs(Context context,
ArrayList<String> pkgs) {
ArrayList<String> empty = new ArrayList<String>();
if (context == null || pkgs == null || pkgs.size() == 0)
return empty;
PackageManager pm = context.getPackageManager();
List<PackageInfo> installedPkgs = pm.getInstalledPackages(0);
int li = installedPkgs.size();
int lj = pkgs.size();
for (int j = 0; j < lj; j++) {
for (int i = 0; i < li; i++) {
String installPkg = "";
String checkPkg = pkgs.get(j);
try {
installPkg = installedPkgs.get(i).applicationInfo.packageName;
} catch (Exception e) {
e.printStackTrace();
}
if (TextUtils.isEmpty(installPkg))
continue;
if (installPkg.equals(checkPkg)) {
empty.add(installPkg);
break;
}
}
}
return empty;
}
/**
* 启动到app详情界面
*
* @param appPkg
* App的包名
* @param marketPkg
* 应用商店包名 ,如果为""则由系统弹出应用商店列表供用户选择,否则调转到目标市场的应用详情界面,某些应用商店可能会失败
* 主流应用商店对应的包名如下:
com.qihoo.appstore 360手机助手
com.taobao.appcenter 淘宝手机助手
com.tencent.android.qqdownloader 应用宝
com.hiapk.marketpho 安卓市场
cn.goapk.market 安智市场
*/
public static void yingyongbao(String appPkg, String marketPkg, Activity activity) {
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);
activity.startActivity(intent);
} catch (Exception e) {
qihu360("com.jiutouxiang","com.qihoo.appstore",activity);
}
}
public static void qihu360(String appPkg, String marketPkg, Activity activity) {
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);
activity.startActivity(intent);
} catch (Exception e) {
taobaozhushou("com.jiutouxiang","com.taobao.appcenter",activity);
}
}
public static void taobaozhushou(String appPkg, String marketPkg, Activity activity) {
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);
activity.startActivity(intent);
} catch (Exception e) {
anzhuo("com.jiutouxiang","com.hiapk.marketpho",activity);
}
}
public static void anzhuo(String appPkg, String marketPkg, Activity activity) {
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);
activity.startActivity(intent);
} catch (Exception e) {
anzhi("com.jiutouxiang","cn.goapk.market",activity);
}
}
public static void anzhi(String appPkg, String marketPkg, Activity activity) {
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);
activity.startActivity(intent);
} catch (Exception e) {
lianjie(activity);
}
}
public static void lianjie( Activity activity) {
/* Uri uri = Uri.parse("http://a.app.qq.com/o/simple.jsp?pkgname=com.jiutouxiang");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
activity.startActivity(intent);*/
ForToast.showToast("您的手机未安装市场应用", MyApplication.getInstance());
}
}
然后调用:
MarketUtils.yingyongbao("com.jiutouxiang","com.tencent.android.qqdownloader",activity);