gpt4 book ai didi

android - 使用应用内计费隐藏广告横幅

转载 作者:行者123 更新时间:2023-11-29 18:48:17 24 4
gpt4 key购买 nike

在用户使用应用内结算进行购买后,如何删除横幅广告和 TextView 。用户付款后,横幅和文本确实消失了,但一旦他们终止应用程序并再次打开,横幅和 TextView 仍然显示,但不同的是当他们点击 TextView 时, toast “谢谢for your purchase” 显示,然后文本和横幅消失。我怎样才能让它们在 oncreate 时完全不出现。谢谢

enter image description here

主要

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" />

最佳答案

尝试使用共享偏好来解决这个问题

下面的步骤对你有帮助

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

关于android - 使用应用内计费隐藏广告横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51360192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com