2014-01-08 45 views
1

我使用這個code在我的家庭活動中創建一個彈出對話框,要求用戶評價我的應用程序,代碼工作正常,但是當用戶選擇選項「不,謝謝」,對話框不應再顯示。關閉彈出對話框一旦點擊

然而,當應用程序被重新打開,我homeActivity重載,一切都在它被重置,所以儘管這

editor.putBoolean("dontshowagain", true); 
editor.commit(); 

,對話框將再次顯示,有反正我可以存儲布爾值,當活動重裝?

public static class AppRater { 

    private final int DAYS_UNTIL_PROMPT = 3; 
    private final int LAUNCHES_UNTIL_PROMPT = 7; 

    public void app_launched(Context mContext) { 
     SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0); 
     if (prefs.getBoolean("dontshowagain", false)) { return ; } 

     SharedPreferences.Editor editor = prefs.edit(); 

     // Increment launch counter 
     long launch_count = prefs.getLong("launch_count", 0) + 1; 
     editor.putLong("launch_count", launch_count); 

     // Get date of first launch 
     Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0); 
     if (date_firstLaunch == 0) { 
      date_firstLaunch = System.currentTimeMillis(); 
      editor.putLong("date_firstlaunch", date_firstLaunch); 
     } 

        // i just use this to test the dialog instantly 
      showRateDialog(mContext, editor); 



     // Wait at least n days before opening 
     if (launch_count >= LAUNCHES_UNTIL_PROMPT) { 
      if (System.currentTimeMillis() >= date_firstLaunch + 
        (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) { 
       showRateDialog(mContext, editor); 
      } 
     } 

     editor.commit(); 
    } 

    public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) { 
     final Dialog dialog = new Dialog(mContext); 
     dialog.setTitle("Rate MyApp"); 

     LinearLayout ll = new LinearLayout(mContext); 
     ll.setOrientation(LinearLayout.VERTICAL); 

     TextView tv = new TextView(mContext); 
     tv.setText("We see that you have been using MyApp well. Would you like to rate us?"); 
     tv.setWidth(240); 
     tv.setPadding(4, 0, 4, 10); 
     ll.addView(tv); 

     Button b1 = new Button(mContext); 
     b1.setText("Rate MyApp"); 
     b1.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Uri uri = Uri.parse("market://details?id=" + mContext.getPackageName()); 
       Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); 
       try { 
        mContext.startActivity(goToMarket); // playstore installed 
       } catch (ActivityNotFoundException e) { // open website if not 
        mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + mContext.getPackageName()))); 
       } 
       dialog.dismiss(); 
      } 
     });   
     ll.addView(b1); 

     Button b2 = new Button(mContext); 
     b2.setText("Remind me later"); 
     b2.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       dialog.dismiss(); 
      } 
     }); 
     ll.addView(b2); 


     Button b3 = new Button(mContext); 
     b3.setText("No, thanks"); 
     b3.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       rated = true; 
       if (editor != null) { 
        editor.putBoolean("dontshowagain", true); 
        editor.commit(); 
       } 
       dialog.dismiss(); 
      } 
     }); 
     ll.addView(b3); 

     dialog.setContentView(ll);   
     dialog.show();  
    } 
} 
// http://www.androidsnippets.com/prompt-engaged-users-to-rate-your-app-in-the-android-market-appirater 

回答

2

不錯的問題。我認爲原因是你在方法app_launched的末尾提交了editor,並且在一段時間後對話框中的按鈕按下了,所以當你這樣做時editor.putBoolean("dontshowagain", true)editor已經被提交,因此你的條目不保存在偏好。

如果您需要關於如何更改代碼來解決此問題的幫助,請在評論中告訴我。

編輯 - 某些代碼

首先,不要編輯傳遞到您的showRateDialog方法,所以更改方法簽名:

public static void showRateDialog(final Context mContext) 

其次,在你的onClick方法,創建一個新的編輯器,寫下標誌並提交。

public void onClick(View v) { 
    rated = true; 
    SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0); 
    // create editor, write stuff and commit, all in one line. 
    prefs.edit().putBoolean("dontshowagain", true).commit(); 
    dialog.dismiss(); 
} 
+0

是的請,其實我試圖設置另一個布爾值,並測試如果該值是假的,那麼我不會打電話給showRateDialog(),但它仍然不工作 – Casper

+0

添加了一些代碼和解釋。這應該讓你去。 – Ridcully

+0

謝謝,我已經修改了我的代碼,如你所建議的,但對話框仍然加載後,我退出應用程序,並再次使用它,順便說一句,我稱這個類在homeActivity的onCreate方法 'AppRater.showRateDialog(this);' – Casper

1

Ridicully is correct。使用此代碼,而不是打開一個新的編輯器:

b3.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     rated = true; 
     SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0); 
     SharedPreferences.Editor editorNew = prefs.edit(); 
     editorNew.putBoolean("dontshowagain", true); 
     editorNew.commit(); 

     dialog.dismiss(); 
    } 
}); 

然後通過移除編輯參數清理showRateDialog。請記住,在設置方法返回後,偵聽器的代碼會被調用很長時間。