0

I am Working on Android Application that Implements Admob Interstitial Ad. What I want is the Handler Lock a Screen and Show Progress bar for two seconds and then Show Interstitial ad.

What I Have done is I animated the Interstitial Ad by Creating an Animation and Call it just After the InterstitialAd.show Method.

 mInterstitialAd!!.adListener = object: AdListener() {
        override fun onAdLoaded() {
            btnstart.setOnClickListener(View.OnClickListener {


                if (mInterstitialAd!!.isLoaded) {
                    mInterstitialAd!!.show()
                    overridePendingTransition(R.anim.in_left, R.anim.nav_default_exit_anim)
                } else {
                    sharpref!!.privacyPolicyAccepted()
                    if (!sharpref!!.isFirstTimeLaunch()) {
                        val intent = Intent(applicationContext, SliderActivity::class.java)
                        startActivity(intent)
                    } else {
                        val intent = Intent(applicationContext, MainActivity::class.java)
                        startActivity(intent)
                    }
                }
            })
        }

The Code Animated the overridePendingTransition(R.anim.in_left, R.anim.nav_default_exit_anim) Interstiial ad but I want is Show Pogress bar for two seconds before interstitail ad opens.

Logcat After Crash

    2020-12-04 10:29:03.603 577-1239/? E/PackageManager: liangshuang2, setEnabledSetting, packageName=com.google.android.apps.youtube.mango,,className=androidx.work.impl.background.systemjob.SystemJobService,,,newState=1,,flags=1,,userId=0,,callingPackage=null
2020-12-04 10:29:04.309 577-5614/? E/PackageManager: liangshuang2, setEnabledSetting, packageName=com.google.android.apps.youtube.mango,,className=androidx.work.impl.background.systemalarm.RescheduleReceiver,,,newState=1,,flags=1,,userId=0,,callingPackage=null
2020-12-04 10:35:56.218 22376-22565/? E/AndroidRuntime: FATAL EXCEPTION: Timer-0
    Process: com.techpaliyal.bottomcutoutnavigation, PID: 22376
    java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
        at android.widget.Toast$TN.<init>(Toast.java:405)
        at android.widget.Toast.<init>(Toast.java:117)
        at android.widget.Toast.makeText(Toast.java:292)
        at android.widget.Toast.makeText(Toast.java:282)
        at 

com.maximus.technologies.views.activities.SplashActivity$setClickListeners$4$onAdLoaded$1$$special$$inlined$timerTask$1.run(Timer.kt:149)
        at java.util.TimerThread.mainLoop(Timer.java:555)
        at java.util.TimerThread.run(Timer.java:505)
2020-12-04 10:36:00.841 577-739/? E/UsbDeviceManager: unknown state HWDISCONNECTED
    
        --------- beginning of main
    2020-12-04 10:36:00.921 763-763/? E/licong: onReceive
    2020-12-04 10:36:00.929 577-596/? E/BatteryExternalStatsWorker: no controller energy info supplied for wifi
2020-12-04 10:36:00.942 577-596/? E/BatteryExternalStatsWorker: no controller energy info supplied for bluetooth
2020-12-04 10:36:01.171 577-596/? E/BatteryExternalStatsWorker: no controller energy info supplied for wifi

Updated Code

 btnstart.setOnClickListener(View.OnClickListener {

        mInterstitialAd!!.adListener = object: AdListener() {
            override fun onAdLoaded() {
                
                if (mInterstitialAd!!.isLoaded) {
                    mInterstitialAd!!.show()
                    overridePendingTransition(R.anim.in_left, R.anim.nav_default_exit_anim)
                } else {
                    sharpref!!.privacyPolicyAccepted()
                    if (!sharpref!!.isFirstTimeLaunch()) {
                        val intent = Intent(applicationContext, SliderActivity::class.java)
                        startActivity(intent)
                    } else {
                        val intent = Intent(applicationContext, MainActivity::class.java)
                        startActivity(intent)
                    }
                }
            }

            override fun onAdFailedToLoad(adError: LoadAdError) {
                if (mInterstitialAd!!.isLoaded) {
                    mInterstitialAd!!.show()
                    overridePendingTransition(R.anim.in_left, R.anim.nav_default_exit_anim)
                } else {
                    sharpref!!.privacyPolicyAccepted()

                    if (!sharpref!!.isFirstTimeLaunch()) {
                        val intent = Intent(applicationContext, SliderActivity::class.java)
                        startActivity(intent)
                    } else {
                        val intent = Intent(applicationContext, MainActivity::class.java)
                        startActivity(intent)
                    }
                }
            }

            override fun onAdOpened() {}

            override fun onAdClicked() {
                // Code to be executed when the user clicks on an ad.
            }

            override fun onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            override fun onAdClosed() {
                if(!sharpref!!.isFirstTimeLaunch()) {
                    val intent = Intent(applicationContext, SliderActivity::class.java)
                    startActivity(intent)
                }
                else
                {
                    val intent = Intent(applicationContext, MainActivity::class.java)
                    startActivity(intent)
                }
            }
        }
    })


}
Usman Ali
  • 425
  • 1
  • 9
  • 31
  • i do this in java using ```CountDownTimer``` – Sara García Dec 04 '20 at 04:16
  • @SaraGarcía I want is the Activity stuck and show progress bar for two seconds and then show interstitial ad – Usman Ali Dec 04 '20 at 04:17
  • 1
    i think u coding in Kotlin. i don't know anything about it but here how to do that in Java. try to rewrite it in Kotlin. ```ProgressDialog pd = new ProgressDialog(this); pd.setMessage("Loading ad..."); pd.show(); new CountDownTimer(2000, 1000){ @Override public void onTick(long p1) { } @Override public void onFinish() {    if(interstitialAd.isLoaded()){                    interstitialAd.show();                 }                pd.dismiss(); } }.start();``` – Sara García Dec 04 '20 at 04:27
  • Where Do I need to put this, Like I want to open Interstitial ad after progress bar for two seconds – Usman Ali Dec 04 '20 at 04:33

1 Answers1

1

Please refer to this answer for more detail on timers.

Quick Overview :-

The answer in the link shows different ways to schedule some work you have after a particular time. You can use this according to your requirements.

As much as I have understood from your question you can have somewhat like this :-

if (mInterstitialAd!!.isLoaded) {
     // show Progress Bar visibility here 
        Handler().postDelayed({
            //Hide Progress Bar visibility here 
            mInterstitialAd!!.show()
            overridePendingTransition(R.anim.in_left,R.anim.nav_default_exit_anim)
            }, 2000)
   }

In this method after your if (mInterstitialAd!!.isLoaded) you can also have a timer(more details in the link provided) and then you can show your ad with your animation if you want.

WhiteSpidy.
  • 1,107
  • 1
  • 6
  • 28