广点通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:效果图:

这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值