get Review Dialog - Android User Interface

Android examples for User Interface:Dialog

Description

get Review Dialog

Demo Code


import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.LabeledIntent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.widget.Toast;

public class Main{
    private static final String REVIEW_PREFS = "Review_pref_data";
    private static final String KEY_REVIEW_DONE = "ReviewDone";
    public static Dialog getReviewDialog(final Context ctx,
            final ReviewDialogFinishListener listener, String title,
            int drawableId, String msg) {

        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setTitle(title);/*w  w w .jav  a2  s  .  c o m*/
        if (drawableId != -1) {
            builder.setIcon(drawableId);
        }
        builder.setMessage(msg)
                .setCancelable(false)
                .setPositiveButton("Rate it",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int id) {
                                startReview(ctx);
                                if (listener != null) {
                                    listener.onReviewDialogFinish();
                                    setReviewDone(ctx, true);
                                }

                            }
                        })
                .setNeutralButton("Later",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int id) {
                                if (listener != null) {
                                    listener.onReviewDialogFinish();
                                }
                            }
                        });
        AlertDialog alert = builder.create();
        return alert;
    }
    public static void startReview(Context context) {

        Intent intent = new Intent(Intent.ACTION_VIEW,
                getReviewUri(context));
        if (isIntentAvailable(context, intent)) {
            context.startActivity(intent);
            setReviewDone(context, true);
        } else {
            setReviewDone(context, false);
            Toast.makeText(context, "Network Error", Toast.LENGTH_LONG)
                    .show();
        }

    }
    public static void setReviewDone(Context context, boolean isDone) {

        SharedPreferences sharedPreferences = context.getSharedPreferences(
                REVIEW_PREFS, Context.MODE_PRIVATE);
        Editor edit = sharedPreferences.edit();
        edit.putBoolean(KEY_REVIEW_DONE, isDone);
        edit.commit();

    }
    private static Uri getReviewUri(Context context) {
        return Uri.parse("market://details?id=" + context.getPackageName());
    }
    public static boolean isIntentAvailable(Context ctx, Intent in) {
        PackageManager packageManager = ctx.getPackageManager();
        List<ResolveInfo> activities = packageManager
                .queryIntentActivities(in, 0);
        return (activities == null) ? false : (activities.size() > 0);
    }
}

Related Tutorials