2017-01-11 97 views
0

我不知道這個問題是否被問到,但我找不到它。 我希望提醒對話框每10次或更多次顯示應用程序啓動。如何每10次啓動顯示警報對話框?

AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity.this); 
a_builder.setMessage("Please take time and rate our application") 
.setCancelable(false) 
.setPositiveButton("Yes",new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
        Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName()); 
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri); 

        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=" + getApplicationContext().getPackageName()))); 
        } 

       } 
      }).setNegativeButton("Not Now",new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }) ; 
    AlertDialog alert = a_builder.create(); 
    alert.setTitle("Rate Us !"); 
    alert.show(); 
+1

可以使用sharedPreferences來存儲您的櫃檯,檢查每一個應用程序推出的價值 – Roljhon

+0

商店的推出數量INT的地方,並和增量,並檢查它每次啓動 –

回答

1

您可以在共享首選項中存儲一個整數值,以保持您的應用程序啓動的次數。
您可以在啓動活動的OnCreate()方法中或作爲應用程序入口點的任何其他活動(例如通知)中遞增值。
您應該在每次顯示對話框後重置該值。
這裏有一小塊的代碼 -

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
int appLaunchCount = pref.getInt("appLaunchCount",-1); 
    if(appLaunchCount==10){ 
     // code to show dialog 
     // reset 
     appLaunchCount=0; 
    } else { 
     // increment count 
     appLaunchCount = appLaunchCount+1; 
    } 
    SharedPreferences.Editor editor = pref.edit(); 
    editor.putInt("appLaunchCount", appLaunchCount); 
    editor.apply(); 
+0

以上答案應該可以工作。 –

+0

是的,它確實工作,謝謝 – Vladimir

+0

很高興可以幫助! :) –

5

設置一個sharedPreferences並每次增加存儲在其中的值。當值達到計數時,只需顯示警報並重置sharedpreferences。

+0

SharedPreferences sharedPreferences; int count; sharedPreferences = getPreferences(0); int launchCount = sharedPreferences.getInt(「numRun」,0); launchCount ++; (); putInt(「numRun」,launchCount).commit(); if(lanuchCount%10 == 0){ //做某事 } – Vladimir

+0

我應該這樣做嗎? – Vladimir

+0

幾乎正確。 –

1
 int count=0; 
int prefcount; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      count++; 
      SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(this); 
      SharedPreferences.Editor edit=pref.edit(); 
      edit.putInt("Count",count); 
      edit.commit(); 
      prefcount=pref.getInt("Count",-1); 
      if(prefcount>10){ 
       //show dialog 
      } 


     } 
} 
Hope this will help you. 
+0

我的朋友這是行不通的,因爲它會在10次之後顯示對話框,並且每次啓動時都會顯示 – Vladimir

+0

我已編輯好密碼。請仔細閱讀。 –

0

您可以將開放計數SharedPreferences上的OnCreate活動

添加計數器。

restoredCount ++ 
editor1.putInt("name", restoredCount); 
editor1.commit(); 

//獲取計數器值

SharedPreferences prefs = getSharedPreferences("user_count", MODE_PRIVATE); 
SharedPreferences.Editor editor1 = getSharedPreferences("user_count", MODE_PRIVATE).edit(); 
int restoredCount = prefs.getInt("name", 0); 



if (restoredCount == 10) { 
      editor1.putInt("name", 0); 
      editor1.commit(); 

      // Here Show Alert Dialog. 

     } 

我希望這個例子是幫助你。