google广告封装类

最近在接入google广告模块,封装了一个广告的Fragment供使用。

踢掉了不必要的XML配置文件,纯代码实现相关功能。

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

/**
 * 
 * used for load google advertise.
 * 

 * 
 * @author Administrator
 * 
 */
public class GoogleAdmobFragment extends Fragment {
	private AdView mAdView;
	
	private static final String ISOFFICIAL_FLAG = "is_official_flag";
	private boolean isOfficialVersion = false;

	public static final GoogleAdmobFragment newInstance(boolean bOfficial){
		GoogleAdmobFragment fragment = new GoogleAdmobFragment();
	    Bundle bundle = new Bundle(2);
	    bundle.putBoolean(ISOFFICIAL_FLAG, bOfficial);
	    fragment.setArguments(bundle);
	    return fragment ;
	}

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Bundle args = getArguments();
		if (args != null) {
			isOfficialVersion = args.getBoolean(ISOFFICIAL_FLAG);
		}
		// Gets the ad view defined in layout/ad_fragment.xml with ad unit ID
		// set in  values/strings.xml.
		//mAdView = (AdView) getView().findViewById(R.id.adView);

		// Create an ad request. Check logcat output for the hashed device ID to
		// get test ads on a physical device. e.g.
		// "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
		AdRequest adRequest = null;
		if(!isOfficialVersion){
			adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
		}else{
			//official release.
			adRequest = new AdRequest.Builder().build();
		}
		
		mAdView.setAdListener(new AdListener() {
			@Override
			public void onAdFailedToLoad(int errorCode) {
				try {
					ViewParent pv = mAdView.getParent();
					if (pv != null) {
						pv = pv.getParent();
						if (pv != null && pv instanceof View) {
							((View) pv).setVisibility(View.GONE);
						}
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		// Start loading the ad in the background.
		mAdView.loadAd(adRequest);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		LinearLayout adContainer = new LinearLayout(getActivity());
		adContainer.setBackgroundColor(Color.BLACK);
		adContainer.setGravity(Gravity.CENTER_HORIZONTAL);
		mAdView = new AdView(getActivity());
		mAdView.setAdSize(AdSize.BANNER);
		String adUnitId = getResources().getString(R.string.banner_ad_unit_id);
		mAdView.setAdUnitId(adUnitId);

		// Place the ad view.
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		adContainer.addView(mAdView, params);
		return adContainer;
	}

相关推荐