插页式广告不是每次点击都展示广告,而是每隔几秒展示一次广告

时间:2018-01-16 09:59:32

标签: java android admob interstitial google-admob

我是Android新手仅仅3个月,在我的代码中,每隔几秒就会显示插页式广告,而不是每次点击都无法显示问题,无法找到与我的要求相似的任何示例。

我的要求是每次点击都应显示非页内广告。

非常感谢任何建议或帮助。

根据堆叠溢出帖子,我已经整合了广告,但根据我的要求不能正常工作,我们非常感谢您的帮助。

代码:

public class AttractionListFragment extends Fragment {

private AttractionAdapter mAdapter;
private LatLng mLatestLocation;
private int mImageSize;
private boolean mItemClicked;
private InterstitialAd mInterstitialAd;

public AttractionListFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Load a larger size image to make the activity transition to the detail screen smooth
    mImageSize = getResources().getDimensionPixelSize(R.dimen.image_size)
            * Constants.IMAGE_ANIM_MULTIPLIER;

    mLatestLocation = Utils.getLocation(getActivity());
    List<Attraction> attractions = loadAttractionsFromLocation(mLatestLocation);
    mAdapter = new AttractionAdapter(getActivity(), attractions);

    final View view = inflater.inflate(R.layout.fragment_main, container, false);
    AttractionsRecyclerView recyclerView =
            (AttractionsRecyclerView) view.findViewById(android.R.id.list);
    recyclerView.setEmptyView(view.findViewById(android.R.id.empty));
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(mAdapter);

    MobileAds.initialize(getActivity(), "ca-app-pub-3940256099942544~3347511713");
    mInterstitialAd = new InterstitialAd(this.getContext());
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

    AdRequest request = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();

    mInterstitialAd.loadAd(request);

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            if (mInterstitialAd.isLoaded())
            mInterstitialAd.show();
        }

     /*   public void onAdClosed() {
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        } */
    });

    return view;
}

@Override
public void onResume() {
    super.onResume();
    mItemClicked = false;
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
            mBroadcastReceiver, UtilityService.getLocationUpdatedIntentFilter());
}

@Override
public void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mBroadcastReceiver);
}

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Location location =
                intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED);
        if (location != null) {
            mLatestLocation = new LatLng(location.getLatitude(), location.getLongitude());
            mAdapter.mAttractionList = loadAttractionsFromLocation(mLatestLocation);
            mAdapter.notifyDataSetChanged();
        }
    }
};

private static List<Attraction> loadAttractionsFromLocation(final LatLng curLatLng) {
    String closestCity = TouristAttractions.getClosestCity(curLatLng);
    if (closestCity != null) {
        List<Attraction> attractions = ATTRACTIONS.get(closestCity);
        if (curLatLng != null) {
            Collections.sort(attractions,
                    new Comparator<Attraction>() {
                        @Override
                        public int compare(Attraction lhs, Attraction rhs) {
                            double lhsDistance = SphericalUtil.computeDistanceBetween(
                                    lhs.location, curLatLng);
                            double rhsDistance = SphericalUtil.computeDistanceBetween(
                                    rhs.location, curLatLng);
                            return (int) (lhsDistance - rhsDistance);
                        }
                    }
            );
        }
        return attractions;
    }
    return null;
}

private class AttractionAdapter extends RecyclerView.Adapter<ViewHolder>
        implements ItemClickListener {

    public List<Attraction> mAttractionList;
    private Context mContext;

    public AttractionAdapter(Context context, List<Attraction> attractions) {
        super();
        mContext = context;
        mAttractionList = attractions;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.list_row, parent, false);
        return new ViewHolder(view, this);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Attraction attraction = mAttractionList.get(position);

        holder.mTitleTextView.setText(attraction.name);
        holder.mDescriptionTextView.setText(attraction.description);
        Glide.with(mContext)
                .load(attraction.imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.empty_photo)
                .override(mImageSize, mImageSize)
                .into(holder.mImageView);

        String distance =
                Utils.formatDistanceBetween(mLatestLocation, attraction.location);
        if (TextUtils.isEmpty(distance)) {
            holder.mOverlayTextView.setVisibility(View.GONE);
        } else {
            holder.mOverlayTextView.setVisibility(View.VISIBLE);
            holder.mOverlayTextView.setText(distance);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mAttractionList == null ? 0 : mAttractionList.size();
    }

    @Override
    public void onItemClick(View view, int position) {
        if (!mItemClicked) {
            mItemClicked = true;
            View heroView = view.findViewById(android.R.id.icon);
            DetailActivity.launch(
                    getActivity(), mAdapter.mAttractionList.get(position).name, heroView);
        }

    }
}

private static class ViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener {

    TextView mTitleTextView;
    TextView mDescriptionTextView;
    TextView mOverlayTextView;
    ImageView mImageView;
    ItemClickListener mItemClickListener;

    public ViewHolder(View view, ItemClickListener itemClickListener) {
        super(view);
        mTitleTextView = (TextView) view.findViewById(android.R.id.text1);
        mDescriptionTextView = (TextView) view.findViewById(android.R.id.text2);
        mOverlayTextView = (TextView) view.findViewById(R.id.overlaytext);
        mImageView = (ImageView) view.findViewById(android.R.id.icon);
        mItemClickListener = itemClickListener;
        view.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        mItemClickListener.onItemClick(v, getAdapterPosition());

    }
}

interface ItemClickListener {
    void onItemClick(View view, int position);
}

}

3 个答案:

答案 0 :(得分:0)

在您的代码中,您会在加载后立即展示广告,而不是在用户点击按钮时显示广告

@Override
    public void onAdLoaded() {
        super.onAdLoaded();
        if (mInterstitialAd.isLoaded())
        mInterstitialAd.show();
    }

在这里你需要删除mInterstitialAd.show();因为它会导致广告在加载后立即显示

而是在按钮点击事件中显示广告:

myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded())
                mInterstitialAd.show();
        }
    });

答案 1 :(得分:0)

花了2天后我才发现问题 上述问题的解决方案如下:

必要的代码更改:

  private static class ViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener {

    TextView mTitleTextView;
    TextView mDescriptionTextView;
    TextView mOverlayTextView;
    ImageView mImageView;
    ItemClickListener mItemClickListener;
    private InterstitialAd mInterstitialAd;



    public ViewHolder(View view, ItemClickListener itemClickListener) {
        super(view);
        mTitleTextView = (TextView) view.findViewById(android.R.id.text1);
        mDescriptionTextView = (TextView) view.findViewById(android.R.id.text2);
        mOverlayTextView = (TextView) view.findViewById(R.id.overlaytext);
        mImageView = (ImageView) view.findViewById(android.R.id.icon);
        mItemClickListener = itemClickListener;
        view.setOnClickListener(this);

    MobileAds.initialize(view.getContext(), "ca-app-pub-3940256099942544~3347511713");
    mInterstitialAd = new InterstitialAd(view.getContext());
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

    AdRequest request = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();

    mInterstitialAd.loadAd(request);

    mInterstitialAd.setAdListener(new AdListener() {

        public void onAdClosed() {
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });

    }

    @Override
    public void onClick(View v)
    {
        mItemClickListener.onItemClick(v, getAdapterPosition());
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }
}

interface ItemClickListener {
    void onItemClick(View view, int position);
}
}

答案 2 :(得分:-1)

您可以显示progressdialog,直到广告插播广告没有显示。