简体   繁体   中英

How to show alert dialog every 10 launches?

I don't know if this question was asked, but I couldn't find it. I want alert dialog to show every 10 or more times app is launched.

 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();

Set a sharedPreferences and increment the count every time by the value stored in it. When the values reaches the count, Just show alert and reset the sharedpreferences.

      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.

You can store an integer value in the shared preference to keep count of how many times your app has been launched.
You can increment the value in OnCreate() method of launch activity or in any other activity that is an entry point to your app(like from notification).
You should reset the value after displaying the dialog every time.
Here's a small piece of code -

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();

You can store open count in SharedPreferences

add counter on oncreate activity.

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

// Get Counter Values

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.  

        }

I Hope this example is help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM