【问题标题】:Hide ad banner with in app billing使用应用内计费隐藏广告横幅
【发布时间】:2018-12-23 21:43:07
【问题描述】:

在用户使用应用内结算进行购买后,我如何删除横幅广告和文本视图。用户付款后,横幅和文本确实消失了,但是一旦他们终止应用程序并再次打开,横幅和文本视图仍会显示,但不同之处在于当他们点击文本视图时,吐司 “谢谢供您购买” 显示,然后文本和横幅消失。我怎样才能让它们在 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" />

【问题讨论】:

    标签: android in-app-billing ads banner


    【解决方案1】:

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

    以下步骤将帮助您

    步骤 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);
        }
    }
    

    【讨论】:

    • 问题总是来得及。
    猜你喜欢
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 2012-05-30
    相关资源
    最近更新 更多