应用程序onStop()导致循环

时间:2019-03-03 15:53:41

标签: java android

我实际上正在以Android应用程序的意外行为运行,非常感谢您提供一些建议。 我正在尝试设置一个WebView,以在滑动刷新页面后以及应用返回到前台后显示一个广告。 问题是,当应用程序进入后台时,它将加载一个Add并显示它,从而自动将应用程序置于前台。看来我使用的onStop()错误,但我确实无法为我的问题找到解决方案。 任何帮助将不胜感激,因为在Coding上还很新

public class MainActivity extends AppCompatActivity {

    private InterstitialAd interstitialAd;
    private final String TAG = MainActivity.class.getSimpleName();
    private WebView wv;

    SwipeRefreshLayout swipe;

    public void loadWebView(){
        wv = (WebView) findViewById(R.id.mywebview);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        wv.getSettings().setAppCacheEnabled(false);
        wv.setWebViewClient(new WebViewClient());
        wv.loadUrl("https://www.google.de");
        swipe.setRefreshing(true);
        wv.reload();
        swipe.setRefreshing(false);
        Werbung();

    }
    public void Werbung() {

        interstitialAd = new InterstitialAd(this, "XXXXXXXXXX_XXXXXXXX");
        interstitialAd.loadAd();

        interstitialAd.setAdListener(new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                // Interstitial ad displayed callback
                Log.e(TAG, "Interstitial ad displayed.");
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                // Interstitial dismissed callback
                Log.e(TAG, "Interstitial ad dismissed.");

            }

            @Override
            public void onError(Ad ad, AdError adError) {
                // Ad error callback
                Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                // Interstitial ad is loaded and ready to be displayed
                Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
                // Show the ad
                interstitialAd.show();
            }

            @Override
            public void onAdClicked(Ad ad) {
                // Ad clicked callback
                Log.d(TAG, "Interstitial ad clicked!");
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                // Ad impression logged callback
                Log.d(TAG, "Interstitial ad impression logged!");
            }
        });
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Check whether we're recreating a previously destroyed instance
        setContentView(R.layout.activity_main);

        interstitialAd = new InterstitialAd(this, "2314489645452951_2314491755452740");
        interstitialAd.loadAd();
        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadWebView();
            }

        });

    }

    public void onBackPressed() {
        if (wv.canGoBack()) {
            wv.goBack();
        } else {
            super.onBackPressed();
        }
    }
    @Override
    public void onStart(){
        super.onStart();
        loadWebView();
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        if (interstitialAd != null) {
            interstitialAd.destroy();
        }

    }
    @Override
    public void onPause(){
        super.onPause();
        if (interstitialAd != null) {
            interstitialAd.destroy();
        }
        interstitialAd.loadAd();
     }
     @Override
    public void onStop(){
        super.onStop();
        if (interstitialAd != null){
            interstitialAd.destroy();
        }
        interstitialAd.loadAd();
     }

    }

0 个答案:

没有答案