web-dev-qa-db-fra.com

Lien "Évaluer cette application" dans l'application Google Play Store sur le téléphone

Je voudrais mettre un lien "Évaluer cette application" dans une application Android pour ouvrir la liste des applications dans l'application Google Play Store de l'utilisateur sur leur téléphone.

  1. Quel code dois-je écrire pour créer le lien market:// ou http://- ouvert dans l'application Google Play Store sur le téléphone?
  2. Où mettez-vous le code?
  3. Quelqu'un at-il un exemple de mise en œuvre de cela?
  4. Devez-vous spécifier l'écran où le lien market:// ou http:// sera placé, et lequel est le mieux à utiliser - market:// ou http://?
219
Adreno

J'ouvre le Play Store depuis mon application avec le code suivant:

    Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
    // To count with Play market backstack, After pressing back button, 
    // to taken back to our application, we need to add following flags to intent. 
    goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
                    Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
                    Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    try {
        startActivity(goToMarket);
    } catch (ActivityNotFoundException e) {
        startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse("http://play.google.com/store/apps/details?id=" + context.getPackageName())));
    }

Cela lancera le Play Store avec la page de votre application déjà ouverte. L'utilisateur peut noter ici.

476
miguel.rodelas

Voici un code fonctionnel et à jour :)

/*
* Start with rating the app
* Determine if the Play Store is installed on the device
*
* */
public void rateApp()
{
    try
    {
        Intent rateIntent = rateIntentForUrl("market://details");
        startActivity(rateIntent);
    }
    catch (ActivityNotFoundException e)
    {
        Intent rateIntent = rateIntentForUrl("https://play.google.com/store/apps/details");
        startActivity(rateIntent);
    }
}

private Intent rateIntentForUrl(String url)
{
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("%s?id=%s", url, getPackageName())));
    int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
    if (Build.VERSION.SDK_INT >= 21)
    {
        flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
    }
    else
    {
        //noinspection deprecation
        flags |= Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
    }
    intent.addFlags(flags);
    return intent;
}

Mettez le code dans la Activity à partir de laquelle vous souhaitez l'appeler.
Lorsque l'utilisateur clique sur un bouton pour évaluer l'application, il vous suffit d'appeler la fonction rateApp().

35
György Benedek

J'utilise toujours ce code:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=PackageName")));
20
Cabezas

Ceci si vous publiez votre application sur Google Play Store et Amazon Appstore. Je gère également le cas où les utilisateurs (en particulier en Chine) ne disposent pas à la fois d'App Store et de navigateur.

public void goToMyApp(boolean googlePlay) {//true if Google Play, false if Amazone Store
    try {
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse((googlePlay ? "market://details?id=" : "amzn://apps/android?p=") +getPackageName())));
    } catch (ActivityNotFoundException e1) {
        try {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse((googlePlay ? "http://play.google.com/store/apps/details?id=" : "http://www.Amazon.com/gp/mas/dl/android?p=") +getPackageName())));
        } catch (ActivityNotFoundException e2) {
            Toast.makeText(this, "You don't have any app that can open this link", Toast.LENGTH_SHORT).show();
        }
    }
}
16
Hải Phong

Vous pouvez toujours appeler getInstalledPackages () à partir de PackageManager class et vérifier que la classe de marché est installée. Vous pouvez également utiliser queryIntentActivities () pour vous assurer que l'intention que vous construisez pourra être gérée par quelque chose, même si ce n'est pas l'application du marché. C’est probablement la meilleure chose à faire car c’est la plus flexible et la plus robuste.

Vous pouvez vérifier si l'application du marché est là par

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://search?q=foo"));
PackageManager pm = getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);

Si la liste a au moins une entrée, le marché est là.

Vous pouvez utiliser les éléments suivants pour lancer Android Market sur la page de votre application. Il est un peu plus automatisé:

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

Si vous souhaitez tester cela sur votre émulateur, le marché ne s'y trouve probablement pas: Consultez ces liens pour plus de détails:

Comment activer l'Android Market dans l'émulateur Google Android

Installation de Google Play sur Android Emulator

9
K_Anas

J'utilise cette approche pour que les utilisateurs évaluent mes applications:

public static void showRateDialog(final Context context) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context)
            .setTitle("Rate application")
            .setMessage("Please, rate the app at PlayMarket")
            .setPositiveButton("RATE", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (context != null) {
                        String link = "market://details?id=";
                        try {
                            // play market available
                            context.getPackageManager()
                                    .getPackageInfo("com.Android.vending", 0);
                        // not available
                        } catch (PackageManager.NameNotFoundException e) {
                            e.printStackTrace();
                            // should use browser
                            link = "https://play.google.com/store/apps/details?id=";
                        }
                        // starts external action
                        context.startActivity(new Intent(Intent.ACTION_VIEW, 
                                Uri.parse(link + context.getPackageName())));
                    }
                }
            })
            .setNegativeButton("CANCEL", null);
    builder.show();
}
6
gtgray

Vous pouvez l'utiliser, cela fonctionne pour moi

