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

标签 android in-app-billing ads banner

在用户使用应用内结算进行购买后,如何删除横幅广告和 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/

相关文章:

java - Activity 之间的共享首选项

android - 应用内结算 : Which public and private keys are used for the in-app billing verification?

javascript - 动态 DFP 广告管理系统 JavaScript

javascript - 这是一种新的广告 JavaScript 类型,更改为 "window.opener.location",我该如何阻止它?

ios - Facebook 移动应用程序安装广告错误 - 找不到您请求的页面

android - 如何阻止 Android 的 ADB 自动连接到我局域网中的设备?

Android:闹钟每 30 分钟播放一次,从 12:30 开始

android - 自动续订订阅应用内结算谷歌播放

java - viewmodelfactory 和使用 Activity 模块的 View 模型注入(inject)之间的区别

google-play - 应用内结算 - 当 queryPurchases() 返回空列表时,Google Play 给我 "You already own this item"消息