2011-11-06 108 views
28

在我的Android应用程序中,我想在某个时间点提示用户对Android应用程序进行评分。我找到了一些代码on this website。这段代码似乎工作得很好。提示用户对应用程序内的Android应用程序进行评分

但不幸的是,当用户手机上没有安装Android Market时,此代码似乎会引发“强制关闭”错误消息。有什么方法可以检查Android市场是否安装,如果没有,不要尝试执行代码?

这引起了错误的路线大概是这样的一个,因为它无法解析URI:

mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME))); 

而且,顺便说一句,有没有可能在该代码可以提高任何其他的东西?

编辑:

几年后,我把所有的代码放到一个小型图书馆项目:AppRater on GitHub

+0

是否有可能在Play商店中的应用程序发布之前测试您的图书馆吗?或者它必须出现在商店才能显示弹出窗口? –

+1

@StackDiego只需从GitHub项目中获取最新的JAR,并调用'demo()'而不是'show()':)感谢您的反馈! – caw

+0

谢谢,我现在要尝试 –

回答

14

你总是可以从PackageManager类叫getInstalledPackages()和检查确保安装市场类别。您还可以使用queryIntentActivities()来确保您构建的意图能够被某种东西处理,即使它不是市场应用程序。这可能是实际做的最好的事情,因为它是最灵活和最健壮的。

+0

谢谢!这正是我一直在寻找的:) – caw

0

如果应用程序透过Android Market下载,用户将安装Android Market的在电话里,所以我真的不认为这是一个问题。这似乎很奇怪...

您可以使用下面的推出Android Market的应用程序的页面上,这是一个有点更加自动化:

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

您发布的代码有效,但上面链接中显示的代码更短 - 只是一条线来启动意图。并且:当然,用户可能没有Android市场应用:他们通过Android市场下载我的应用,然后(在某个时候)从他们的手机中删除Android市场应用;) – caw

+0

为什么他们会想要那样做?这似乎非常愚蠢。绝对不是你作为开发人员应该关心的东西。但是如果你坚持,你可以做库尔蒂斯的建议。检查应用程序是否已安装。您也可以执行以下操作:http://stackoverflow.com/questions/4439043/what-is-the-package-name-of-the-android-market-or-google-apps并检查它是否正在返回应用程序。 –

+1

谢谢。尽管如此,它可能是“愚蠢的”但可能的。方法getPackageName()是避免对包名进行硬编码的好方法。但由于班级(见链接)在外部班级,我不能使用它。 – caw

1

并非所有的Android设备都使用应用程序市场。 Kindle和Nook有他们自己的市场,因此需要用于检查市场是否存在的代码是一个很好的选择。尽管应该有方法将评级发送到正确的市场,但不管它是哪一个。有什么东西要看。

+1

考虑到Nook和亚马逊市场有一个提交过程,这些链接总是被检查,所以这不是一个真正的问题。你永远不会收到一个应用程序接受发布在任何一个市场与谷歌的价格链接,所以力量关闭不会是一个问题,你将不得不重做市场特定的费率代码。 –

36

这里有您需要的所有代码,(Kurt的回答和推断的信息的聚集,加上链接和问题):

/* This code assumes you are inside an activity */ 
final Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName()); 
final Intent rateAppIntent = new Intent(Intent.ACTION_VIEW, uri); 

if (getPackageManager().queryIntentActivities(rateAppIntent, 0).size() > 0) 
{ 
    startActivity(rateAppIntent); 
} 
else 
{ 
    /* handle your error case: the device has no way to handle market urls */ 
} 
+0

完美:-)喜欢这种方法。这将在应用程序在商店中发布时起作用 – Nabin

9

您还可以使用RateMeMaybe:https://github.com/Kopfgeldjaeger/RateMeMaybe

它给你相当多的配置选项(如果用户选择“不是现在”,对话标题,消息等),最少天数/启动直到第一次提示,最短天数/启动直到每个下一个提示。它也很容易使用。

用法示例从自述:

RateMeMaybe rmm = new RateMeMaybe(this); 
rmm.setPromptMinimums(10, 14, 10, 30); 
rmm.setDialogMessage("You really seem to like this app, " 
       +"since you have already used it %totalLaunchCount% times! " 
       +"It would be great if you took a moment to rate it."); 
