具有相同布局的WebView的AdMob广告?

时间:2014-06-18 13:28:40

标签: android webview admob

我尝试使用WebView实施AdMob广告。对于我的生活,即使在日志中我也无法展示任何东西。它几乎就像广告没有被调用一样 - 我的AdMob帐户没有显示任何请求。起初我以为WebView只占用了布局中的所有空间,但现在我再次猜测自己,因为我没有看到任何与广告相关的LogCat输出。

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <com.google.ads.AdView android:id="@+id/adView"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 ads:adUnitId="[myIDishere]"
                 ads:adSize="BANNER"
                 ads:testDevices="[myhashishere]"
                 ads:loadAdOnCreate="true"/>
  <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

  </LinearLayout>

然后是我的主要活动:

public class MainActivity extends Activity {



//StartServicesCheck


private boolean checkPlayServices() {
      int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
      if (status != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
          showErrorDialog(status);
        } else {
          Toast.makeText(this, "This device is not supported.", 
              Toast.LENGTH_LONG).show();
          finish();
        }
        return false;
      }
      return true;
    } 

    void showErrorDialog(int code) {
      GooglePlayServicesUtil.getErrorDialog(code, this, 
          REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
    }

    static final int REQUEST_CODE_RECOVER_PLAY_SERVICES = 1001;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      switch (requestCode) {
        case REQUEST_CODE_RECOVER_PLAY_SERVICES:
          if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Google Play Services must be installed.",
                Toast.LENGTH_SHORT).show();
            finish();
          }
          return;
      }
      super.onActivityResult(requestCode, resultCode, data);
    }



//EndServicesCheck

private WebView mWebview ;

@Override
public void onBackPressed() {
    if(mWebview.canGoBack() == true){
        mWebview.goBack();
    }else{
        super.onBackPressed();
    }
}


public class BannerExample extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Look up the AdView as a resource and load a request.
        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
      }
    }


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    mWebview  = new WebView(this);

    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
    mWebview.getSettings().setBuiltInZoomControls(true);

    if(android.os.Build.VERSION.SDK_INT >= 11) {
        mWebview.getSettings().setDisplayZoomControls(false);
    }

    final Activity activity = this;

    mWebview.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
    });

    mWebview .loadUrl("[webpagehere]");
    setContentView(mWebview );


    mWebview.setWebViewClient(new WebViewClient(){

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            mWebview.scrollTo(681, 100);
        }

    }
    );

}

}

我的编码本身有问题吗?我有点倾向于这种方式,因为我没有看到请求或记录输出。

1 个答案:

答案 0 :(得分:1)

WebView更改为以下内容。 AdView

隐藏了您的WebView
<WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:weight="1" />

我还建议将AdView的宽度设置为match_parent,将adSize设置为"SMART_BANNER" SMART_BANNER广告会根据设备的方向和宽度自动调整大小。

修改

我刚刚完成了你的代码。它有很多小错误。将其更改为以下内容:

public class MainActivity extends Activity {



//StartServicesCheck

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
mWebview  = (WebView) findViewById(R.id.webview);

mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);

if(android.os.Build.VERSION.SDK_INT >= 11) {
    mWebview.getSettings().setDisplayZoomControls(false);
}

final Activity activity = this;

mWebview.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
    }
});

mWebview .loadUrl("[webpagehere]");
setContentView(mWebview );


mWebview.setWebViewClient(new WebViewClient(){

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        mWebview.scrollTo(681, 100);
    }

}
);

}


private boolean checkPlayServices() {
  int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
  if (status != ConnectionResult.SUCCESS) {
    if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
      showErrorDialog(status);
    } else {
      Toast.makeText(this, "This device is not supported.", 
          Toast.LENGTH_LONG).show();
      finish();
    }
    return false;
  }
  return true;
} 

void showErrorDialog(int code) {
  GooglePlayServicesUtil.getErrorDialog(code, this, 
      REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
}

static final int REQUEST_CODE_RECOVER_PLAY_SERVICES = 1001;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  switch (requestCode) {
    case REQUEST_CODE_RECOVER_PLAY_SERVICES:
      if (resultCode == RESULT_CANCELED) {
        Toast.makeText(this, "Google Play Services must be installed.",
            Toast.LENGTH_SHORT).show();
        finish();
      }
      return;
  }
  super.onActivityResult(requestCode, resultCode, data);
}



//EndServicesCheck

private WebView mWebview ;
@Override
public void onBackPressed() {
    if(mWebview.canGoBack() == true){
        mWebview.goBack();
    }else{
        super.onBackPressed();
    }
}




}

PS:对于搞砸的缩进抱歉:P