首页 > 解决方案 > 如何在 Java 中的事件之间设置超时

问题描述

当用户单击其显示广告的任何按钮时,我的应用程序有很多按钮,并且没有计时器为此我的意思是,如果您在 1 秒后再次单击其显示广告的相同按钮,我的帐户为此受到 admob 的限制,我想当用户每 2 分钟点击任何按钮时显示广告((每 2 分钟事件后显示广告))

我的 Java 代码在按钮中显示广告

    Button button1 = findViewById(R.id.button);
    Button button2 = findViewById(R.id.button2);
    Button button3 = findViewById(R.id.button3);
  


    button1.setOnClickListener(v -> {

        if (mInterstitialAd != null) {
            save_id(button1.getId());
            mInterstitialAd.show(MainActivity.this);
       

        } else {
            Log.d("TAG", "The interstitial ad wasn't ready yet.");

            Intent intent = new Intent(getApplicationContext(), Activity1.class);

            startActivity(intent);


        }
    });

    button2.setOnClickListener(v -> {

        if (mInterstitialAd != null) {
            save_id(button2.getId());
            mInterstitialAd.show(MainActivity.this);

        
        } else {
            Log.d("TAG", "The interstitial ad wasn't ready yet.");

            Intent intent = new Intent(getApplicationContext(), Activity2.class);

            startActivity(intent);
        }
    });

    button3.setOnClickListener(v -> {

        if (mInterstitialAd != null) {
            save_id(button3.getId());
            mInterstitialAd.show(MainActivity.this);

        } else {
            Log.d("TAG", "The interstitial ad wasn't ready yet.");



            Intent intent = new Intent(getApplicationContext(), Activity3.class);

            startActivity(intent);
        }
    });


private void save_id(int id) {

    SharedPreferences preferences = getSharedPreferences("SAVING", MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt("mID", id);
    editor.apply();
}

private int load_id(){
    SharedPreferences preferences = getSharedPreferences("SAVING", MODE_PRIVATE);
    return preferences.getInt("mID", 0);

}


@Override
protected void onStart() {
    super.onStart();

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {

            AdRequest adRequest = new AdRequest.Builder().build();


            InterstitialAd.load(MainActivity.this,"ca-app-pub-3940256099942544/1033173712", adRequest, new InterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                    // The mInterstitialAd reference will be null until
                    // an ad is loaded.
                    mInterstitialAd = interstitialAd;
                    Log.i("TAG", "onAdLoaded");

                    mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
                        @Override
                        public void onAdDismissedFullScreenContent() {
                            // Called when fullscreen content is dismissed.
                            Log.d("TAG", "The ad was dismissed.");




                            Intent intent;
                            switch (load_id()){
                                case R.id.button:
                                    intent = new Intent(MainActivity.this, Activity1.class);

                                    break;
                                case R.id.button2:
                                    intent = new Intent(MainActivity.this, Activity2.class);
                                    break;
                                case R.id.button3:
                                    intent = new Intent(MainActivity.this, Activity3.class);
                                    break;



                                default:
                                    return;
                            }
                            startActivity(intent);
                        }


                        @Override
                        public void onAdFailedToShowFullScreenContent(AdError adError) {
                            // Called when fullscreen content failed to show.
                            Log.d("TAG", "The ad failed to show.");
                        }

                        @Override
                        public void onAdShowedFullScreenContent() {
                            // Called when fullscreen content is shown.
                            // Make sure to set your reference to null so you don't
                            // show it a second time.
                            mInterstitialAd = null;
                            Log.d("TAG", "The ad was shown.");
                        }
                    });

                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    // Handle the error
                    Log.i("TAG", loadAdError.getMessage());
                    mInterstitialAd = null;
                }


            });

        }
    });

}

}

标签: javaandroidadmob

解决方案


看起来您只需要跟踪上次展示广告的时间。Instant是一个很好的选择。

import java.time.Instant;
import java.time.Duration;

public class TimerExample {

//Set to 2 minutes ago to ensure the first ad is displayed immediately
final Duration WAIT_BETWEEN_ADS = Duration.ofMinutes( 2 ) ;
private Instant lastAdDisplayed = Instant.now().minus(WAIT_BETWEEN_ADS);

public void setupExampleButton() {
    Button button1 = findViewById(R.id.button);
    button1.setOnClickListener(v -> {

        if(mInterstitialAd != null && lastAdDisplayed.isBefore(Instant.now().minus(WAIT_BETWEEN_ADS))){
            // show the ad!
            lastAdDisplayed = Instant.now();
            save_id(button1.getId());
            mInterstitialAd.show(MainActivity.this);
        } else {
            // Don't show the ad!
            Log.d("TAG", "The interstitial ad wasn't ready yet.");
            Intent intent = new Intent(getApplicationContext(), Activity1.class);
            startActivity(intent);
        }
    });
}

}


推荐阅读