AndroidでAdMobとAdMakerの切り替え
AdMakerのSDKもバージョンが上がって、広告の成否が判断できるようになってました。
優先度を設定して表示するサンプル。
・AdMobのSDKは4.1.1
・AdMakerのSDKは1.1
広告クラス
public class ComAd { private static final int STATE_OK = 1; // 成功 private static final int STATE_EXEC = 0; // 実行中 private static final int STATE_NG = -1; // 失敗 private Activity activuty; private RelativeLayout adLayout; private AdView adMob; private libAdMaker adMaker; private boolean priorityAdMobFlg; // AdMob優先フラグ private int stateAdMob; // AdMob状態 private int stateAdMaker; // AdMaker状態 /** * * @param activuty */ public ComAd(Activity activity, RelativeLayout adLayout) { this.activuty = activity; this.adLayout = adLayout; this.adLayout.setVisibility(LinearLayout.INVISIBLE); if (Locale.getDefault().equals(Locale.JAPAN) || Locale.getDefault().equals(Locale.JAPANESE)) { // 日本のみAdMaker優先とか priorityAdMobFlg = false; } else { priorityAdMobFlg = true; } } // 広告取得 public void loadAd() { // 実行中に設定 stateAdMob = STATE_EXEC; stateAdMaker = STATE_EXEC; // 実行 loadAdMob(); loadAdMaker(); } // AdMob取得 private void loadAdMob() { adMob = new AdView(activuty, AdSize.BANNER, "xxxxxxx"); adLayout.addView(adMob); adMob.loadAd(new AdRequest()); adMob.setAdListener(new AdListener() { public void onReceiveAd(Ad arg0) { // 成功 stateAdMob = STATE_OK; setAd(); } public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) { // 失敗 stateAdMob = STATE_NG; setAd(); } public void onPresentScreen(Ad arg0) {} public void onLeaveApplication(Ad arg0) {} public void onDismissScreen(Ad arg0) {} }); } // AdMaker取得 private void loadAdMaker() { LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); adMaker = new libAdMaker(activuty); adMaker.setLayoutParams(lp); adMaker.setBackgroundColor(Color.WHITE); adLayout.addView(adMaker); adMaker.siteId = "XXX"; // siteId adMaker.zoneId = "XXXX"; // zoneId adMaker.setUrl("http://images.ad-maker.info/apps/xxxx.html"); // URL adMaker.start(); adMaker.setAdMakerListener(new AdMakerListener() { public void onReceiveAdMaker() { // 成功 stateAdMaker = STATE_OK; setAd(); } public void onFailedToReceiveAdMaker(String arg0) { // 失敗 stateAdMaker = STATE_NG; setAd(); } }); } // Ad private void setAd() { // メモのためif分は分かりやすく記述 // ここのif分を変更すれば、先に表示された広告を優先で表示等にできる if (stateAdMob == STATE_OK && priorityAdMobFlg) { //AdMobかつAdMob優先ならAdMob表示(AdMaker処理は待たない) setAdMob(); return; } if (stateAdMaker == STATE_OK && !priorityAdMobFlg) { //AdMakerかつAdMob優先ではないならAdMaker表示(AdMob処理は待たない) setAdMaker(); return; } if (stateAdMob == STATE_OK && stateAdMaker == STATE_NG) { //adMobのみ成功 setAdMob(); return; } if (stateAdMob == STATE_NG && stateAdMaker == STATE_OK) { //adMakerのみ成功 setAdMaker(); return; } if (stateAdMob == STATE_NG && stateAdMaker == STATE_NG) { // 両方失敗ならやりなおし loadAd(); } } // AdMob private void setAdMob() { // AdMob表示 adLayout.setVisibility(LinearLayout.VISIBLE); adMob.setVisibility(AdView.VISIBLE); // AdMaker非表示 adMaker.setVisibility(AdView.GONE); adMaker.stop(); } // AdMaker private void setAdMaker() { // AdMaker表示 adLayout.setVisibility(LinearLayout.VISIBLE); adMaker.setVisibility(AdView.VISIBLE); // AdMob非表示 adMob.setVisibility(AdView.GONE); adMob.stopLoading(); } }
呼び出し元ActivityのOnCreateあたりで
//広告表示 new ComAd(this, (RelativeLayout)findViewById(R.id.ad_layout)).loadAd();
呼び出し元Activityのレイアウトの上部か下部に
<RelativeLayout android:id="@+id/ad_layout" android:layout_width="fill_parent" android:layout_height="50dp"></RelativeLayout>
AndroidManifestに以下を忘れずに
<activity android:name="com.google.ads.AdActivity" android:configChanges="orientation|keyboard|keyboardHidden"></activity> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />