I am using interstitial ads from Admob in my app. Google rejects my app and claims ad fraud. They say that my app displays an ad after exiting the app. I have changed my ad code to be sure that ad loading and ad displaying only happens when the activity is running and the app is in the foreground. Still they reject my app and claim ad fraud. I am just doing basic interstitial code here:
implementation 'com.google.android.gms:play-services-ads:19.7.0'
public class AdManager {
boolean activityStarted;
public void onStart() {
activityStarted=true;
}
public void onStop() {
activityStarted=false;
}
public void initializeAds(Context context) {
MobileAds.initialize(context);
MobileAds.setAppMuted(true);
List<String> testDeviceIds = Arrays.asList("some id");
RequestConfiguration configuration =
new RequestConfiguration.Builder().setTestDeviceIds(testDeviceIds).build();
MobileAds.setRequestConfiguration(configuration);
}
public InterstitialAd getInterstialAdWithUnit(Context context) {
InterstitialAd mInterstitialAd = new InterstitialAd(context);
mInterstitialAd.setAdUnitId(BuildConfig.INTERSTITIAL_AD_UNIT_ID);
setAdListener(mInterstitialAd);
checkNewAdNeeded(mInterstitialAd);
return mInterstitialAd;
}
public void setAdListener(InterstitialAd mInterstitialAd) {
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
checkNewAdNeeded(mInterstitialAd);
}
});
}
public void showAdIfNeeded(InterstitialAd mInterstitialAd) {
if (activityStarted && mInterstitialAd.isLoaded())
mInterstitialAd.show();
else {
checkNewAdNeeded(mInterstitialAd);
}
}
public void requestNewInterstitial(InterstitialAd mInterstitialAd) {
AdRequest adRequest = new AdRequest.Builder()
.build();
mInterstitialAd.loadAd(adRequest);
}
public void checkNewAdNeeded(InterstitialAd mInterstitialAd) {
//do not load ad when acticity is stopped
if (activityStarted && !mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded())
requestNewInterstitial(mInterstitialAd);
}
}
public class FullscreenActivity extends AppCompatActivity {
AdManager adManager=new AdManager();
public void onButtonClicked() {
//call another activity when Button is clicked and wait for return to display ad
activity.startActivityForResult(intent, requestCode);
runOnUiThread(new Runnable() {
@Override
public void run() {
adManager.checkNewAdNeeded(mInterstitialAd);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//back from other activity, try to display ads only if app has focus
if ( from activity xy) {
if (mHasFocus)
showAdWhileHasFocus();
else
showAdWhenHasFocus=true;
}
}
@Override
protected void onStart() {
super.onStart();
adManager.onStart();
}
@Override
protected void onStop() {
adManager.onStop();
super.onStop();
}
boolean showAdWhenHasFocus;
public void showAdWhileHasFocus() {
showAdWhenHasFocus=false;
adManager.showAdIfNeeded(mInterstitialAd,promotionAndAdsEvent);
}
boolean mHasFocus=false;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus) {
} else {
if (showAdWhenHasFocus)
showAdWhileHasFocus();
}
super.onWindowFocusChanged(hasFocus);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}