1

Hello friend i am new in android and implement admob in my project i want to count how many time user click on my ads and stored in shareperfernce so if user click on my ads more than one my my ad will show next 4 days thats my scenario but the problem is that i dont know how to count number of click in interstitial ads and stored in sharedpefernce here is my code i make simple for your readability

    @Override
        protected void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_chapters);
            JodaTimeAndroid.init(this);

            sharedPreferences=getSharedPreferences("TimeStamp",MODE_PRIVATE);
            final String getcickdate=sharedPreferences.getString("currentdate",null);

              AppPrefrences.getUserClick(this);




        interstitialAd.setAdListener(new AdListener() {

                @Override
                public void onAdClosed() {
                    super.onAdClosed();
                    startActivity(intent);
                    interstitialAd.loadAd(new AdRequest.Builder().build());
                }


                @Override
                public void onAdLoaded() {
                    // Code to be executed when an ad finishes loading.
                    Toast.makeText(Chapters.this, "loaded", Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onAdFailedToLoad(int errorCode) {
                    // Code to be executed when an ad request fails.


                }

                @Override
                public void onAdOpened() {
                    // Code to be executed when the ad is displayed.




                }
                @Override
                public void onAdClicked() {
                    Date date=new Date();
                    SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy");
                    String currnetdate=format.format(date);
                    AppPrefrences.setClickTime(context,currnetdate);
                    int clickCount =AppPrefrences.getUserClick(context);
                    clickCount = clickCount + 1;
                     AppPrefrences.setUserClick(context,clickCount);

                    editor.commit();




    package bible.swordof.God.util;

    import android.content.Context;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    import android.widget.Toast;

    import es.dmoral.toasty.Toasty;

    public class AppPrefrences {

                private static SharedPreferences mPrefs;
                private static SharedPreferences.Editor mPrefsEditor;


        public static int getUserClick(Context ctx) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            return mPrefs.getInt("click", 0);
        }

        public static void setUserClick(Context ctx, int value) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            mPrefsEditor = mPrefs.edit();
            mPrefsEditor.putInt("click", value);
            mPrefsEditor.commit();
        }

public static String getClickTime(Context ctx) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            return mPrefs.getString("clickTime", "");
        }

        public static void setClickTime(Context ctx, String value) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            mPrefsEditor = mPrefs.edit();
            mPrefsEditor.putString("clickTime", value);
            mPrefsEditor.commit();
        }


    public static void clearAllPreferences(Context ctx) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            mPrefsEditor = mPrefs.edit();
            mPrefsEditor.clear();
            mPrefsEditor.commit();
        }
            }

when ever user click on ad onAdclick listner store value in sharedperfernce and increment existing value for example if user click on first on ad it will count 1 which i done but problem is that how to incremnet in pervious for example if user click on next on ad the store value in sharedperfence will change to 2

Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17
skyworld
  • 65
  • 1
  • 9
  • If you need to track ad clicks daily, better use a map. Check this https://stackoverflow.com/a/26709199/9263083 – sanoJ Jul 12 '19 at 06:33

0 Answers0