rmm.setDialogTitle("Rate this app"); 
rmm.setPositiveBtn("Yeeha!"); 
rmm.run(); 
+1

假设我在我的第一个活动的onCreate中执行代码,那么它是否会向用户显示对话框?因为每次我调用14天后显示对话框的RateMeMaybe的方法。对? – keen

0

当我使用 “市场://细节ID =?” + getApplicationContext()getPackageName()它打开了我mobogenie市场,所以我更喜欢使用https://play.google.com/store/apps/details?id=。 “+ getApplicationContext()。getPackageName()

+0

这仅仅意味着你的另类市场也表示它也可以处理'market://'URL。你可能选择这个应用程序作为你手机上的默认设置。 – caw

+0

hm可能是,我在模拟器上试过了。 –

1

这个简单的代码将实现你想要什么,不需要外部库或任何幻想。只需将它放在主要活动的OnCreate事件上即可。变量RunEvery将决定费率消息的出现频率。在这个例子中,它被设置为10.

// Count times app has been opened, display rating message after number of times 
// By Rafael Duval 
    try { 

     // Get the app's shared preferences 
     SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this); 

     // Get the value for the run counter 
     int counter = app_preferences.getInt("counter", 0); 

     // Do every x times 
     int RunEvery = 10; 

     if(counter != 0 && counter % RunEvery == 0) 
     { 
      //Toast.makeText(this, "This app has been started " + counter + " times.", Toast.LENGTH_SHORT).show(); 

      AlertDialog.Builder alert = new AlertDialog.Builder(
        MyActivity.this); 
        alert.setTitle("Please rate"); 
        alert.setIcon(R.drawable.ic_launcher); //app icon here 
        alert.setMessage("Thanks for using this free app. Please take a moment to rate it."); 

        alert.setPositiveButton("Cancel", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
         int whichButton) {        
          //Do nothing 
         } 
        }); 

        alert.setNegativeButton("Rate it", 
        new DialogInterface.OnClickListener() { 

         public void onClick(DialogInterface dialog, int which) { 

          final String appName = getApplicationContext().getPackageName(); 
          try { 
          startActivity(new Intent(Intent.ACTION_VIEW, 
           Uri.parse("market://details?id=" 
           + appName))); 
          } catch (android.content.ActivityNotFoundException anfe) { 
          startActivity(new Intent(
           Intent.ACTION_VIEW, 
           Uri.parse("http://play.google.com/store/apps/details?id=" 
           + appName))); 
          } 

         } 
        }); 
        alert.show();    
     } 


     // Increment the counter 
     SharedPreferences.Editor editor = app_preferences.edit(); 
     editor.putInt("counter", ++counter); 
     editor.commit(); // Very important   

    } catch (Exception e) { 
     //Do nothing, don't run but don't break 
    }   
5

首先你需要计算应用程序使用次数;

SharedPreferences preferences = getSharedPreferences("progress", MODE_PRIVATE); 
int appUsedCount = preferences.getInt("appUsedCount",0); 
appUsedCount++; 
SharedPreferences.Editor editor = preferences.edit(); 
editor.putInt("appUsedCount", appUsedCount); 
editor.apply(); 

if (appUsedCount==10 || appUsedCount==50 || appUsedCount==100 || appUsedCount==200 || appUsedCount==300){ 
    AskForRating(appUsedCount); 
} else { 
    finish(); 
} 

比你可以这样提示;

private void AskForRating(int _appUsedCount){ 

    AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    alert.setTitle("Please Rate Us"); 
    alert.setIcon(R.drawable.book); 
    alert.setMessage("Thanks for using the application. If you like YOUR APP NAME please rate us! Your feedback is important for us!"); 
    alert.setPositiveButton("Rate it",new Dialog.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int whichButton){ 
      String url = "https://play.google.com/store/apps/details?id=YOUR PACKAGE NAME"; 
      Intent i = new Intent(Intent.ACTION_VIEW); 
      i.setData(Uri.parse(url)); 
      startActivity(i); 
     } 
    }); 
    alert.setNegativeButton("Not now", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      finish(); 
     } 
    }); 
    alert.show(); 
} 
+0

这里的区别在于它会通过浏览器将您带到网站,而不是使用Play商店应用。您应该首先检查使用Play商店应用程序是不可能的,然后使用浏览器访问该网站。 – xxx

0

使用此代码

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()))); 
} 
相关问题