2017-03-08 53 views
0

我刚刚接触android,最近我开发了一个在WebView上运行的应用程序,意味着我的活动中放置了一个webview,通过html,js页面加载webview来完成gamin活动。如何从Web视图加载插页式广告

这里我的要求是“如何从网页文件加载插页式广告(HTML,JS),我用Google搜索最相关的离子应用程序的建议,但我的应用程序是AndroidStudio相关。 所以请帮助

回答

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(); 
 
     } 
 
    } 
 
}

+0

Hai Raman,感谢您的回复,但在上面的webview和活动之间的沟通。在我看来,我需要加载和InterstitialAd时,从webview中引发事件。上面的代码没有任何这样的交流。 – RamBen

0

从加载的WebView的广告不是那么简单,但可以这样做。因此,让我们采取以下为例e:

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(); });" 
     ); 
    } 
    } 
}