用户点击 say 超过五次后,如何在我的应用中禁用广告?
How can I disable ads on my app after a user has clicked say more than five times?
用户点击后禁用广告 如何在 android studio 中编写代码?
简单地说,您可以在(比如)onAdClick() 方法中使用计数变量并在其中添加条件,
`onAdclick(){
count++;
if(count>5)
disableAd();
}`
这些代码会对你有所帮助。
build.gradle
implementation 'com.google.android.gms:play-services-ads:20.2.0'
MainActivity 代码
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();
}
}
}
用户点击后禁用广告 如何在 android studio 中编写代码?
简单地说,您可以在(比如)onAdClick() 方法中使用计数变量并在其中添加条件,
`onAdclick(){
count++;
if(count>5)
disableAd();
}`
这些代码会对你有所帮助。
build.gradle
implementation 'com.google.android.gms:play-services-ads:20.2.0'
MainActivity 代码
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();
}
}
}