同一活动中的AdMob和WebView无法正常工作(Android应用)

时间:2014-12-01 13:10:39

标签: java android webview admob

我无法让adob在xml文件中包含webview标记的活动中工作,横幅广告只出现25%的时间,当它没有显示时我会在日志中收到此消息:"广告是不可见。没有刷新Ad"。继承我的代码

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

<WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="70dp"
     />

<LinearLayout
 android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:layout_marginBottom="5dp" >

<com.google.android.gms.ads.AdView android:id="@+id/adView"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   ads:adSize="BANNER"
                   ads:adUnitId="MY_ID_UNIT"/>

     </LinearLayout>

  </RelativeLayout>

JAVA FILE

package com.example.quickfeed;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.support.v7.app.ActionBarActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;



public class UltimasNoticias extends ActionBarActivity {

 ProgressDialog mProgress;

 private static final String TEST_DEVICE_ID = "MY_ID_UNIT";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ultimas_noticias);
    Intent intent = getIntent();
    WebView myWebView = (WebView) findViewById(R.id.webview);

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mProgress = ProgressDialog.show(this, "Cargando", "Por favor espere un momento...");



 // The "loadAdOnCreate" and "testDevices" XML attributes no longer available.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice(TEST_DEVICE_ID)
        .build();
    adView.loadAd(adRequest);


    myWebView.setWebViewClient(new WebViewClient() {
        // load url
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        // when finish loading page
        public void onPageFinished(WebView view, String url) {
            if(mProgress.isShowing()) {
                mProgress.dismiss();
            }
        }
    });
    // set url for webview to load
    myWebView.loadUrl("http://www.quickfeed.com.ar/app/ultimas.php");
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.ultimas_noticias, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
WebView myWebView = (WebView) findViewById(R.id.webview);

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mProgress = ProgressDialog.show(this, "Cargando", "Por favor espere un momento...");


    myWebView.setWebViewClient(new WebViewClient() {
        // load url
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        // when finish loading page
        public void onPageFinished(WebView view, String url) {
            if(mProgress.isShowing()) {
                mProgress.dismiss();
            }
        }
    });
    // set url for webview to load
    myWebView.loadUrl("http://www.quickfeed.com.ar/app/ultimas.php");
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

继承人清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.quickfeed"
android:versionCode="1"
android:versionName="1.0" >



<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >



    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".UltimasNoticias"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:label="@string/title_activity_ultimas_noticias"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.quickfeed.MainActivity" />
    </activity>


      <activity android:name="com.google.android.gms.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

</manifest>

THE LOGCAT:

