2014-01-08 6 views
1

이 코드를 사용하여 사용자가 내 응용 프로그램을 평가하도록 요청하는 팝업 대화 상자를 만들 때이 코드는 제대로 작동하지만 사용자가 "No Thanks"옵션을 선택하면 대화 상자 다시는 보여서는 안됩니다. 이일단 클릭하면 팝업 대화 상자가 표시되지 않습니다.

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

에도 불구하고 대화 상자가 다시 표시됩니다 있도록 응용 프로그램을 다시 열 그러나

, 내 homeActivity 다시로드는, 그 안에 모든 것이, 리셋 활동이있을 때 나는 부울 값을 저장할 수 어쨌든이 다시로드?

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 방법 편집기를 통과하지 않기 때문에에 메소드 서명을 변경하여 onClick 방법,

public static void showRateDialog(final Context mContext) 

둘째, 새로운 에디터를 작성 플래그를 쓰고 커밋하십시오.

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

네, 실제로 다른 부울 값을 설정하고 그 값이 false이면 테스트 해 보았습니다. 그런 다음 showRateDialog()를 호출하지는 않지만 여전히 작동하지 않습니다. – Casper

+0

일부 코드 및 설명을 추가했습니다. 이것은 당신을 가야한다. – Ridcully

+0

고마워, 내가 제안한 코드를 수정했지만 응용 프로그램을 종료하고 다시 사용하면 대화 상자가 계속로드된다. btw 나는 homeActivity에서 onCreate 메소드로이 클래스를 호출했다. 'AppRater.showRateDialog (this); ' – Casper

1

리지컬이 정확합니다. 대신에이 코드를 사용하여 새로운 편집기를 엽니 다

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를 정리합니다. 리스너의 코드는 설정 메소드가 반환 된 후에 오랫동안 호출된다는 점을 기억하십시오.