如何在5次点击后显示插页式广告? (共享偏好)

时间:2017-03-19 08:00:53

标签: android admob interstitial

我试图在点击5次按钮后显示插页式广告,下面是我的代码无法正常工作,请有人告诉我如何解决这个问题。我尝试了两种方法,两种方法都不起作用。

PS:我是Android开发的新手。




第一个代码



int counter = 0;

...



		FragmentManager fm = getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();

		Button play_video = (Button) rootView.findViewById(R.id.play_video);
		play_video.setOnClickListener(new View.OnClickListener() {
    
			@Override
      
			public void onClick(View v) {
				if (counter == 5) {
					if (mInterstitialAd.isLoaded()) {
						mInterstitialAd.show();
						counter = 0;
					} else {
						counter++;
						stream(vidData);
						getDialog().dismiss();

					}

					if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
						AdRequest adRequest = new AdRequest.Builder().build();
						mInterstitialAd.loadAd(adRequest);
					}
				}
			}
		});






第二代码(共享优先权)



private int counter = 0;

.....

FragmentManager fm = getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();

		Button play_video = (Button) rootView.findViewById(R.id.play_video);
		play_video.setOnClickListener(new View.OnClickListener() {

			Context context = getActivity();
			SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = preferences.edit();

			int counter = preferences.getInt("CounterR", 0);

			@Override

			public void onClick(View v) {
				if (mInterstitialAd.isLoaded() & counter >= 5) {

					counter = 0;
					mInterstitialAd.show();

				} else {

					counter++;

					stream(vidData);
					getDialog().dismiss();

				}
				editor.putInt("CounteR", counter);
				editor.commit();


				if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
					AdRequest adRequest = new AdRequest.Builder().build();
					mInterstitialAd.loadAd(adRequest);
				}
			}
		});






更新完整代码



private int counter = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.vid_alert, container, false);

		close_dialog = (Button) findViewById(R.id.close_dialog);

		mInterstitialAd = new InterstitialAd(getActivity());

		mInterstitialAd.setAdUnitId("xxxxxxxxxxxxxxxxxx");

		AdRequest adRequest = new AdRequest.Builder().build();

		// Load ads into Interstitial Ads
		mInterstitialAd.loadAd(adRequest);

		// Create and set AdListener for interstitial
		mInterstitialAd.setAdListener(new AdListener() {

			public void onAdClosed() {
				// When user closes ad end this activity (go back to first
				// activity)
				finish();
			}

			private void finish() {
				// TODO Auto-generated method stub

			}
		});

		getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		Bundle mArgs = getArguments();
		final String vidData = mArgs.getString("vid_data");
		final String vidID = mArgs.getString("vid_id");

		FragmentManager fm = getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();

		Context context = getActivity();
		SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();

		Button stream_video = (Button) rootView.findViewById(R.id.stream_video);
		stream_video.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (counter == 5) {
					if (mInterstitialAd.isLoaded()) {
						mInterstitialAd.show();
						AdRequest adRequest = new AdRequest.Builder().build();
						mInterstitialAd.loadAd(adRequest);
						counter = 0;
					} else {
						// means interstitial not yet loaded,so make a load
						// request
						AdRequest newadRequest = new AdRequest.Builder().build();
						mInterstitialAd.loadAd(newadRequest);
						// as nothing will happen when we get inside this else
						// statement, you should call the stream method from
						// here too

						streamFB(vidData);
						getDialog().dismiss();

					}
				} else {
					counter++;
					streamFB(vidData);
					getDialog().dismiss();
				}
			}
		});
		Button close_dialog = (Button) rootView.findViewById(R.id.close_dialog);
		close_dialog.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (counter == 5) {
					if (mInterstitialAd.isLoaded()) {
						mInterstitialAd.show();
						AdRequest adRequest = new AdRequest.Builder().build();
						mInterstitialAd.loadAd(adRequest);
						counter = 0;
					} else {
						// means interstitial not yet loaded,so make a load
						// request
						AdRequest newadRequest = new AdRequest.Builder().build();
						mInterstitialAd.loadAd(newadRequest);
						// as nothing will happen when we get inside this else
						// statement, you should call the stream method from
						// here too
						getDialog().dismiss();
					}
				} else {
					counter++;
					getDialog().dismiss();
				}
			}
		});
		// Do something else
		return rootView;




3 个答案:

答案 0 :(得分:1)

问题在于你的逻辑,其他部分应该带有外部if,检查下面的代码:

if (counter == 5) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                AdRequest adRequest = new AdRequest.Builder().build();
                mInterstitialAd.loadAd(adRequest);
                counter = 0;
            }else{
                // means interstitial not yet loaded,so make a load request
                AdRequest newadRequest = new AdRequest.Builder().build();
                mInterstitialAd.loadAd(newadRequest);
                // as nothing will happen when we get inside this else statement, you should call the stream method from here too
                stream(vidData);
                getDialog().dismiss();
            }
        }else {
            counter++;
            stream(vidData);
            getDialog().dismiss();
        }

在onclick方法中移动此代码。让我知道它是否有效,直接在stackoverflow上编辑,请检查语法错误,可能有一两个。

答案 1 :(得分:0)

尝试使用此功能,如果它不起作用,则可能是您的广告代码存在问题。

int counter = 0;

        ...



                FragmentManager fm = getFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();

                Button play_video = (Button) rootView.findViewById(R.id.play_video);
                play_video.setOnClickListener(new View.OnClickListener() {

                    @Override

                    public void onClick(View v) {
                        if (counter != 5) {
                            counter++;
                            stream(vidData);
                            getDialog().dismiss();
                           }
                        else if (counter ==5){
                            if (mInterstitialAd.isLoaded()) {
                                mInterstitialAd.show();
                                counter = 0;
                            } 
                            }

                            if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
                                AdRequest adRequest = new AdRequest.Builder().build();
                                mInterstitialAd.loadAd(adRequest);
                            }
                        }
                    }
                });
    }

答案 2 :(得分:0)

在mInterstitialAd.show()之后删除counter = 0;从if条件并将其放在onAdClosed()函数中,请参见下面的示例: mInterstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { requestNewInterstitial(); counter=0; startNewGame(); } });