用户点击后停用广告如何在android studio中编码?
答案 0 :(得分:0)
只需在onAdClick()方法中使用count变量并将条件放入其中就像,
`onAdclick(){
count++;
if(count>5)
disableAd();
}`
答案 1 :(得分:0)
这些代码会帮助你。
build.gradle
implementation 'com.google.android.gms:play-services-ads:20.2.0'
主要活动代码
public class MainActivity extends AppCompatActivity {
AdView mAdView;
int click = 0;
SharedPreferences sharedPreferences;
boolean timeUp = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BannerAdsLoad(); // Here I'm calling a method to load ads.
sharedPreferences = getSharedPreferences("Ads", MODE_PRIVATE);
}
private void BannerAdsLoad() {
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
@Override
public void onAdFailedToLoad(LoadAdError adError) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// here i'm sharing total clicks on ads & last click time
click++;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("Click", click);
String date = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date());
editor.putString("blockDate", date);
editor.apply();
}
@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
@Override
public void onAdClosed() {
// Code to be executed when the user is about to return
// to the app after tapping on an ad.
}
});
}
@Override
protected void onResume() {
super.onResume();
if (!timeUp)
blockUserDialog(); // Block User Dialog, You can customize it
}
@Override
protected void onStart() {
super.onStart();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String currDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date());
SharedPreferences sfr = getSharedPreferences("Ads", MODE_PRIVATE);
String blockDate = sfr.getString("blockDate", currDate);
try {
Date date1 = simpleDateFormat.parse(currDate);
Date date2 = simpleDateFormat.parse(blockDate);
long diff = (date1.getTime() - date2.getTime()) / (1000 * 60) % 60;
if (!(diff == 2 || diff > 2)) {
blockUserDialog();
}
timeUp = true;
} catch (ParseException e) {
e.printStackTrace();
}
}
private void blockUserDialog() {
SharedPreferences sharedPreferences = getSharedPreferences("Ads", MODE_PRIVATE);
int clicks = sharedPreferences.getInt("Click", 0);
if (clicks == 3) {
new AlertDialog.Builder(this).
setTitle("You are blocked due to more clicks on ad for 2 Minutes")
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setCancelable(false).show();
}
}
}