按下后退按钮为应用评分

时间:2013-12-24 03:29:12

标签: android

我正在使用此代码让用户评价我们的应用程序。问题是我无法找到放置此代码的位置,以便当用户通过按返回按钮退出应用程序时,会出现一个对话框,提示用户对我们的应用进行评级。这是代码:

AlertDialog.Builder alert = new AlertDialog.Builder(FrontPage.this);
alert.setTitle("Rate Us:");
alert.setPositiveButton("Yes", new OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {

        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse("market://details?id=" + getPackageName()));
        startActivity(i);
    }
});

alert.setNegativeButton("No", new OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
    }
});
alert.create();
alert.show();

4 个答案:

答案 0 :(得分:2)

在您的Activity中覆盖onBackPressed()方法并将此代码放在那里。不要调用super.onBackPressed(),因为它只是完成了Activity。并在按钮单击对话框或按两次单击按钮完成您的活动。从对话框完成“活动”时要小心。首先关闭对话框,然后调用finish()。在解雇之前提前完成活动Dialog将抛出异常

答案 1 :(得分:2)

试试这个:

@Override
public void onBackPressed() {
    AlertDialog.Builder alert = new AlertDialog.Builder(FrontPage.this);
    alert.setTitle("Rate Us:");

    alert.setPositiveButton("Yes", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse("market://details?id=" + getPackageName()));
            startActivity(i);
        }
    });

    alert.setNegativeButton("No", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        }
    });
    alert.create();
    alert.show();
}

希望这有帮助。

答案 2 :(得分:2)

您需要将代码放在onBackPressed()方法中。像这样:

public static final String PACKAGE_NAME = "yourPackageName";

public void rate()
{
    new AlertDialog.Builder(FrontPage.this)
    .setMessage("Rate Us:")
    .setCancelable(true)
    .setNegativeButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + PACKAGE_NAME)));
    }}).setNeutralButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {
            finish();
    }}).show();
}

public void onBackPressed()
{
    rate();
}

答案 3 :(得分:1)

只需将其粘贴到您的java类

中即可
 @Override
public void onBackPressed() {

    AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
    alert.setTitle("Rate Us:");
    alert.setPositiveButton("Yes", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            Uri uri = Uri.parse("market://details?id=" + getPackageName());
            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
            try {
                startActivity(goToMarket);
            } catch (ActivityNotFoundException e) {
                startActivity(new Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse("http://play.google.com/store/apps/details?id="
                                + getPackageName())));
            }

            dialog.dismiss();
            finish();
        }
    });

    alert.setNegativeButton("No", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            finish();
        }
    });
    alert.create();
    alert.show();

}