0

我是 android 新手,最近我开发了一个在 WebView 上运行的应用程序,这意味着在我的活动中,我放置了一个 webview,通过 html、js 页面通过将这些页面加载到 webview 来执行 gamin 活动。

在这里我的请求是“如何从网页文件(html,js)加载插页式广告我在谷歌上搜索了大多数与 Ionic App 相关的建议,但我的应用程序与 AndroidStudio 相关。所以请帮忙

4

2 回答 2

1

从 WebView 加载广告并不是那么简单,但可以做到。所以让我们看下面的例子:

class MainActivity extends AppCompatActivity {
  protected InterstitialAd mInterstitialAd;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // load interstitial ad
    mInterstitialAd = new InterstitialAd(getContext());
    mInterstitialAd.setAdUnitId("YOUR_AD_ID");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());

    WebView browser = findViewById(R.id.webview);
    browser.addJavascriptInterface(new InterceptorJavaScript(context), "Interceptor");

    WebSettings webSettings = browser.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);

    browser.setWebViewClient(new CapturingWebViewClient());

    browser.loadUrl("http://website.com");
  }

  /**
   * This class will handle all JS events from the UI back to 
   * Java code and will trigger the Interstitial ad
   */
  private class InterceptorJavaScript {
    Context webViewContext;

    /**
     * Instantiate the interface and set the context
     */
    InterceptorJavaScript(Context webView) {
      webViewContext = webView;
    }

    @JavascriptInterface
    @SuppressWarnings("unused")
    public void startInterstitial() {
      // we need to run it on the main UI thread
      Handler mHandler = new Handler(Looper.getMainLooper());
      mHandler.post(new Runnable() {
        @Override
        public void run() {
          if (MainActivity.mInterstitialAd.isLoaded()) {
            MainActivity.mInterstitialAd.show();
          }
          // preload new ad
          BrowserFragment.mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
      });
    }
  }

  private class CapturingWebViewClient extends WebViewClient {
    /**
     * This method will inject the JavaScritp that will trigger 
     * your Java code and show the interstitial in the main UI thread
     */
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        view.loadUrl(
          "javascript:Interceptor.startInterstitial();"
        );
        // Attach JS event to your button so you can call the JS function. I've used jQuery just for simplicity
        view.loadUrl(
          "javascript:$('#btn').on('click', function(){ Interceptor.startInterstitial(); });"
        );
    }
  }
}
于 2017-10-31T15:52:25.280 回答
0

你好这是代码....

import android.os.Bundle; import android.view.Window; import android.view.WindowManager;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

public class a4 extends AppCompatActivity {

    AdView mAdView;
    InterstitialAd mInterstitialAd;
    WebView WebViewWithCSS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_a4);

        WebViewWithCSS = (WebView)findViewById(R.id.webView);

        WebSettings webSetting = WebViewWithCSS.getSettings();
        webSetting.setJavaScriptEnabled(true);

        WebViewWithCSS.setWebViewClient(new WebViewClient());
        WebViewWithCSS.loadUrl("file:///android_asset/4.html");

        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        mAdView.loadAd(adRequest);

        mInterstitialAd = new InterstitialAd(this);

        // set the ad unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));

        adRequest = new AdRequest.Builder()
                .build();

        // Load ads into Interstitial Ads
        mInterstitialAd.loadAd(adRequest);

        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
            }
        });
    }

    @Override
    public void onPause() {
        if (mAdView != null) {
            mAdView.pause();
        }
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdView != null) {
            mAdView.resume();
        }
    }

    @Override
    public void onDestroy() {
        if (mAdView != null) {
            mAdView.destroy();
        }
        super.onDestroy();
    }


    private void showInterstitial() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }
}

于 2017-03-28T18:05:13.520 回答