廣點通sdk接入 _原生廣告

廣點通sdk接入 _原生廣告

1:導入相關架包,寫入相關權限和配置
android-query-full.0.26.7.jar
GDTUnionSDK.4.8.513.jar
Volley.jar//本案例通過此框架設置圖片

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <service
android:name="com.qq.e.comm.DownloadService"
android:exported="false" />

    <activity
 android:name="com.qq.e.ads.ADActivity"            android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />

2:include自定義廣告佈局nativelistitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants" >

    <!-- 廣告Logo -->
    <ImageView
        android:id="@+id/img_logo"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp" />

    <!-- 廣告標題 -->
    <TextView
        android:id="@+id/text_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/img_logo"
        android:layout_margin="5dp"
        android:layout_toRightOf="@id/img_logo"
        android:textColor="@android:color/black" />

    <!-- 內容 -->
    <TextView
        android:id="@+id/text_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_name"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="15dp"
        android:layout_toRightOf="@id/img_logo"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="#A0000000" />

    <!-- 文本狀態 -->
    <TextView
        android:id="@+id/text_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_desc"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_toRightOf="@id/img_logo"
        android:textColor="#A0000000" />

    <!-- 內容圖片 -->
    <ImageView
        android:id="@+id/img_poster"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_below="@id/text_status"
        android:layout_margin="10dp"
        android:minHeight="180dp"
        android:scaleType="fitXY" />

    <!-- 分割線 -->
    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/img_poster"
        android:layout_margin="10dp"
        android:background="#30000000" />

    <!-- 下載按鈕 -->
    <Button
        android:id="@+id/btn_download"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/divider"
        android:layout_margin="10dp"
        android:background="#009688"
        android:textColor="@android:color/white" />

</RelativeLayout>

3:主界面佈局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.qq.e.NewNativeAdActivity" >

    <!-- 自定義廣告佈局類 -->
    <include
        android:id="@+id/nativeADContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        layout="@layout/nativelistitem" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/nativeADContainer"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/loadNative"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="加載廣告" />

        <Button
            android:id="@+id/showNative"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="展示廣告" />
    </LinearLayout>

</RelativeLayout>

4:詳見Activity:
/**
 * 自定義佈局的原生廣告
 * 
 * @author Administrator
 * 
 */
public class NativeADActivity extends Activity implements NativeAdListener,
        OnClickListener {
    private NativeADDataRef adItem;
    private NativeAD nativeAD;
    private Button btn_download;// 下載按鈕
    private Button loadNative, showNative;//加載  /和顯示廣告

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gdtnativead_demo);

        loadNative = (Button) findViewById(R.id.loadNative);
        showNative = (Button) findViewById(R.id.showNative);

        loadNative.setOnClickListener(this);
        showNative.setOnClickListener(this);

    }

    /**
     * 加載廣告,初始化
     */
    public void loadAD() {
        // 在廣告初始化前調用此方法,轉爲多線程原生廣告
        MultiProcessFlag.setMultiProcess(true);
        if (nativeAD == null) {
            // 初始化
            this.nativeAD = new NativeAD(this, Constants.APPID,
                    Constants.NativePosID, this);
        }
        int count = 1; // 一次拉取的廣告條數:範圍1-30
        nativeAD.loadAD(count);// 開始讀取廣告
    }

    /**
     * 展示廣告,自定義廣告佈局
     */
    public void showAD() {


        ImageView img_logo = (ImageView) findViewById(R.id.nativeADContainer)
                .findViewById(R.id.img_logo);// 廣告Logo
        ImageView img_poster = (ImageView) findViewById(R.id.nativeADContainer)
                .findViewById(R.id.img_poster);// 圖片內容
        TextView text_name = (TextView) findViewById(R.id.nativeADContainer)
                .findViewById(R.id.text_name);// 文字標題
        TextView text_desc = (TextView) findViewById(R.id.nativeADContainer)
                .findViewById(R.id.text_desc);// 文字內容
        btn_download = (Button) findViewById(R.id.nativeADContainer)
                .findViewById(R.id.btn_download);// 下載按鈕

        getImage(img_logo, adItem.getIconUrl());//通過Volley框架設置圖片
        getImage(img_poster, adItem.getImgUrl());

        text_name.setText(adItem.getTitle());//設置文字
        text_desc.setText(adItem.getDesc());

        adItem.onExposured(this.findViewById(R.id.nativeADContainer));

        //設置下載按鈕
        btn_download.setText(getADButtonText());
        btn_download.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                adItem.onClicked(v);//發送相關的狀態通知
            }
        });

    }

    /**
     * App類廣告安裝、下載狀態的更新(普鏈廣告沒有此狀態,其值爲-1) 返回的AppStatus含義如下: 0:未下載 1:已安裝 2:已安裝舊版本
     * 4:下載中(可獲取下載進度“0-100”) 8:下載完成 16:下載失敗
     */
    private String getADButtonText() {
        if (adItem == null) {
            return "……";
        }
        if (!adItem.isAPP()) {
            return "查看詳情";
        }
        switch (adItem.getAPPStatus()) {
        case 0:
            return "點擊下載";
        case 1:
            return "點擊啓動";
        case 2:
            return "點擊更新";
        case 4:
            return "下載中" + adItem.getProgress() + "%";
        case 8:
            return "點擊安裝";
        case 16:
            return "下載失敗,點擊重試";
        default:
            return "查看詳情";
        }
    }

    /**
     * 加載成功時調用
     */
    @Override
    public void onADLoaded(List<NativeADDataRef> arg0) {
        if (arg0.size() > 0) {
            adItem = arg0.get(0);
            // $.id(R.id.showNative).enabled(true);//設置爲可用
            showNative.setEnabled(true);
            Toast.makeText(this, "原生廣告加載成功", Toast.LENGTH_LONG).show();
        } else {
            Log.i("AD_DEMO", "NOADReturn");
        }
    }

    /**
     * 廣告狀態發送改變,更新下載按鈕文字
     */
    @Override
    public void onADStatusChanged(NativeADDataRef arg0) {
        btn_download.setText(getADButtonText());
    }

    /**
     * 加載失敗時調用
     */
    @Override
    public void onNoAD(int arg0) {
        Log.e("tag: ", "" + arg0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.loadNative:// 加載廣告
            loadAD();
            break;
        case R.id.showNative:// 顯示廣告
            showAD();
            break;
        }

    }

    /**
     * Volley框架獲得圖片
     * 
     * @param iv
     * @param url
     */
    public void getImage(ImageView iv, String url) {
        String imgUrl = url;
        RequestQueue mRequestQueue = Volley.newRequestQueue(this);
        final LruCache<String, Bitmap> mImageCache = new LruCache<String, Bitmap>(
                20);
        ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
            @Override
            public void putBitmap(String key, Bitmap value) {
                mImageCache.put(key, value);
            }

            @Override
            public Bitmap getBitmap(String key) {
                return mImageCache.get(key);
            }
        };
        ImageLoader mImageLoader = new ImageLoader(mRequestQueue, imageCache);
        // imageView是一個ImageView實例
        // ImageLoader.getImageListener的第二個參數是默認的圖片resource id
        // 第三個參數是請求失敗時候的資源id,可以指定爲0
        ImageLoader.ImageListener listener = ImageLoader
                .getImageListener(iv, android.R.drawable.ic_menu_rotate,
                        android.R.drawable.ic_delete);
        mImageLoader.get(imgUrl, listener);
    }
}

5:效果圖:

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章