您的位置:首页 > 移动开发 > Android开发

Android跳转到应用商店详情页面

2017-11-27 14:55 567 查看
最近有一个需求,产品经理按照IOS发布新版后在appstore评论或者评分功能设计的,让Android跳转到本机应用商店中本应用的详情页。

上网查了一些资料,实现步骤如下:

弹出对话框:

/**
* 应用评价对话框
*
* @param activity 上下文
* @return 对话框
*/
public static Dialog showAppSuggestDialog(Activity activity) {
Dialog dialog = new Dialog(activity, R.style.MyDialog);
View contentView = View.inflate(activity, R.layout.dialog_version_suggest, null);
dialog.setContentView(contentView);
dialog.setCanceledOnTouchOutside(true);
dialog.show();
ImageView update_close = contentView.findViewById(R.id.update_close);
TextView tv_say_bad = contentView.findViewById(R.id.tv_say_bad);
TextView tv_say_good = contentView.findViewById(R.id.tv_say_good);
update_close.setOnClickListener(view -> dialog.dismiss());
tv_say_bad.setOnClickListener(view -> {
dialog.dismiss();
toMarket(activity);
});
tv_say_good.setOnClickListener(view -> {
dialog.dismiss();
toMarket(activity);
});
return dialog;
}


MyDialog为对话框样式:

<style name="MyDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@color/transparent</item>
</style>


点击对话框按钮获取跳转意图:

/**
* 获取跳转意图
*
* @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) {
MyToast.showToast(Constants.ERROR_NO_MARKET);
}
} else {
MyToast.showToast(Constants.ERROR_NO_MARKET);
}
}


最终实现结果:

评论和评分,在Android上只能进入详情页,由用户点击评价进到评分和评论的页面,所以跳转逻辑一样;

进行跳转只能跳转到本机已安装应用,所以进入的是本机预装的应用商店。如果本机应用商店中没有收录自己的应用,跳转后搜索不到应用包名。ps:然而上线前这个功能被pass掉了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: