如何限制每个会话的广告数量?

时间:2015-01-27 09:39:17

标签: java android admob

我有一个应用,在我的应用的几个页面上执行了一定数量的操作后显示插页式广告。我担心用户可能会在我的应用程序中闲逛,并且会在他们的脸上沾上一大堆插页式广告。

我如何限制每个会话的广告数量?比如,每次会话限制为2或3个插页式广告。

谷歌搜索这个问题让我得到了这个唯一相关的结果: https://groups.google.com/forum/#!topic/google-admob-ads-sdk/QZV3Z5Vy4pg

"至于如何做到这一点,例如,您可以将计数器存储在共享偏好设置中,并在每次显示插页式广告时将其递增。如果您达到5,则可以暂时停止请求插页式广告。当应用程序再次启动时,可以清除计数器。"

我是java代码/开发的新手,并且已经对SharedPreferences做了很多研究,但我对如何将它合并到我的代码中感到茫然。

如果有人可以帮助我走上正确的道路或者有更好的想法,我全都听见了!

谢谢!

编辑:我的代码:



import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.xxxx.xxxxx.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class menuclass extends Activity{

	private InterstitialAd interstitial;
  
  // setting up counter for the interstitial ad
	private int counter = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
      
              //Layout
		setContentView(R.layout.menulayout);
		
		
		//Prepare the Interstitial Ad
		interstitial = new InterstitialAd(menuclass.this);

		//Insert the Ad Unit ID
		interstitial.setAdUnitId("xxxxxx/xxxxxx");

		//Locate the Banner Ad in xml
		AdView adView = (AdView) this.findViewById(R.id.adView);

		//Request for Ads
		AdRequest adRequest = new AdRequest.Builder()

		//Add a test device to show Test Ads
		.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
		.addTestDevice("xxxxxxxxxxxxx")
		.build ();

		//Load ads into Banner Ads
		adView.loadAd(adRequest);

		//Load ads into Interstitial Ads
		loadInterstitial();
	
		//setting up buttons
				Button cp1 = (Button) findViewById(R.id.menu1);
				Button cp2 = (Button) findViewById(R.id.menu2);
				Button cp3 = (Button) findViewById(R.id.menu3);
				Button cp4 = (Button) findViewById(R.id.menu4);
				Button cp5 = (Button) findViewById(R.id.menu5);
				
				
		// On Click Listeners, all buttons are involved in adding to the counter
				
				cp1.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(menuclass.this, menu1class.class));
						
						counter++;
						displayInterstitial();
						
					}
				});
				
				cp2.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(menuclass.this, menu2class.class));
						
						counter++;
						displayInterstitial();
					}
				});
				cp3.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(menuclass.this, menu3class.class));
						
						counter++;
						displayInterstitial();
					}
				});
				cp4.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(Cmenuclass.this, menu4class.class));
						
						counter++;
						displayInterstitial();
					}
				});
				cp5.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(menuclass.this, menu5class.class));
						
						counter++;
						displayInterstitial();
					}
				});
		
	}


// displaying the interstitial
private void displayInterstitial()
{

//sharedpreferences
//Here, SharedPreference is changed to SharedPreferences
  SharedPreferences sp = getSharedPreferences("preference name", MODE_PRIVATE);
  //I will try 1 ad per session
  if(sp.getInt("key", 0) < 1){
     Editor ed = sp.edit();
     ed.putInt("key",(sp.getInt("key", 0) +1));
//Here, ed.commint(): is changed to ed.commit();
     ed.commit();
     //do adds work
   }
//end shared preferences

// If Ads are loaded and counter >= 5 show Interstitial, else show nothing.
if (interstitial.isLoaded() & counter>=5)
{
//Resets the counter
// counter = 0; (don't want counter at this point)
interstitial.show();
loadInterstitial();
}
}

private void loadInterstitial()
{
AdRequest adRequest = new AdRequest.Builder().
addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("xxxxxxxxxxxx")
.build(); 
interstitial.loadAd(adRequest);
}

}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

首先在您的oncreate中调用此 loadInterstitial(); 以初始加载您的添加

并且我的示例逻辑还有这样一个事实:如果您的按钮被点击了至少5次或小于6 ,您可以在if语句中更改它。

我摆脱了第一种方法..

使用共享偏好方法

  SharedPreferences sp = getSharedPreferences("preference name", MODE_PRIVATE);
  if(sp.getInt("key", 0) < any number here){
     Editor ed = sp.edit();
     ed.putInt("key",(sp.getInt("key", 0) +1));
     ed.commit();
     //do adds work
   }

当你需要展示时调用它们,可能会放入一个方法

编辑2 带来您的代码

先生,您的代码非常好,只需按照这种方式进行修改

private void displayInterstitial(){

//sharedpreferences
//Here, SharedPreference is changed to SharedPreferences - thanks for changing it
SharedPreferences sp = getSharedPreferences("preference name", MODE_PRIVATE);
//I will try 1 ad per session - when you call show(), it shows one add, okay? so
if(sp.getInt("key", 0) < 5){ // the zero is the default value if it's empty, the if statement checks if your sp integer is less than 5.
    // and continues if it checks true, if not, skip the whole statement, that's the logic here
   Editor ed = sp.edit(); 
   ed.putInt("key",sp.getInt("key", 0) +1);
   //Here, ed.commint(): is changed to ed.commit(); -thanks once more- i looked stupid at first
   ed.commit();
   //do adds work - you should put your add code in this method below "do adds work"
   if (interstitial.isLoaded() && counter < 5){ // that's your counter, the && checks if your first argument is true before it moves on to the 
  //second argument.. if it checks false, all is false & checks 
  // checks both arguments regardless of their outcome
        interstitial.show(); 
        loadInterstitial(); 
   } 
 }
  //end shared preferences, end of if-statement, end of logic code
} // end of method 

if语句是检查器,如果if语句检查为false则不会显示add,只要你的int小于5,它就会一直检查为

A lso先生,创建getter和setter很容易,getter和setter只是帮助设置和检索变量/对象的一些功能,你可以通过声明你的变量/对象来创建一个,在那之后,在你的日食上去来源然后生成getter和setter ..它们将由eclipse生成 - (如果你使用eclipse,那就像其他ides一样)这很容易,还要注意你最初的方式非常好,只需要稍加改动

代码中的日志行以了解正在发生的事情

并且应该工作。 SIR TOM