2011年1月17日月曜日

【Android】AdMobが表示されない場合にAdMakerを表示するクラス

前に使ったクラスを晒します。

package jp.dd0125.common;

import jp.co.nobot.libYieldMaker.libYieldMaker;
import android.app.Activity;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.admob.android.ads.AdListener;
import com.admob.android.ads.AdView;

public class CommonAdMob_AdMaker {
 private static final String TAG = "CommonAdvertisement";

 ProgressBar pb;
 AdView adView;
 libYieldMaker adMaker;
 Activity act;
 public CommonAdMob_AdMaker(Activity act){
  this.act = act;

  pb = new ProgressBar(act);
  pb.setVisibility(ProgressBar.VISIBLE);


  //pb.setGravity(android.view.Gravity.RIGHT);
  pb.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

 }




 public void createAdMob(int align){
        adView = new AdView(act);
        adView.setVisibility(android.view.View.VISIBLE);
        adView.requestFreshAd();

        adView.setGravity(align);
        adView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


        adView.setAdListener(new AdListener() {

            public void onReceiveRefreshedAd(AdView adView) {
                Log.d(TAG, "AdListener.onReceiveRefreshedAd called!");
            }

            public void onReceiveAd(AdView adView) {
                Log.d(TAG, "AdListener.onReceiveAd called!");
                // 広告が表示できたのでProgressBarを消す
                pb.setVisibility(ProgressBar.GONE);
            }

            public void onFailedToReceiveRefreshedAd(AdView adView) {
                Log.d(TAG, "AdListener.onFailedToReceiveRefreshedAd called!");
            }

            public void onFailedToReceiveAd(AdView adView) {
                Log.d(TAG, "AdListener.onFailedToReceiveAd called!");
                // プログレスバーを消す
                pb.setVisibility(ProgressBar.GONE);


                // AdMaker の広告を設定している場合、AdMakerの広告を表示するのでAdViewの更新を行わせない
                if(adMaker != null){
                    adView.setRequestInterval(0);
                    adView.setVisibility(AdView.GONE);

                    // AdMakerの広告を表示させる
                    adMaker.setVisibility(libYieldMaker.VISIBLE);
                    adMaker.startView();

                }
            }
        });
 }

 public void createAdMaker(String url){
  adMaker = new libYieldMaker(act);
        adMaker.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        adMaker.setActivity(act);
        adMaker.setUrl(url);

 }
 public RelativeLayout getAdMob_AdMaker(){
  RelativeLayout layout = new RelativeLayout(act);
  layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

  layout.setGravity(android.view.Gravity.CENTER);
  layout.addView(pb);
  if(adView != null){
   layout.addView(adView);
  }
  if(adMaker != null){
   layout.addView(adMaker);
   if(adView == null) {
    pb.setVisibility(ProgressBar.GONE);
    adMaker.setVisibility(libYieldMaker.VISIBLE);
    adMaker.startView();
   }
  }
  return layout;
 }
}

このように使います。

// 広告
 //AdManager.setTestDevices( new String[] {
 //  AdManager.TEST_EMULATOR,
 //  //"XXXXXXXXXXXXXXXXXXXX",
 //  });
 LinearLayout l_ad = (LinearLayout)findViewById(レイアウトID);
 CommonAdMob_AdMaker Ad;
 Ad = new CommonAdMob_AdMaker(this);
 Ad.createAdMob(RelativeLayout.ALIGN_TOP); // AdMobを生成
 Ad.createAdMaker("http://stg-images.ad-maker.info/sample-test.html"); // AdMakerを生成

 l_ad.addView(Ad.getAdMob_AdMaker()); // 広告を持ったレイアウトが返ってきますので addViewします

ただし、
・res/values/attr.xml
・AdMob と AdMaker の jarライブラリ組み込み
は必要です。

0 件のコメント:

コメントを投稿