首页 > 解决方案 > 使用应用内计费隐藏广告横幅

问题描述

用户使用应用内计费进行购买后,如何删除横幅广告和文本视图。用户付款后,横幅和文本确实消失了,但是一旦他们终止应用程序并再次打开,横幅和文本视图仍然显示,但不同的是当他们点击文本视图时,吐司“感谢您的购买"显示,然后文本和横幅消失。我怎样才能让它们在 oncreate 时根本不出现。谢谢

在此处输入图像描述

主要的

public class MainActivity extends AppCompatActivity implements BillingProcessor.IBillingHandler{
BillingProcessor bp;
AdView Adview;
TextView textView6;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bp = new BillingProcessor(this, "YOUR LICENSE KEY FROM GOOGLE PLAY CONSOLE HERE", this);
    MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713");
    Adview=findViewById(R.id.adView);
    textView6=findViewById(R.id.textView6);
    AdRequest adRequest = new AdRequest.Builder()/*.addTestDevice("")*/.build();
    Adview.loadAd(adRequest);

}
public void onClickAds(View view){
    bp.purchase(MainActivity.this, "android.test.purchased");
}

@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {

    Toast.makeText(this, "Thank you for your purchase",Toast.LENGTH_LONG).show();
    ViewGroup parent1 = (ViewGroup) Adview.getParent();
    ViewGroup parent2 = (ViewGroup) textView6.getParent();
    parent1.removeView(Adview);
    parent2.removeView(textView6);
    parent1.invalidate();
    parent2.invalidate();
    }

@Override
public void onPurchaseHistoryRestored() {

}

@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
    Toast.makeText(this, "AN ERROR OCCURED.",Toast.LENGTH_LONG).show();
}

@Override
public void onBillingInitialized() {

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (!bp.handleActivityResult(requestCode, resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
@Override
public void onDestroy() {
    if (bp != null) {
        bp.release();
    }
    super.onDestroy();
}

}

XML

   <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"

    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
    ads:layout_constraintBottom_toBottomOf="parent"
    ads:layout_constraintEnd_toEndOf="parent"
    ads:layout_constraintStart_toStartOf="parent"
    ads:layout_constraintTop_toBottomOf="@+id/constraintLayout"
    ads:layout_constraintVertical_bias="0.42"></com.google.android.gms.ads.AdView>

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onClickAds"
    android:text="Don't like seeing ads? Tap Here!"
    android:textColor="@android:color/background_light"
    android:textStyle="bold|italic"
    app:layout_constraintBottom_toTopOf="@+id/adView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

标签: androidin-app-billingadsbanner

解决方案


尝试对此问题使用共享偏好

以下步骤将帮助您

步骤 1. 在顶部初始化

private static final String PREF_FILE  = "PREF_FILE";
private static final String IS_PRODUCT_PURCHASE = "IS_PRODUCT_PURCHASE";

步骤 2. 在 onProductPurchased() 方法中添加这些行

SharedPreferences.Editor editor = getSharedPreferences(PREF_FILE, MODE_PRIVATE).edit();
    editor.putBoolean(IS_PRODUCT_PURCHASE,true);
    editor.commit();

setp 3. 最后在 onCreate() 中调用这个方法

private void checkForIsPurchase() {
    SharedPreferences preferences = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
    boolean isPurchase = preferences.getBoolean(IS_PRODUCT_PURCHASE, false);
    if (isPurchase) {
        Adview.setVisibility(View.GONE);
        textView6.setVisibility(View.GONE);
    }
}

推荐阅读