如何在java中设置事件之间的超时

llew8vvj  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(373)

我的应用程序有许多按钮,当用户点击任何按钮时,它的显示广告,并且没有计时器,我的意思是,如果你在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;
                }

            });

        }
    });

}

}

5us2dqdw

5us2dqdw1#

看起来您只需要跟踪上次显示广告的时间。 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);
        }
    });
}

}

相关问题