12-02 07:36:47.476:I / ActivityManager(2370):在显示0上从pid 4004开始u0 {cmp = com.example.quickfeed / .UltimasNoticias} 12-02 07:36:47.482:I / wm_task_moved(2370):[2,1,0] 12-02 07:36:47.484:I / am_create_activity(2370):[0,920230720,2,com.example.quickfeed / .UltimasNoticias,NULL,NULL,NULL,0] 12-02 07:36:47.492:I / am_pause_activity(2370):[0,630334866,com.example.quickfeed / .MainActivity] 12-02 07:36:47.507:E / NetlinkEvent(1640):NetlinkEvent :: FindParam():参数&#39; TIME_NS&#39;未找到 12-02 07:36:47.539:I / am_on_paused_called(4004):[0,com.example.quickfeed.MainActivity] 12-02 07:36:47.554:I / am_restart_activity(2370):[0,920230720,2,com.example.quickfeed / .UltimasNoticias] 12-02 07:36:47.572:W / AudioTrack(2370):客户拒绝AUDIO_OUTPUT_FLAG_FAST 12-02 07:36:48.053:W / Resources(4004):预装的可绘制资源#0x1080093(android:drawable / sym_def_app_icon)随配置而变化!! 12-02 07:36:48.115:W / art(4004):尝试从IRT中删除本地句柄范围条目,忽略 12-02 07:36:48.503:W / GooglePlayServicesUtil(4004):Google Play服务已过期。需要6171000但找到5053030 12-02 07:36:48.541:I / Ads(4004):开始广告请求。 12-02 07:36:48.578:I / Ads(4004):请将AdActivity的主题设置为@android:style / Theme.Translucent以启用透明背景插页式广告。 12-02 07:36:48.688:I / am_on_resume_called(4004):[0,com.example.quickfeed.UltimasNoticias] 12-02 07:36:48.758:I / Choreographer(4004):跳过31帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:49.069:W / GooglePlayServicesUtil(4004):Google Play服务已过期。需要6171000但找到5053030 12-02 07:36:49.160:E / GooglePlayServicesUtil(4004):由于错误2,GooglePlayServices不可用 12-02 07:36:50.475:I / Choreographer(4004):跳过163帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:51.499:I / art(2805):背景粘滞并发标记扫描GC释放7040(932KB)AllocSpace对象,0(0B)LOS对象,485%免费,1963KB / 6MB,暂停5.136ms总计198.503女士 12-02 07:36:52.883:W / art(4004):尝试从IRT中删除本地句柄范围条目,忽略 12-02 07:36:53.480:I / Choreographer(4004):跳过270帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:53.708:I / Choreographer(2370):跳过106帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:53.770:I / am_activity_launch_time(2370):[0,920230720,com.example.quickfeed / .UltimasNoticias,6224,6224] 12-02 07:36:53.771:I / ActivityManager(2370):显示com.example.quickfeed / .UltimasNoticias:+ 6s224ms 12-02 07:36:54.223:I / Choreographer(2370):跳过43帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:54.908:I / Choreographer(2370):跳过63帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:54.939:I / Choreographer(4004):跳过133帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:55.482:I / sf_frame_dur(54):[com.example.quickfeed / com.example.quickfeed.MainActivity,0,0,0,1,0,0,1] 12-02 07:36:57.204:I / Choreographer(4004):跳过68帧!应用程序可能在其主线程上做了太多工作。 12-02 07:36:57.243:I / chromium(4004):[INFO:CONSOLE(0)]&#34;文档是从带有清单https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.appcache&#34;的应用程序缓存中加载的,源:{{ 3}}(0) 12-02 07:36:57.278:I / chromium(4004):[INFO:CONSOLE(0)]&#34;应用程序缓存检查事件&#34;,来源:https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html(0) 12-02 07:36:57.279:I / chromium(4004):[INFO:CONSOLE(0)]&#34;应用程序缓存检查事件&#34;,来源:https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html(0) 12-02 07:36:57.400:I / chromium(4004):[INFO:CONSOLE(0)]&#34;应用程序缓存NoUpdate事件&#34;,来源:https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html(0) 12-02 07:36:57.405:I / chromium(4004):[INFO:CONSOLE(0)]&#34; Application Cache NoUpdate event&#34;,source:https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html(0) 12-02 07:36:57.501:W / ActivityManager(2370):启动超时已过期,放弃唤醒锁定! 12-02 07:37:00.037:W / Ads(4004):收到广告回复时出现问题。 ErrorCode:0 12-02 07:37:00.362:W / art(4004):尝试从IRT中删除本地句柄范围条目,忽略 12-02 07:37:00.472:W / Ads(4004):无法加载广告:0 12-02 07:37:00.474:I / Choreographer(4004):跳过30帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:05.557:E / NetlinkEvent(1640):NetlinkEvent :: FindParam():参数&#39; TIME_NS&#39;未找到 12-02 07:37:05.570:E / NetlinkEvent(1640):NetlinkEvent :: FindParam():参数&#39; TIME_NS&#39;未找到 12-02 07:37:07.948:I / art(4004):后台粘性并发标记扫描GC释放20322(1273KB)AllocSpace对象,6(96KB)LOS对象,288%免费,5MB / 9MB,暂停62.254ms总计611.250女士 12-02 07:37:17.266:E / NetlinkEvent(1640):NetlinkEvent :: FindParam():参数&#39; TIME_NS&#39;未找到 12-02 07:37:17.280:E / NetlinkEvent(1640):NetlinkEvent :: FindParam():参数&#39; TIME_NS&#39;未找到 12-02 07:37:21.021:I / Choreographer(4004):跳过38帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:21.402:I / Choreographer(4004):跳过36帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:22.265:I / Choreographer(4004):跳过30帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:22.299:I / netstats_mobile_sample(2370):[9036180,9261,552404,5716,8888646,9257,472268,5712,8741567,9158,471901,5689,1417523842185] 12-02 07:37:22.304:I / netstats_wifi_sample(2370):[0,0,0,0,0,0,0,0,0,0,0,0,1417523842185] 12-02 07:37:22.754:I / Choreographer(4004):跳过45帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:23.079:I / Choreographer(4004):跳过30帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:23.418:I / Choreographer(4004):跳过32帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:23.750:I / Choreographer(4004):跳过31帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:25.048:I / Choreographer(4004):跳过36帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:25.950:I / Choreographer(4004):跳过31帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:40.975:D / LightsService(2370):过度延迟设定灯:591ms 12-02 07:37:41.069:D / LightsService(2370):过度延迟设置指示灯:94ms 12-02 07:37:42.948:I / Choreographer(4004):跳过149帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:44.627:I / free_storage_changed(2370):110465024 12-02 07:37:45.174:I / auditd(3196):type = 1400 audit(0.0:24):avc:拒绝{0x10} for comm =&#34; Binder_A&#34; capability = 36 scontext = u:r:system_server:s0 tcontext = u:r:system_server:s0 tclass = capability2 12-02 07:37:45.339:I / Choreographer(4004):跳过97帧!应用程序可能在其主线程上做了太多工作。 12-02 07:37:46.0​​01:I / sf_frame_dur(54):[Cargando,0,0,0,196,83,9,3]

1 个答案:

答案 0 :(得分:0)

你的活动布局很奇怪!删除LinearLayout并使用com.google.android.gms.ads.AdView中的以下标记:android:id =“@ + id / adView”

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

然后尝试不在webview中加载网址。在这种情况下,广告是否在100%的时间内展示?