Почему не показывается межстраничная реклама? Реклама должна быть показана при первом нажатии на кнопку refresh и на каждом 30 нажатии. Но при первом нажатии я получаю всплывающие окна с
ок
и
реклама загружена
но нечего не происходи при 30 нажатии я получаю те же сообщения но уже рекламу показывает пре последующих каждых 30 нажатий я получаю всплывающие окна с
ок
и
реклама не загружена
public class MainActivity extends AppCompatActivity {
WebView webView;
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";
private static final String TAG = "MyActivity";
private int clickCount = 0;
private SharedPreferences prefs;
private static final String JS_INTERFACE_NAME = "AndroidInterface";
private InterstitialAd mInterstitialAd;
@SuppressLint("WrongViewCast")
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
loadAd();
WebSettings webSettings = webView.getSettings();
webView.getSettings().setJavaScriptEnabled(true);
webView.clearCache(true);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.setDrawingCacheEnabled(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowFileAccess(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
final Activity activity = this;
webView.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
webView.loadUrl("file:///android_asset/error.html");
}
});
// Добавляем интерфейс для взаимодействия с JavaScript
webView.addJavascriptInterface(new WebAppInterface(), JS_INTERFACE_NAME);
webView.loadUrl("file:///android_asset/index.html");
}
public void loadAd() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(
this,
AD_UNIT_ID,
adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
MainActivity.this.mInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
Toast.makeText(MainActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
interstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
// Make sure to set your reference to null so you don't
// show it a second time.
MainActivity.this.mInterstitialAd = null;
Log.d("TAG", "The ad was dismissed.");
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
// Make sure to set your reference to null so you don't
// show it a second time.
MainActivity.this.mInterstitialAd = null;
Log.d("TAG", "The ad failed to show.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d("TAG", "The ad was shown.");
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mInterstitialAd = null;
String error =
String.format(
"domain: %s, code: %d, message: %s",
loadAdError.getDomain(), loadAdError.getCode(), loadAdError.getMessage());
Toast.makeText(
MainActivity.this, "onAdFailedToLoad() with error: " + error, Toast.LENGTH_SHORT)
.show();
}
});
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (mInterstitialAd != null) {
Toast.makeText(MainActivity.this, "Загружена", Toast.LENGTH_SHORT).show();
mInterstitialAd.show(MainActivity.this);
} else {
Toast.makeText(MainActivity.this, "Не загружена", Toast.LENGTH_SHORT).show();
}
}
// Класс для взаимодействия между WebView и JavaScripts
public class WebAppInterface {
@JavascriptInterface
public void shareContent(String content) {
// Выполните здесь свой код для обработки и передачи информации в социальные сети
Intent intent = new Intent(
Intent.ACTION_SEND
);
intent.setType("text/plain");
String Body = content;
String Sub = "https://play.google.com/store/apps/details?id=com.buratinoapps.cryptocurrency.faucet.rotator";
intent.putExtra(Intent.EXTRA_TEXT, Body);
intent.putExtra(Intent.EXTRA_TEXT, Sub);
startActivity(Intent.createChooser(intent, "Share using"));
Toast.makeText(MainActivity.this, "Поделиться: " + content, Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public void showedAds() {
clickCount++;
if (clickCount == 1 || clickCount % 30 == 0) {
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
showInterstitial();
}
}
}
}