Code: Select all
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.example.cointoss.databinding.ActivitySettingBinding
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
class SettingActivity : AppCompatActivity() {
private lateinit var binding: ActivitySettingBinding
private var interstitialAd: InterstitialAd? = null
override fun onCreate(savedInstanceState: Bundle?) {
MobileAds.initialize(this) {}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.statusBarColor = ContextCompat.getColor(this, R.color.lightgrey)
}
binding= ActivitySettingBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
setContentView(binding.root)
loadAd()
// loadInterstitial()
binding.shareTv.setOnClickListener {
val intent= Intent().apply{
type = "text/plain"
action = Intent.ACTION_SEND
}
startActivity(intent)
}
binding.feedbackTv.setOnClickListener {
val recipient = "[email protected]"
val subject = "App Feedback"
val body = "I want to share my thoughts on your app..."
val uriString = String.format("mailto:%s?subject=%s&body=%s",
Uri.encode(recipient),
Uri.encode(subject),
Uri.encode(body)
)
val uri = Uri.parse(uriString)
val emailIntent = Intent(Intent.ACTION_SENDTO, uri).apply {
setPackage("com.google.android.gm")
}
startActivity(emailIntent)
}
binding.rateTv.setOnClickListener {
RateAppDialog.show(this)
}
binding.adbtn.setOnClickListener {
// showAd()
if (interstitialAd != null) {
interstitialAd?.show(this)
} else {
showAd() // fallback if ad is not ready
}
}
}
// private fun loadInterstitial() {
// val adRequest = AdRequest.Builder().build()
// InterstitialAd.load(
// this,
// "ca-app-pub-3940256099942544/1033173712", // replace with real ad unit ID
// adRequest,
// object : InterstitialAdLoadCallback() {
// override fun onAdLoaded(ad: InterstitialAd) {
// interstitialAd = ad
// setAdCallbacks()
// }
//
// override fun onAdFailedToLoad(error: LoadAdError) {
// interstitialAd = null
// }
// }
// )
// }
//
// private fun setAdCallbacks() {
// interstitialAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
// override fun onAdDismissedFullScreenContent() {
// interstitialAd = null
// loadInterstitial() // preload next ad
// doAction()
// }
// }
// }
//
// private fun doAction() {
// Toast.makeText(this, "Button action executed", Toast.LENGTH_SHORT).show()
// // your actual button logic here
// }
private fun showAd() {
if (interstitialAd != null) {
interstitialAd?.show(this)
Log.d("TAG", "Ad displayed. Loading next ad.")
// After showing an ad, it can't be reused. Load a new one immediately.
loadAd()
} else {
Log.d("TAG", "Interstitial ad is not ready yet.")
// Optional: You can try to load an ad again if it's not ready
// loadAd()
}
}
private fun loadAd(){
val adRequest = AdRequest.Builder().build()
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest, object : InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
interstitialAd = null
}
override fun onAdLoaded(ad: InterstitialAd) {
interstitialAd = ad
}
})
}
}
Code: Select all
```
the below are my dependency for app level build.gradle file
``` implementation("androidx.datastore:datastore-preferences:1.2.0")
implementation(platform("com.google.firebase:firebase-bom:33.1.0"))
implementation("com.google.firebase:firebase-config-ktx")
implementation("com.google.firebase:firebase-analytics:21.6.2")
implementation("com.google.android.gms:play-services-ads:24.8.0")
Mobile version