Android:AdView并不总是通过Preference中的CreateView显示

时间:2014-03-28 14:28:20

标签: java android admob preferences

帮助我,stackoverflow,你是我唯一的希望。我想创建一个Android动态壁纸,在壁纸的首选项活动中显示广告。我几乎通过以下示例来实现这一点:

Android Admob advert in PreferenceActivity

这很好用,但此示例使用OnCreateView()加载广告。我注意到OnCreateView会多次调用自定义广告偏好设置。当您在首选项列表中滚动时,甚至会发生这种情况,因此会一次又一次地触发AdRequest。这里也提到了这个问题:

AdView requests new add every time SeekBar is touched

所以我将AdRequest的调用移到onAttachedToHierarchy()并使用成员变量来存储adView。然后,我使用存储的adView将其添加到OnCreateView()中的视图中 - 但是,这会导致奇怪的行为:广告重新加载已停止,但广告仅“有时”显示在列表的顶部喜好。例如,当我加载偏好设置时,广告未显示。我向下滚动,再向上,突然广告就在那里。再次下来,再次快速上升,广告已经消失。 Sloooow up:Ad再次出现。调试显示,在OnCreateView()的多个调用中,有些是“成功”而广告是显示的,有些则不是。它似乎需要在OnCreateView()的最后一次调用时发生“成功”调用,以便广告保留在屏幕上。 我需要做些什么来确保adView始终正确显示?是否可能有不同的方法在首选项屏幕中显示广告?

我的代码如下:

显示广告的偏好:

public class AdPreference extends Preference {

public AdPreference(Context context, AttributeSet attrs, int defStyle) {super    (context, attrs, defStyle);}
public AdPreference(Context context, AttributeSet attrs) {super(context, attrs);}
public AdPreference(Context context) {super(context);}

/** The view to show the ad. */
private AdView adView;

/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "XXXXXXXXXXXXXXX";

@Override 
public void onAttachedToHierarchy(PreferenceManager preferenceManager){
    super.onAttachedToHierarchy(preferenceManager);

    Activity activity = (Activity)getContext();

    // Create an adView.

    adView = new AdView(activity);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);
}

@Override
protected View onCreateView(ViewGroup parent) {
    // this will create the linear layout defined in ads_layout.xml
    View view = super.onCreateView(parent);

    if (adView.getParent()!=null)
    {
        ((LinearLayout)adView.getParent()).removeAllViews();
    }
    ((LinearLayout)view).addView(adView);

    return view;    
}

}

首选项XML的片段:

<?xml version="1.0" encoding="UTF-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:examplens="http://example.com">
<example.AdPreference     android:layout="@layout/ad_preference" />
<Preference android:title="@string/preference1" />
    .
    .
    .
<PreferenceCategory>

</PreferenceCategory>

广告偏好的布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayoutForAd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>

如果问题描述不清楚,您需要更多代码/信息,请告知我们!提前谢谢!

2 个答案:

答案 0 :(得分:0)

不要在偏好设置中展示广告。这是真的糟糕的用户体验。并且PreferenceActvity机制没有很好地设置来处理它。

您的用户在您的偏好设置屏幕上花费了多长时间? 请将广告放在应用主体中。

答案 1 :(得分:0)

我通过做一个小改动来实现它:诀窍是缓存整个&#34; LinearLayout&#34; -View而不是缓存AdView。 AdPreference的代码已更改为:

public class AdPreference extends Preference {

public AdPreference(Context context, AttributeSet attrs, int defStyle) {super    (context, attrs, defStyle);}
public AdPreference(Context context, AttributeSet attrs) {super(context, attrs);}
public AdPreference(Context context) {super(context);}

/** The view to show the ad. */
private static AdView adView;
private static View myCustomView;

/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "XXXXXXXXXXXXXXX";

@Override
protected View onCreateView(ViewGroup parent) {
    // this will create the linear layout defined in ads_layout.xml

    if (myCustomView==null)
    {

        View view = super.onCreateView(parent);

        Activity activity = (Activity)getContext();

        // Create an adView.

        adView = new AdView(activity);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        //adView.

        ((LinearLayout)view).addView(adView);

        myCustomView = view;

        // Create an ad request. Check logcat output for the hashed device ID to
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);

    }
    return myCustomView;    
}

}

这使我按预期为我工作,没有永久广告重新加载,广告仍然保持稳定&#34;在首选项列表中。