这是我第一次接触广告和Android IAP。这是我的代码,我想检查用户是否购买了我的应用的专业版,以删除recyclerview中的所有广告。目前,我只在recyclerview中的项目之间显示横幅广告
代码如下:
public class NewReleasesAdapter extends RecyclerView.Adapter< RecyclerView.ViewHolder> implements BillingProcessor.IBillingHandler {
public static final String TAG = NewReleasesAdapter.class.getSimpleName();
private LayoutInflater mInflater;
private Context mContext;
private List<Release> mNewReleases;
private DatabaseHelper mDatabaseHelper;
// A game/release item view type.
private static final int GAME_ITEM_VIEW_TYPE = 0;
// The banner ad view type.
private static final int BANNER_AD_VIEW_TYPE = 1;
// Billing
private BillingProcessor mBillingProcessor;
private boolean mIsProVersion;
// Ads
private AdRequest mAdRequest;
public NewReleasesAdapter(Context context) {
mContext = context;
mInflater = LayoutInflater.from(mContext);
mDatabaseHelper = DatabaseHelper.getDatabaseHelper(mContext);
// Is in europe
// if(!mIsProVersion) {
if (ConsentInformation.getInstance(mContext).isRequestLocationInEeaOrUnknown()) {
switch (ConsentInformation.getInstance(mContext).getConsentStatus()) {
case PERSONALIZED:
// Personalized ads
mAdRequest = new AdRequest.Builder().build();
break;
case NON_PERSONALIZED:
// fall though to none Personalized ads
default:
// None Personalized ads
Bundle extras = new Bundle();
// npa is short for "none personalized ads"
extras.putString("npa", "1");
mAdRequest = new AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
.build();
break;
}
} else {
// Personalized ads
mAdRequest = new AdRequest.Builder().build();
}
// }
}
public void setNewReleases(List<Release> newReleases) {
this.mNewReleases = newReleases;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch(viewType) {
case GAME_ITEM_VIEW_TYPE:
return new NewGameHolder(mInflater.inflate(R.layout.newly_added_game, parent, false));
case BANNER_AD_VIEW_TYPE:
return new ViewHolderAdMob(mInflater.inflate(R.layout.list_item_ad, parent, false), mAdRequest);
default:
return new NewGameHolder(mInflater.inflate(R.layout.newly_added_game, parent, false));
}
}
@Override
public int getItemViewType(int position) {
// isn't pro version
if ((position + 1) % 6 == 0 && !mIsProVersion) {
return BANNER_AD_VIEW_TYPE;
}
return GAME_ITEM_VIEW_TYPE;
}
@Override
public int getItemCount() {
if (mNewReleases == null) {
return 0;
}
return mNewReleases.size();
}
@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
}
@Override
public void onPurchaseHistoryRestored() {
}
@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
}
@Override
public void onBillingInitialized() {
mBillingProcessor.loadOwnedPurchasesFromGoogle();
if (mBillingProcessor.isPurchased("pro")) {
mIsProVersion = true;
// Toast.makeText(mContext, "You're a pro user! Thanks!", Toast.LENGTH_SHORT).show();
} else {
mIsProVersion = false;
}
}
public BillingProcessor getBillingProcessor() {
return mBillingProcessor;
}
class NewGameHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView summary;
public TextView releaseDate;
public TextView platforms;
public TextView genres;
public ImageView cover;
public Button favorite;
public ProgressBar coverProgressBar;
public RelativeLayout newReleaseLayout;
public NewGameHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.game_name);
summary = itemView.findViewById(R.id.summary);
releaseDate = itemView.findViewById(R.id.release_date);
platforms = itemView.findViewById(R.id.platforms);
genres = itemView.findViewById(R.id.genres);
cover = itemView.findViewById(R.id.cover_image);
favorite = itemView.findViewById(R.id.favorite);
coverProgressBar = itemView.findViewById(R.id.cover_progress_bar);
newReleaseLayout = itemView.findViewById(R.id.new_release);
}
}
public static class ViewHolderAdMob extends RecyclerView.ViewHolder {
public AdView mAdView;
public ViewHolderAdMob(View view, AdRequest adRequest) {
super(view);
mAdView = view.findViewById(R.id.adView);
if (adRequest != null) {
mAdView.loadAd(adRequest);
}
}
}
}
现在的问题是我拥有专业版,并且该应用仍在展示广告,这意味着在我致电mIsProVersion
时getItemViewType
为假,这确定了是否要显示下一个项目我的recyclerview是普通商品或广告商品。我猜想我对计费库的实现不好。如果用户使用专业版,则隐藏广告的正确方法是什么?谢谢