2011-11-06 63 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()))); 
} 
相關問題