每7次滑动后不会显示插页式广告

时间:2018-05-23 05:34:15

标签: android facebook ads interstitial

我是Android的新手,我在每7次刷卡后添加了facebook插页式广告代码来展示广告,但我的代码只显示插页式广告一次。你能帮我解决一下如何解决这个问题吗?横幅广告工作正常。但我无法解决插页式广告的任何建议或帮助。

 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.view.MenuItemCompat;
 import android.support.v4.view.PagerAdapter;
 import android.support.v4.view.ViewPager;
 import android.support.v7.app.ActionBarActivity;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.ShareActionProvider;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.TextView;

 import com.facebook.ads.*;

 import com.abc.shareactionprovider.content.ContentItem;

 import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {
// The items to be displayed in the ViewPager
private final ArrayList<ContentItem> mItems = getSampleContent();

// Keep reference to the ShareActionProvider from the menu
private ShareActionProvider mShareActionProvider;

private InterstitialAd interstitialAd;

//final int counter = 0;
private AdView adView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set content view (which contains a CheeseListFragment)
    setContentView(R.layout.sample_main);

    // Retrieve the ViewPager from the content view
    ViewPager vp = (ViewPager) findViewById(R.id.viewpager);

    // Set an OnPageChangeListener so we are notified when a new item is selected
    //vp.setOnPageChangeListener(mOnPageChangeListener);
    vp.addOnPageChangeListener(mOnPageChangeListener);

    // Finally set the adapter so the ViewPager can display items
    vp.setAdapter(mPagerAdapter);

    adView = new AdView(this, "1111122222333333", AdSize.BANNER_HEIGHT_50);

    LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);

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

    interstitialAd = new InterstitialAd(this, "33333344444444");

    interstitialAd.loadAd();

}

// BEGIN_INCLUDE(get_sap)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu resource
    getMenuInflater().inflate(R.menu.main_menu, menu);

    // Retrieve the share menu item
    MenuItem shareItem = menu.findItem(R.id.menu_share);

    // Now get the ShareActionProvider from the item
    mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

    // Get the ViewPager's current item position and set its ShareIntent.
    int currentViewPagerItem = ((ViewPager) findViewById(R.id.viewpager)).getCurrentItem();
    setShareIntent(currentViewPagerItem);

    return super.onCreateOptionsMenu(menu);
}
// END_INCLUDE(get_sap)

/**
 * A PagerAdapter which instantiates views based on the ContentItem's content type.
 */
private final PagerAdapter mPagerAdapter = new PagerAdapter() {
    LayoutInflater mInflater;

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Just remove the view from the ViewPager
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // Ensure that the LayoutInflater is instantiated
        if (mInflater == null) {
            mInflater = LayoutInflater.from(MainActivity.this);
        }

        // Get the item for the requested position
        final ContentItem item = mItems.get(position);

        // The view we need to inflate changes based on the type of content
        switch (item.contentType) {
            case ContentItem.CONTENT_TYPE_TEXT: {
                // Inflate item layout for text
                TextView tv = (TextView) mInflater
                        .inflate(R.layout.item_text, container, false);

                // Set text content using it's resource id
                tv.setText(item.contentResourceId);

                // Add the view to the ViewPager
                container.addView(tv);
                return tv;
            }
            case ContentItem.CONTENT_TYPE_IMAGE: {
                // Inflate item layout for images
                ImageView iv = (ImageView) mInflater
                        .inflate(R.layout.item_image, container, false);

                // Load the image from it's content URI
                iv.setImageURI(item.getContentUri());

                // Add the view to the ViewPager
                container.addView(iv);
                return iv;
            }
        }

        return null;
    }
};

private void setShareIntent(int position) {
    // BEGIN_INCLUDE(update_sap)
    if (mShareActionProvider != null) {
        // Get the currently selected item, and retrieve it's share intent
        ContentItem item = mItems.get(position);
        Intent shareIntent = item.getShareIntent(MainActivity.this);

        // Now update the ShareActionProvider with the new share intent
        mShareActionProvider.setShareIntent(shareIntent);
    }
    // END_INCLUDE(update_sap)
}

/**
 * A OnPageChangeListener used to update the ShareActionProvider's share intent when a new item
 * is selected in the ViewPager.
 */
private final ViewPager.OnPageChangeListener mOnPageChangeListener
        = new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // NO-OP
        if(interstitialAd.isAdLoaded()) {
            if (position % 7 == 0) {
                interstitialAd.show();
            }
        }

    }

    @Override
    public void onPageSelected(int position) {
        setShareIntent(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // NO-OP
    }
};

/**
 * @return An ArrayList of ContentItem's to be displayed in this sample
 */
static ArrayList<ContentItem> getSampleContent() {
    ArrayList<ContentItem> items = new ArrayList<ContentItem>();

    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i1.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i2.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i3.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i4.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i5.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i6.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i7.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i8.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i9.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i10.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i11.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i12.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i13.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i14.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i15.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i16.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i17.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i18.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i19.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i20.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i21.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i22.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i23.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i24.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i25.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i26.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i27.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i28.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i29.jpg"));
    items.add(new ContentItem(ContentItem.CONTENT_TYPE_IMAGE, "i30.jpg"));


    return items;
}

 }

1 个答案:

答案 0 :(得分:1)

是的,它只显示一次,因为您只在<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="tknz-list"> <div class="tknz-token" data-token="t"><span class="tknz-token-label">t</span><span class="tknz-token-x">×</span></div> <div class="tknz-token" data-token="u"><span class="tknz-token-label">u</span><span class="tknz-token-x">×</span></div> <div class="tknz-token" data-token="i"><span class="tknz-token-label">i</span><span class="tknz-token-x">×</span></div> </div>中一次性加载广告,而不是只显示一次。但是,当您显示广告时,您必须再次加载广告,以便在接下来的7次观看后显示广告。因此,请为此创建一种方法,并在您的广告将要显示/显示之前每次调用。

这是示例代码: -

onCreate()

并将此广告ID放入项目的 string.xml

public void loadInterstitial() {
        // Instantiate an InterstitialAd object
        AdSettings.addTestDevice("350cf676a5848059b96313bdddc21a35");
        interstitialAd = new InterstitialAd(MainActivity.this, getString(R.string.ins_ads_id));
        interstitialAd.loadAd();
        // Set listeners for the Interstitial Ad
        interstitialAd.setAdListener(new InterstitialAdListener() {
            @Override
            public void onInterstitialDisplayed(Ad ad) {
                Log.v("OkHttp", ad.toString());
            }

            @Override
            public void onInterstitialDismissed(Ad ad) {
                Log.v("OkHttp", ad.toString());    
            }

            @Override
            public void onError(Ad ad, AdError adError) {
                Log.v("OkHttp", ad.toString() + "   " + adError.getErrorCode() + "   " + adError.getErrorMessage());
            }

            @Override
            public void onAdLoaded(Ad ad) {
                Log.v("OkHttp", ad.toString());
                showInterstitial();
            }

            @Override
            public void onAdClicked(Ad ad) {
                Log.v("OkHttp", ad.toString());
            }

            @Override
            public void onLoggingImpression(Ad ad) {
                Log.v("OkHttp", ad.toString());
            }
        });
    }

    public void showInterstitial() {
        interstitialAd.show();
    }

将您的<string name="ins_ads_id">222591425151579_222592145151XXX</string> 代码更改为此代码。

onPageScrolled