android - 使用应用内计费隐藏广告横幅
问题描述
用户使用应用内计费进行购买后,如何删除横幅广告和文本视图。用户付款后,横幅和文本确实消失了,但是一旦他们终止应用程序并再次打开,横幅和文本视图仍然显示,但不同的是当他们点击文本视图时,吐司“感谢您的购买"显示,然后文本和横幅消失。我怎样才能让它们在 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" />
解决方案
尝试对此问题使用共享偏好
以下步骤将帮助您
步骤 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);
}
}
推荐阅读
- javascript - jQuery Switch Buttons,如果单击并且如果单击 == 1,您可以添加吗?
- python - 无法在 python 中找到文本的 xpath 元素
- javascript - React Native:如何将数据从屏幕传递到另一个?
- c++ - 无法在 Visual Studio 中打开文件
- java - 如果我们在 dockerfile 中使用 CMD,Docker 多阶段构建会失败
- spacy - spaCy 仅使用某些组件
- microsoft-teams - 如何让 OnPost 在 Microsoft Teams 中触发
- c++ - 关于标准容器内边界检查的问题
- tensorflow - 资源调用 close -flutter/tflite 错误失败
- android - Android更改搜索栏颜色