public static void showRateDialogForRate(final Context context) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context)
            .setTitle("Rate application")
            .setMessage("Please, rate the app at PlayMarket")
            .setPositiveButton("RATE", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (context != null) {
                        ////////////////////////////////
                        Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
                        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                        // To count with Play market backstack, After pressing back button,
                        // to taken back to our application, we need to add following flags to intent.
                        goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
                                Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET |
                                Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                        try {
                            context.startActivity(goToMarket);
                        } catch (ActivityNotFoundException e) {
                            context.startActivity(new Intent(Intent.ACTION_VIEW,
                                    Uri.parse("http://play.google.com/store/apps/details?id=" + context.getPackageName())));
                        }


                    }
                }
            })
            .setNegativeButton("CANCEL", null);
    builder.show();
}
3
Suman

Linkify est une autre approche qui peut fonctionner pour vous. Si j'ai un TextView qui demande à l'utilisateur d'évaluer l'application, je peux lier quelques mots dans le texte pour les mettre en surbrillance et lorsque l'utilisateur les touche, le Play Store s'ouvre, prêt pour leur révision:

class playTransformFilter implements TransformFilter {
   public String transformUrl(Matcher match, String url) {
        return "market://details?id=com.qwertyasd.yourapp";
   }
}

class playMatchFilter implements MatchFilter {
    public boolean acceptMatch(CharSequence s, int start, int end) {
        return true;
    }
}
text1 = (TextView) findViewById(R.id.text1);
text1.setText("Please rate it.");
final Pattern playMatcher = Pattern.compile("rate it");
Linkify.addLinks(text1, playMatcher, "", 
                   new playMatchFilter(), new playTransformFilter());
2
Garnet Ulrich

Un point concernant toutes les réponses ayant des implémentations basées sur la stratégie getPackageName () est que l'utilisation de BuildConfig.APPLICATION_ID peut être plus simple et fonctionner correctement si vous utilisez la même base de code pour créer plusieurs applications avec des identifiants d'application différents (par exemple, produit à étiquette blanche).

2
hecht
import Android.content.ActivityNotFoundException;
import Android.content.Context;
import Android.content.Intent;
import Android.net.Uri;
import Android.os.Build;
import Android.support.annotation.StringRes;
import Android.widget.Toast;

public class PlayStoreLink {

public void checkForUpdate(Context context, int applicationId) 
{
    try {
        context.startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse(context.getString(R.string.url_market_details)
                        + applicationId)));
    } catch (Android.content.ActivityNotFoundException anfe) {
        try {
            context.startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse(context.getString(R.string.url_playstore_app)
                            + applicationId)));
        } catch (Exception e) {
            Toast.makeText(context,
                    R.string.install_google_play_store,
                    Toast.LENGTH_SHORT).show();
        }
    }
}

public void moreApps(Context context, @StringRes int devName) {
    try {
        context.startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse(context.getString(R.string.url_market_search_app)
                        + context.getString(devName))));
    } catch (Android.content.ActivityNotFoundException anfe) {
        try {
            context.startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse(context.getString(R.string.url_playstore_search_app)
                            + context.getString(devName))));
        } catch (Exception e) {
            Toast.makeText(context,
                    R.string.install_google_play_store,
                    Toast.LENGTH_SHORT).show();
        }
    }
}

public void rateApp(Context context, int applicationId) {
    try {
        Uri uri = Uri.parse(context.getString(R.string.url_market_details)
                + applicationId);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KitKat_WATCH)
            flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
        else
            flags |= Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
        intent.addFlags(flags);
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        checkForUpdate(context, applicationId);
    }
}
}

<string name="install_google_play_store" translatable="false">Please install google Play Store and then try again.</string>
<string name="url_market_details" translatable="false">market://details?id=</string>
<string name="url_playstore_app" translatable="false">https://play.google.com/store/apps/details?id=</string>
<string name="url_market_search_app" translatable="false">market://search?q=pub:</string>
<string name="url_playstore_search_app" translatable="false">http://play.google.com/store/search?q=pub:</string>
<string name="app_link" translatable="false">https://play.google.com/store/apps/details?id=</string>

devName est le nom du compte développeur sur le Play Store

1
Pratik Saluja

Classement du Play Store

 btn_rate_us.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("market://details?id=" + getPackageName());
                Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                // To count with Play market backstack, After pressing back button,
                // to taken back to our application, we need to add following flags to intent.
                goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
                        Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
                        Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                try {
                    startActivity(goToMarket);
                } catch (ActivityNotFoundException e) {
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
                }
            }
        });
1
Keshav Gera

Vous devriez utiliser ce code simple pour évaluer votre application dans votre activité.

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

Une version kotlin 

fun openAppInPlayStore() {
    val uri = Uri.parse("market://details?id=" + context.packageName)
    val goToMarketIntent = Intent(Intent.ACTION_VIEW, uri)

    var flags = Intent.FLAG_ACTIVITY_NO_HISTORY or Intent.FLAG_ACTIVITY_MULTIPLE_TASK
    flags = if (Build.VERSION.SDK_INT >= 21) {
        flags or Intent.FLAG_ACTIVITY_NEW_DOCUMENT
    } else {
        flags or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    goToMarketIntent.addFlags(flags)

    try {
        startActivity(context, goToMarketIntent, null)
    } catch (e: ActivityNotFoundException) {
        val intent = Intent(Intent.ACTION_VIEW,
                Uri.parse("http://play.google.com/store/apps/details?id=" + context.packageName))

        startActivity(context, intent, null)
    }
}
0
kuzdu