如何放置按钮以转到其他应用程序



我有这段代码可以带您从我的页面访问其他应用程序。

Button more_apps = (Button)findViewById(R.id.more_apps);
more_apps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent more_apps = new Intent();
more_apps.setAction(Intent.ACTION_VIEW);
more_apps.addCategory(Intent.CATEGORY_BROWSABLE);
more_apps.setData(Uri.parse("https://play.google.com/store/apps/dev?id=****"));
startActivity(more_apps);
}
});

没有问题,只是有时谷歌播放会拒绝该应用程序,并向我发送一张图片,表明"更多应用程序"按钮正在推广其他应用程序。。布拉布拉

所以我有了一个想法:当你点击按钮时,会出现一条确认消息,显示"是"或"否",如果你按"是",它会将你引导到链接,而按"否"则会保留在应用程序中。

如何做到这一点

显示对话框的最简单方法可能是使用AlertDialog.Builder()进行构建。

您可以从一些随机Util类中使用以下方法,并只传递参数:

public static AlertDialog createAlert(String message, String positiveButtonText, DialogInterface.OnClickListener positiveListener, String negativeButtonText, DialogInterface.OnClickListener negativeListener, Context context, boolean isCancelable) {
return new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton(positiveButtonText, positiveListener)
.setNegativeButton(negativeButtonText, negativeListener)
.setCancelable(isCancelable)
.create();
}

你这样使用它:

AlertDialog goToPlayStoreDialog = RandomUtilClass.createAlert(
"Do you want to see more of my apps?",
"Yes", positiveListener,
"No", negativeListener,
context, false);
goToPlayStoreDialog.show();

以下是监听器的实现方式:

DialogInterface.OnClickListener positiveListener = (dialog, which) -> {
**Start intent here**
};

对于消极的听众,只需调用dialog.dismiss();

注意您应该在创建对话框之前实现监听器:(

相关内容

最新更新