简体   繁体   中英

Rate app on the press of back button

I'm using this code to make the user rate our application. The problem is that I'm unable to find where to place this code so that when user exits the application by pressing the back button, a dialog will prompt the user to rate our app. Here is the code:

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();

override onBackPressed() method in your Activity and put this code there. don't call super.onBackPressed() as it simply finishes Activity. and finish your Activity in one of button click of the dialog or in both clicks of button. and be careful when you are finishing the Activity from dialog. first dismiss the dialog and then call finish() . early finishing the Activity before dismissing Dialog will throw an Exception

Try this :

@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();
}

Hope this helps.

You need to put your code in the onBackPressed() method. Something like this:

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();
}

Just copy paste it in your java class

 @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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM