1334 lines
		
	
	
		
			49 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			1334 lines
		
	
	
		
			49 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| package com.novelbook.android;
 | ||
| 
 | ||
| import android.Manifest;
 | ||
| import android.annotation.TargetApi;
 | ||
| import android.app.Activity;
 | ||
| import android.app.ProgressDialog;
 | ||
| import android.content.Intent;
 | ||
| import android.content.pm.ActivityInfo;
 | ||
| import android.content.pm.PackageManager;
 | ||
| import android.os.Build;
 | ||
| import android.os.Bundle;
 | ||
| import android.os.Handler;
 | ||
| import android.os.Message;
 | ||
| import android.support.annotation.Nullable;
 | ||
| 
 | ||
| import android.support.v7.app.AppCompatActivity;
 | ||
| import android.support.v7.widget.RecyclerView;
 | ||
| import android.support.v7.widget.Toolbar;
 | ||
| import android.util.Log;
 | ||
| import android.view.LayoutInflater;
 | ||
| import android.view.MenuItem;
 | ||
| import android.view.View;
 | ||
| import android.view.ViewGroup;
 | ||
| import android.widget.Button;
 | ||
| import android.widget.FrameLayout;
 | ||
| import android.widget.ImageView;
 | ||
| import android.widget.TextView;
 | ||
| import android.widget.Toast;
 | ||
| 
 | ||
| import com.androidquery.AQuery;
 | ||
| import com.androidquery.callback.AQuery2;
 | ||
| import com.androidquery.callback.ImageOptions;
 | ||
| import com.bytedance.sdk.openadsdk.AdSlot;
 | ||
| import com.bytedance.sdk.openadsdk.TTAdConstant;
 | ||
| import com.bytedance.sdk.openadsdk.TTAdDislike;
 | ||
| import com.bytedance.sdk.openadsdk.TTAdNative;
 | ||
| import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
 | ||
| import com.bytedance.sdk.openadsdk.TTBannerAd;
 | ||
| 
 | ||
| import com.bytedance.sdk.openadsdk.TTFeedAd;
 | ||
| import com.bytedance.sdk.openadsdk.TTImage;
 | ||
| import com.bytedance.sdk.openadsdk.TTInteractionAd;
 | ||
| import com.bytedance.sdk.openadsdk.TTNativeAd;
 | ||
| import com.google.gson.Gson;
 | ||
| import com.novelbook.android.AD.toutiao.TTAdManagerHolder;
 | ||
| 
 | ||
| import com.novelbook.android.db.Novel;
 | ||
| 
 | ||
| import com.novelbook.android.netutils.NetUtil;
 | ||
| 
 | ||
| import com.novelbook.android.upgrade.UpdateManager;
 | ||
| import com.novelbook.android.utils.CommonUtil;
 | ||
| 
 | ||
| import com.novelbook.android.utils.Constants;
 | ||
| import com.novelbook.android.utils.ImageUtil;
 | ||
| 
 | ||
| import com.novelbook.android.utils.OnItemClickListener;
 | ||
| import com.novelbook.android.adapter.BookListAdapter;
 | ||
| 
 | ||
| 
 | ||
| import java.util.ArrayList;
 | ||
| import java.util.Date;
 | ||
| import java.util.HashMap;
 | ||
| import java.util.List;
 | ||
| import java.util.Map;
 | ||
| import java.util.concurrent.ConcurrentHashMap;
 | ||
| 
 | ||
| import com.qq.e.ads.banner.AbstractBannerADListener;
 | ||
| import com.qq.e.ads.banner.BannerView;
 | ||
| import com.qq.e.ads.cfg.VideoOption;
 | ||
| import com.qq.e.ads.nativ.ADSize;
 | ||
| import com.qq.e.ads.nativ.NativeADUnifiedListener;
 | ||
| import com.qq.e.ads.nativ.NativeExpressAD;
 | ||
| import com.qq.e.ads.nativ.NativeExpressADView;
 | ||
| import com.qq.e.ads.nativ.NativeExpressMediaListener;
 | ||
| import com.qq.e.ads.nativ.NativeUnifiedAD;
 | ||
| import com.qq.e.ads.nativ.NativeUnifiedADData;
 | ||
| import com.qq.e.comm.constants.AdPatternType;
 | ||
| import com.qq.e.comm.pi.AdData;
 | ||
| import com.qq.e.comm.util.AdError;
 | ||
| 
 | ||
| import org.litepal.util.Const;
 | ||
| 
 | ||
| import butterknife.BindView;
 | ||
| import butterknife.ButterKnife;
 | ||
| 
 | ||
| import static com.novelbook.android.utils.AdUtil.getAppID;
 | ||
| import static com.novelbook.android.utils.Constants.AD_TENCENT_QQ;
 | ||
| import static com.novelbook.android.utils.Constants.AD_TOUTIAO;
 | ||
| 
 | ||
| public abstract  class Activity_base extends AppCompatActivity implements NativeExpressAD.NativeExpressADListener {
 | ||
| 
 | ||
|     private static String TAG ="Activity_base";
 | ||
|     private ProgressDialog mProgressDialog;
 | ||
|     MyApp application;
 | ||
|     private Activity_base oContext;
 | ||
|     protected Gson gson = new Gson();
 | ||
|     protected long mLastBannerTime =0;
 | ||
|    //---qq ad---->
 | ||
|     private NativeExpressAD nativeExpressAD;
 | ||
|     private NativeExpressADView nativeExpressADView;
 | ||
|     //---qq ad end----
 | ||
|     @Nullable
 | ||
|     @BindView(R.id.toolbar)
 | ||
|     Toolbar toolbar;
 | ||
|     @Nullable
 | ||
|     @BindView(R.id.banner_container)
 | ||
|     FrameLayout mBannerContainer;
 | ||
|    /* @Nullable
 | ||
|     @BindView(R.id.banner_native_container)
 | ||
|     FrameLayout mNativeBannerContainer;*/
 | ||
|     @Nullable
 | ||
|     @BindView(R.id.recycleViewBookList)
 | ||
|     RecyclerView rvshudan;
 | ||
|     void showBook(String bookName) {  //show paihangbang activity
 | ||
|         Intent intent = new Intent(this, BookActivity.class);
 | ||
|         intent.putExtra("BOOKNAME",bookName);
 | ||
|         startActivity(intent);
 | ||
|     }
 | ||
|    // protected MyImageLoader imgloader = new MyImageLoader();
 | ||
|     /**
 | ||
|      * 初始化布局
 | ||
|      */
 | ||
|     public abstract int getLayoutRes();
 | ||
| 
 | ||
|     protected abstract void initViews();
 | ||
| 
 | ||
|     @Override
 | ||
|     protected void onCreate(Bundle savedInstanceState) {
 | ||
|         super.onCreate(savedInstanceState);
 | ||
|         if (application == null) {
 | ||
|             // 得到Application对象
 | ||
|             application = (MyApp) getApplication();
 | ||
|         }
 | ||
|         oContext = this;// 把当前的上下文对象赋值给BaseActivity
 | ||
|         addActivity();// 调用添加方法
 | ||
| 
 | ||
|         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
 | ||
|         setContentView(getLayoutRes());
 | ||
|         ButterKnife.bind(this);
 | ||
|         setupToolbar();
 | ||
|         // 初始化View注入
 | ||
|         this.mAQuery = new AQuery2(this);
 | ||
|         initAD_TouTiao();
 | ||
|         setTitle();
 | ||
|         initData();
 | ||
|         initViews();
 | ||
|         initNaviBanner();
 | ||
|         if (Build.VERSION.SDK_INT >= 23) {
 | ||
|             checkAndRequestPermission();
 | ||
|         }
 | ||
|     }
 | ||
|     @TargetApi(Build.VERSION_CODES.M)
 | ||
|     private void checkAndRequestPermission() {
 | ||
|         List<String> lackedPermission = new ArrayList<String>();
 | ||
|         if (!(checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)) {
 | ||
|             lackedPermission.add(Manifest.permission.READ_PHONE_STATE);
 | ||
|         }
 | ||
| 
 | ||
|         if (!(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
 | ||
|             lackedPermission.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
 | ||
|         }
 | ||
| 
 | ||
|         if (!(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)) {
 | ||
|             lackedPermission.add(Manifest.permission.ACCESS_FINE_LOCATION);
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|             // 请求所缺少的权限,在onRequestPermissionsResult中再看是否获得权限,如果获得权限就可以调用SDK,否则不要调用SDK。
 | ||
|             String[] requestPermissions = new String[lackedPermission.size()];
 | ||
|             if(requestPermissions.length>0) {
 | ||
|                 lackedPermission.toArray(requestPermissions);
 | ||
|                 requestPermissions(requestPermissions, 1024);
 | ||
|             }
 | ||
| 
 | ||
|     }
 | ||
|     void initNaviBanner(){
 | ||
|         if(mBannerContainer==null){
 | ||
|             return;
 | ||
|         }
 | ||
|         if(Constants.AD_SETTING.isShowAdsense() && Constants.AD_SETTING.getBigBanner().isShow() && NetUtil.isNetworkConnected()){
 | ||
|             loadNativeBannerAd();
 | ||
|             mBannerContainer.setVisibility(View.VISIBLE);
 | ||
|         }else{
 | ||
|             mBannerContainer.setVisibility(View.GONE);
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     protected void  setupToolbar(){
 | ||
|       //  Toolbar toolbar = findViewById(R.id.toolbar);
 | ||
|      //  setSupportActionBar(toolbar);
 | ||
|        setSupportActionBar(toolbar);
 | ||
|        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 | ||
|     }
 | ||
|     protected abstract void setTitle();
 | ||
| 
 | ||
|    protected abstract  void initData() ;
 | ||
| 
 | ||
|     protected void closeCurrentActitivty(){
 | ||
|         if( this instanceof BookActivity ) {
 | ||
|          // return;
 | ||
|         }
 | ||
|         finish();
 | ||
|     }
 | ||
|     protected BookListAdapter getBookListAdapter(List<Novel> mDatas,int itemResourceId){
 | ||
|         BookListAdapter mAdapter = new BookListAdapter(this ,mDatas,itemResourceId,new  OnItemClickListener()
 | ||
|         {
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onItemClick(View view, int position)
 | ||
|             {
 | ||
| 
 | ||
|              //   showBook("射雕" +position);
 | ||
|                 showBookDetail(mDatas.get(position));
 | ||
|                 closeCurrentActitivty();
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onItemLongClick(View view, int position)
 | ||
|             {
 | ||
|                 // initDialog(position);
 | ||
|                 // mAdapter.removeData(position);
 | ||
|             }
 | ||
|             @Override
 | ||
|             public void onLinearOutClick(View view, int bookId,int llId) {
 | ||
|                 //  Toast.makeText(activity, "book "+ bookId + " clicked", Toast.LENGTH_SHORT).show();
 | ||
| 
 | ||
|                 // showBookDetail( mData.get(bookId));
 | ||
|             }
 | ||
|         });
 | ||
| 
 | ||
|         return mAdapter;
 | ||
|     }
 | ||
|     void showBookDetail(Novel book) {
 | ||
|        List<Activity> acts =   application.getActivityList();
 | ||
|        for(Activity activity :acts){
 | ||
|            if(activity instanceof BookActivity){
 | ||
|                activity.finish();
 | ||
|                break;
 | ||
|            }
 | ||
|        }
 | ||
| 
 | ||
| 
 | ||
|         Intent intent = new Intent(this, BookActivity.class);
 | ||
|         intent.putExtra(BookActivity.EXTRA_BOOK,book);
 | ||
|         startActivity(intent);
 | ||
|     }
 | ||
| 
 | ||
|     @Override
 | ||
|     public boolean onOptionsItemSelected(MenuItem item) {
 | ||
|         if(item.getItemId()==android.R.id.home){ //拦截toolbar 返回事件
 | ||
| 
 | ||
|                 finish();
 | ||
|             return true;
 | ||
|             }
 | ||
| 
 | ||
|         return super.onOptionsItemSelected(item);
 | ||
|     }
 | ||
| 
 | ||
|     protected  List<Novel> getFakeData(int max ){
 | ||
|         List<Novel>  mDatas = new ArrayList<Novel>();
 | ||
|         for (int i = 0; i <max; i++)
 | ||
|         {
 | ||
|             Novel bk = new Novel();
 | ||
|             bk.setAuthor("金庸");
 | ||
|             bk.setName("射雕英雄传" +(char)i);
 | ||
|             bk.setNovelType("武侠");
 | ||
|             bk.setDesc("南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事");
 | ||
|             mDatas.add(bk);
 | ||
|         }
 | ||
|         return mDatas;
 | ||
|     }
 | ||
| 
 | ||
|     public abstract void fillData();
 | ||
|     protected static final int MSG_VERSION_LATEST =4;
 | ||
|     protected static final int MSG_VERSION_FAILE =5;
 | ||
|     Handler handler = new Handler() {
 | ||
|         @Override
 | ||
|         public void handleMessage(Message msg) {
 | ||
| 
 | ||
|             int wt = msg.what;
 | ||
| 
 | ||
|             if (msg.what == 1) {
 | ||
|                 fillData();
 | ||
|             } else if (msg.what == 2)  //
 | ||
|             {
 | ||
| 
 | ||
| 
 | ||
|                 Toast.makeText(Activity_base.this, " 请求失败", Toast.LENGTH_LONG).show();
 | ||
|             } else if (msg.what == 3) {
 | ||
| 
 | ||
|                 Toast.makeText(Activity_base.this, " ", Toast.LENGTH_LONG).show();
 | ||
|             } else if (msg.what == MSG_VERSION_LATEST) {
 | ||
| 
 | ||
|                 Toast.makeText(Activity_base.this, "已是最新版本", Toast.LENGTH_LONG).show();
 | ||
|             }else if (msg.what == MSG_VERSION_FAILE) {
 | ||
| 
 | ||
|                 Toast.makeText(Activity_base.this, "版本检查失败", Toast.LENGTH_LONG).show();
 | ||
|             }
 | ||
| 
 | ||
|             hideProgress();
 | ||
| 
 | ||
|         }
 | ||
|     };
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|     class MyViewHolder extends RecyclerView.ViewHolder {
 | ||
|         @BindView(R.id.title)
 | ||
|         TextView tvTitle;
 | ||
|         @BindView(R.id.author)
 | ||
|         TextView tvAuthor;
 | ||
|         @BindView(R.id.category)
 | ||
|         TextView tvCate;
 | ||
|         @BindView(R.id.desc)
 | ||
|         TextView tvDesc;
 | ||
| 
 | ||
|         @Nullable
 | ||
|         @BindView(R.id.btnItemDelete)
 | ||
|         Button btnDelete;
 | ||
| 
 | ||
|         public MyViewHolder(View view) {
 | ||
|             super(view);
 | ||
|             ButterKnife.bind(this, view);
 | ||
|             //tvTitle = (TextView) view.findViewById(R.id.title);
 | ||
|             // tvAuthor = (TextView) view.findViewById(R.id.author);
 | ||
| 
 | ||
|         }
 | ||
|     }
 | ||
|     public void showProgressDialog(boolean flag, String message) {
 | ||
|         if (mProgressDialog == null) {
 | ||
|             mProgressDialog = new ProgressDialog(this);
 | ||
|             mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
 | ||
|             mProgressDialog.setCancelable(true);
 | ||
|             mProgressDialog.setCanceledOnTouchOutside(false);
 | ||
|             mProgressDialog.setMessage(message);
 | ||
|         }
 | ||
|         if(NetUtil.isNetworkConnected())
 | ||
|         mProgressDialog.show();
 | ||
|     }
 | ||
| 
 | ||
|     public void hideProgress() {
 | ||
|         if (mProgressDialog == null)
 | ||
|             return;
 | ||
| 
 | ||
|         if (mProgressDialog.isShowing()) {
 | ||
|             mProgressDialog.dismiss();
 | ||
|         }
 | ||
|     }
 | ||
|     /**
 | ||
|      * 检查是否拥有权限
 | ||
|      * @param thisActivity
 | ||
|      * @param permission
 | ||
|      * @param requestCode
 | ||
|      * @param errorText
 | ||
|      */
 | ||
|     protected void checkPermission (Activity thisActivity, String permission, int requestCode, String errorText) {
 | ||
|         CommonUtil.checkPermission(thisActivity,permission,requestCode,errorText);
 | ||
|     }
 | ||
| 
 | ||
|     protected void loadImageView(String url, ImageView imageView){
 | ||
|         ImageUtil.loadImage( this, url , imageView);
 | ||
|     }
 | ||
| 
 | ||
|     public void checkUpdate(boolean isSilence){
 | ||
|             UpdateManager manager = new UpdateManager(this);
 | ||
|             if(isSilence)
 | ||
|                 manager.checkUpdateSilence();
 | ||
|             else
 | ||
|                 manager.checkUpdate();
 | ||
|     }
 | ||
|     // 添加Activity方法
 | ||
|     public void addActivity() {
 | ||
|         application.addActivity_(oContext);// 调用myApplication的添加Activity方法
 | ||
|     }
 | ||
| 
 | ||
|     //销毁当个Activity方法
 | ||
|     public void removeActivity() {
 | ||
|         application.removeActivity_(oContext);// 调用myApplication的销毁单个Activity方法
 | ||
|     }
 | ||
| 
 | ||
|     //销毁所有Activity方法
 | ||
|     public void removeALLActivity() {
 | ||
|         application.removeALLActivity_();// 调用myApplication的销毁所有Activity方法
 | ||
|     }
 | ||
| 
 | ||
|     @Override
 | ||
|     public void finish() {
 | ||
|         removeActivity();
 | ||
|         super.finish();
 | ||
|     }
 | ||
| 
 | ||
|    public void loadNativeBannerAd(FrameLayout bannerContainer ) {
 | ||
|             mBannerContainer =bannerContainer;
 | ||
|             loadNativeBannerAd( )  ;
 | ||
|     }
 | ||
| 
 | ||
|    private void loadNativeBannerAd() {
 | ||
|      /*   if (mBannerContainer .getVisibility() != View.VISIBLE) {
 | ||
|             mBannerContainer.setVisibility(View.VISIBLE);
 | ||
|         }
 | ||
|     */
 | ||
| 
 | ||
|      if(!Constants.AD_SETTING.isShowAdsense()  ){return;}
 | ||
| 
 | ||
|      mLastBannerTime=0;//复位小banner请求时间戳,否则会在小banner位置显示nati banner 信息
 | ||
| 
 | ||
|         if (mBannerContainer.getChildCount() > 0) {
 | ||
|             mBannerContainer.removeAllViews();
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         if(Constants.AD_SETTING.getChapterContentBanner().getSource()  == AD_TENCENT_QQ) {
 | ||
|             loadNativeBanner_qq(-1, 388);
 | ||
|         }else if(Constants.AD_SETTING.getChapterContentBanner().getSource() == AD_TOUTIAO) {
 | ||
|             loadNativeBanner_toutiao(mBannerContainer ,690,388);
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     public void loadBanner(FrameLayout bannerContainer, int width, int height) {
 | ||
|         mBannerContainer =bannerContainer;
 | ||
|         loadBanner(  width,   height);
 | ||
|     }
 | ||
| 
 | ||
|     private void loadBanner( int width, int height){
 | ||
| 
 | ||
|        if(mBannerContainer.getChildCount()>0)
 | ||
|        if (new Date().getTime() -  mLastBannerTime  < Constants.AD_SETTING.getChapterBanner().getLifeCycleSecond()*1000){
 | ||
|            Log.d(TAG, String.format("loadBanner:mLastBannerTime %s ,timepassed %s",mLastBannerTime,(new Date().getTime() -  mLastBannerTime)/1000));
 | ||
|             return;
 | ||
|        }
 | ||
| 
 | ||
|         Log.d(TAG, "loadBanner:mLastBannerTime start new banner request");
 | ||
|         if(Constants.AD_SETTING.getChapterBanner().getSource()== AD_TENCENT_QQ) {
 | ||
|             getBanner_qq();
 | ||
|         }else if(Constants.AD_SETTING.getChapterBanner().getSource()== AD_TOUTIAO) {
 | ||
|             loadBanner_toutiao(mBannerContainer, width,height);
 | ||
| 
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| //------ 头条 ad -----
 | ||
|     AQuery2 mAQuery;
 | ||
|     Button mCreativeButton;
 | ||
|     TTAdNative mTTAdNative;
 | ||
|     void initAD_TouTiao(){
 | ||
|         if(Constants.AD_SETTING.isShowAdsense())
 | ||
|         try {
 | ||
|             mTTAdNative = TTAdManagerHolder.get().createAdNative(this);
 | ||
|             //step3:(可选,强烈建议在合适的时机调用):申请部分权限,如read_phone_state,防止获取不了imei时候,下载类广告没有填充的问题。
 | ||
|             TTAdManagerHolder.get().requestPermissionIfNecessary(this);
 | ||
|             //this.mAQuery = new AQuery2(this);
 | ||
|         }catch (Exception er){
 | ||
|             Log.e(TAG, "initAD_TouTiao: ", er);
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     TTAdNative getTTAdNative(){
 | ||
|         if(mTTAdNative== null){
 | ||
|             if(Constants.AD_SETTING.isShowAdsense()){
 | ||
|                 TTAdManagerHolder.init(MyApp.applicationContext);
 | ||
|                 initAD_TouTiao();
 | ||
|             }
 | ||
|         }
 | ||
|         return mTTAdNative;
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|     //----toutiao ad ----
 | ||
| //    boolean mShowAd =false;
 | ||
|        void loadBanner_toutiao(FrameLayout bannerContainer,int width, int height) {
 | ||
|         //step4:创建广告请求参数AdSlot,具体参数含义参考文档
 | ||
|          if(!Constants.AD_SETTING.isShowAdsense() || getTTAdNative()==null ){return;}
 | ||
|            String codeId = Constants.AD_SETTING.getChapterBanner().getSlotId(Constants.AD_TOUTIAO) ;
 | ||
|        /* if(height >390){
 | ||
|             width =(int)(height*1.78);
 | ||
|             if(width > Constants.SCREEN_WIDTH_PIX-50 ){
 | ||
|                 width= Constants.SCREEN_WIDTH_PIX-50;
 | ||
|                 height =(int)(width/1.78);
 | ||
|             }
 | ||
|         }else if(height >150){
 | ||
|             width =(int)(height*1.78);
 | ||
|         }*/
 | ||
|         // width =690;
 | ||
|         //height=150;
 | ||
|      //  height=height > 500 ? 500: height;
 | ||
|         Log.d(TAG, String.format("loadBannerAd:finial width %s, height %s ",width,height));
 | ||
|         AdSlot adSlot = new AdSlot.Builder()
 | ||
|                 .setCodeId(codeId) //广告位id
 | ||
|                 .setSupportDeepLink(true)
 | ||
|                 .setImageAcceptedSize(width, height)
 | ||
|                 .build();
 | ||
|         //step5:请求广告,对请求回调的广告作渲染处理
 | ||
|          getTTAdNative().loadBannerAd(adSlot, new TTAdNative.BannerAdListener() {
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onError(int code, String message) {
 | ||
|                // TToast.show(oContext, "load error : " + code + ", " + message);
 | ||
|                 Log.e(TAG, String.format("loadBannerAd:code %s, message %s ", code,message));
 | ||
|                 bannerContainer.removeAllViews();
 | ||
|                // bannerContainer.setVisibility(View.GONE);
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onBannerAdLoad(final TTBannerAd ad) {
 | ||
|                 if (ad == null ) {
 | ||
|                     return;
 | ||
|                 }
 | ||
|                 View bannerView = ad.getBannerView();
 | ||
|                 if (bannerView == null) {
 | ||
|                     return;
 | ||
|                 }
 | ||
|                 mLastBannerTime =new Date().getTime();
 | ||
|                 //设置轮播的时间间隔  间隔在30s到120秒之间的值,不设置默认不轮播
 | ||
|                // ad.setSlideIntervalTime(30 * 1000);
 | ||
|                 bannerContainer.removeAllViews();
 | ||
|                 bannerContainer.addView(bannerView);
 | ||
|              //   bannerContainer.setVisibility(View.VISIBLE);
 | ||
|            //     Log.d(TAG, "loadBannerAd: set banner VISIBLE");
 | ||
|              //   mTTAdNative.loadBannerAd(adSlot,null);
 | ||
|                 //设置广告互动监听回调
 | ||
|                 ad.setBannerInteractionListener(new TTBannerAd.AdInteractionListener() {
 | ||
|                     @Override
 | ||
|                     public void onAdClicked(View view, int type) {
 | ||
|                       //  TToast.show(oContext, "广告被点击");
 | ||
|                     }
 | ||
| 
 | ||
|                     @Override
 | ||
|                     public void onAdShow(View view, int type) {
 | ||
|                       //  TToast.show(oContext, "广告展示");
 | ||
|                     }
 | ||
|                 });
 | ||
|                 //(可选)设置下载类广告的下载监听
 | ||
|                 bindDownloadListener(ad);
 | ||
|                 //在banner中显示网盟提供的dislike icon,有助于广告投放精准度提升
 | ||
|                 ad.setShowDislikeIcon(new TTAdDislike.DislikeInteractionCallback() {
 | ||
|                     @Override
 | ||
|                     public void onSelected(int position, String value) {
 | ||
|                       //  TToast.show(oContext, "点击 " + value);
 | ||
|                         //用户选择不喜欢原因后,移除广告展示
 | ||
|                         bannerContainer.removeAllViews();
 | ||
|                     }
 | ||
| 
 | ||
|                     @Override
 | ||
|                     public void onCancel() {
 | ||
|                       //  TToast.show(oContext, "点击取消 ");
 | ||
|                     }
 | ||
|                 });
 | ||
| 
 | ||
|                 //获取网盟dislike dialog,您可以在您应用中本身自定义的dislike icon 按钮中设置 mTTAdDislike.showDislikeDialog();
 | ||
|                 /*mTTAdDislike = ad.getDislikeDialog(new TTAdDislike.DislikeInteractionCallback() {
 | ||
|                         @Override
 | ||
|                         public void onSelected(int position, String value) {
 | ||
|                             TToast.show(mContext, "点击 " + value);
 | ||
|                         }
 | ||
| 
 | ||
|                         @Override
 | ||
|                         public void onCancel() {
 | ||
|                             TToast.show(mContext, "点击取消 ");
 | ||
|                         }
 | ||
|                     });
 | ||
|                 if (mTTAdDislike != null) {
 | ||
|                     XXX.setOnClickListener(new View.OnClickListener() {
 | ||
|                         @Override
 | ||
|                         public void onClick(View v) {
 | ||
|                             mTTAdDislike.showDislikeDialog();
 | ||
|                         }
 | ||
|                     });
 | ||
|                 } */
 | ||
| 
 | ||
|             }
 | ||
|         });
 | ||
|     }
 | ||
|     private boolean mHasShowDownloadActive = false;
 | ||
|     private void bindDownloadListener(TTBannerAd ad) {
 | ||
|         ad.setDownloadListener(new TTAppDownloadListener() {
 | ||
|             @Override
 | ||
|             public void onIdle() {
 | ||
|              //   TToast.show(oContext, "点击图片开始下载", Toast.LENGTH_LONG);
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
 | ||
|                 if (!mHasShowDownloadActive) {
 | ||
|                     mHasShowDownloadActive = true;
 | ||
|                  //   TToast.show(oContext, "下载中,点击图片暂停", Toast.LENGTH_LONG);
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
 | ||
|               //  TToast.show(oContext, "下载暂停,点击图片继续", Toast.LENGTH_LONG);
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
 | ||
|              //   TToast.show(oContext, "下载失败,点击图片重新下载", Toast.LENGTH_LONG);
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onInstalled(String fileName, String appName) {
 | ||
|               //  TToast.show(oContext, "安装完成,点击图片打开", Toast.LENGTH_LONG);
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onDownloadFinished(long totalBytes, String fileName, String appName) {
 | ||
|                // TToast.show(oContext, "点击图片安装", Toast.LENGTH_LONG);
 | ||
|             }
 | ||
|         });
 | ||
|     }
 | ||
|     //-------------native toutiao ad
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|  //   List<TTNativeAd> toutiaoNati_Banner_AdCache = new ArrayList<TTNativeAd>();
 | ||
|     Map<String, List<TTNativeAd> > toutiaoNati_Banner_AdCache = new ConcurrentHashMap<String, List<TTNativeAd>>();
 | ||
|      void loadNativeBanner_toutiao(FrameLayout bannerContainer,int width,int height) {
 | ||
|          //step4:创建广告请求参数AdSlot,注意其中的setNativeAdtype方法,具体参数含义参考文档
 | ||
| 
 | ||
|          if (!Constants.AD_SETTING.isShowAdsense()|| getTTAdNative() == null) {
 | ||
|              return;
 | ||
|          }
 | ||
| 
 | ||
|          String codeId =Constants.AD_SETTING.getChapterContentBanner().getSlotId(Constants.AD_TOUTIAO);
 | ||
| 
 | ||
|          final   String key =width+"_"+height;
 | ||
|          List<TTNativeAd> adCache =null;
 | ||
|          if( toutiaoNati_Banner_AdCache.containsKey(key)){
 | ||
|              adCache  = toutiaoNati_Banner_AdCache.get(key);
 | ||
| 
 | ||
|          }
 | ||
|          final  boolean isfillAd = adCache!=null &&adCache.size()>1;
 | ||
| 
 | ||
|        //  final  boolean isfillAd =!toutiaoNati_Banner_AdCache.containsKey(key);
 | ||
| 
 | ||
|          if(adCache!=null && isfillAd){
 | ||
|              Log.d(TAG, "loadNativeBannerAd: set ad from cache, cache Size " + adCache.size());
 | ||
|               setNativeBanner(bannerContainer, key);
 | ||
|              if(adCache.size()>0){
 | ||
|                  return;
 | ||
|              }
 | ||
| 
 | ||
|          }
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|          final AdSlot adSlot = new AdSlot.Builder()
 | ||
|                  .setCodeId(codeId)
 | ||
|                  .setSupportDeepLink(true)
 | ||
|                  .setImageAcceptedSize(width, height)
 | ||
|                  .setNativeAdType(AdSlot.TYPE_BANNER) //请求原生广告时候,请务必调用该方法,设置参数为TYPE_BANNER或TYPE_INTERACTION_AD
 | ||
|                  .setAdCount(1)
 | ||
|                  .build();
 | ||
| 
 | ||
|          //step5:请求广告,对请求回调的广告作渲染处理
 | ||
|          getTTAdNative().loadNativeAd(adSlot, new TTAdNative.NativeAdListener() {
 | ||
|              @Override
 | ||
|              public void onError(int code, String message) {
 | ||
|                  // TToast.show(oContext, "load error : " + code + ", " + message);
 | ||
|                  Log.e(TAG, "loadBannerAd: " + code + ", " + message);
 | ||
|                 // bannerContainer.setBackgroundResource(R.color.transparent);
 | ||
|              }
 | ||
| 
 | ||
|              @Override
 | ||
|              public void onNativeAdLoad(List<TTNativeAd> ads) {
 | ||
|                //  bannerContainer.setBackgroundResource(R.color.transparent);
 | ||
|                  List<TTNativeAd> tmp = new ArrayList<TTNativeAd>();
 | ||
|                  for (TTNativeAd ad : ads) {
 | ||
|                      if (ad != null) {
 | ||
|                          tmp.add(ad);
 | ||
|                      }
 | ||
|                  }
 | ||
|                  if (!toutiaoNati_Banner_AdCache.containsKey(key)) {
 | ||
|                      toutiaoNati_Banner_AdCache.put(key, tmp);
 | ||
|                  } else {
 | ||
|                      toutiaoNati_Banner_AdCache.get(key).addAll(tmp);
 | ||
|                  }
 | ||
| 
 | ||
|                 boolean isfillAd1 =toutiaoNati_Banner_AdCache.get(key).size()>1;
 | ||
|                  Log.d(TAG, String.format("loadNativeBannerAd: ads count %s ,not null count %s,key %s,will fill ad %s", ads.size(), tmp.size(), key, isfillAd1));
 | ||
|                  if (isfillAd1) {
 | ||
|                      setNativeBanner(bannerContainer, key);
 | ||
|                  }else{
 | ||
|                      loadNativeBanner_toutiao(mBannerContainer, width,  height);
 | ||
|                      Log.d(TAG, String.format("loadNativeBannerAd:  load again ...  containsKey(key) ? %s,ads size %s",toutiaoNati_Banner_AdCache.containsKey(key),
 | ||
|                              toutiaoNati_Banner_AdCache.containsKey(key)?toutiaoNati_Banner_AdCache.get(key).size() :0
 | ||
|                      ));
 | ||
|                  }
 | ||
|                /*  if (!toutiaoNati_Banner_AdCache.containsKey(key)){
 | ||
|                      loadNativeBannerAd(bannerContainer,  codeId,  width,  height);
 | ||
|                      Log.d(TAG, String.format("loadNativeBannerAd:  load again ...  containsKey(key) ? %s,ads size %s",toutiaoNati_Banner_AdCache.containsKey(key),
 | ||
|                              toutiaoNati_Banner_AdCache.containsKey(key)?toutiaoNati_Banner_AdCache.get(key).size() :0
 | ||
|                      ));
 | ||
|                 }*/
 | ||
| 
 | ||
|                 /* if (ads.get(0) == null || !mShowAd) {
 | ||
|                      return;
 | ||
|                  }
 | ||
|                  View bannerView = LayoutInflater.from(oContext).inflate(R.layout.ad_toutiao_native_ad, bannerContainer, false);
 | ||
|                  if (bannerView == null) {
 | ||
|                      return;
 | ||
|                  }
 | ||
|                  if (mCreativeButton != null) {
 | ||
|                      //防止内存泄漏
 | ||
|                      mCreativeButton = null;
 | ||
|                  }
 | ||
|                  bannerContainer.removeAllViews();
 | ||
|                  bannerContainer.addView(bannerView);
 | ||
|                  bannerContainer.setVisibility(View.VISIBLE);
 | ||
|                  if (!Constants.AD_BACKGROUND_TRANSPARENT)
 | ||
|                      bannerContainer.setBackgroundResource(R.color.mintcream);  // @color/mintcream
 | ||
|                  Log.d(TAG, "loadBannerAd: set banner VISIBLE,mShowAd " + mShowAd);
 | ||
|                  //绑定原生广告的数据
 | ||
|                  setAdData(bannerContainer, bannerView, ads.get(0));*/
 | ||
|                  //     mTTAdNative.loadNativeAd(adSlot,null);
 | ||
|              }
 | ||
|          });
 | ||
|      }
 | ||
|      void setNativeBanner(FrameLayout bannerContainer,String key ){
 | ||
| 
 | ||
|          if(!toutiaoNati_Banner_AdCache.containsKey(key)){
 | ||
|              return;
 | ||
|          }
 | ||
|          List<TTNativeAd> adCache = toutiaoNati_Banner_AdCache.get(key);
 | ||
|          TTNativeAd nativeAd =null ;
 | ||
|         int i=0;
 | ||
|          for (TTNativeAd ad :adCache){
 | ||
|                if(ad!=null){
 | ||
|                    nativeAd =ad;
 | ||
|                    break;
 | ||
|                }
 | ||
|              i++;
 | ||
|          }
 | ||
| 
 | ||
|          if (nativeAd == null  ) {
 | ||
|              return;
 | ||
|          }
 | ||
|          adCache.remove(i);
 | ||
|          if(adCache.size()>0) {
 | ||
|              toutiaoNati_Banner_AdCache.put(key, adCache);
 | ||
|          }else{
 | ||
|              toutiaoNati_Banner_AdCache.remove(key);
 | ||
|          }
 | ||
| 
 | ||
|          View bannerView = LayoutInflater.from(oContext).inflate(R.layout.ad_toutiao_native_ad, bannerContainer, false);
 | ||
|          if (bannerView == null) {
 | ||
|              return;
 | ||
|          }
 | ||
|          if (mCreativeButton != null) {
 | ||
|              //防止内存泄漏
 | ||
|              mCreativeButton = null;
 | ||
|          }
 | ||
| 
 | ||
|          bannerContainer.removeAllViews();
 | ||
|          bannerContainer.addView(bannerView);
 | ||
|          //bannerContainer.setVisibility(View.VISIBLE);
 | ||
|          if (!Constants.AD_BACKGROUND_TRANSPARENT)
 | ||
|              bannerContainer.setBackgroundResource(R.color.mintcream);  // @color/mintcream
 | ||
|        //  Log.d(TAG, "loadBannerAd: set banner VISIBLE,mShowAd " + mShowAd);
 | ||
|          //绑定原生广告的数据
 | ||
|          setAdData(bannerContainer, bannerView,  nativeAd);
 | ||
|          //     mTTAdNative.loadNativeAd(adSlot,null);
 | ||
|      }
 | ||
| 
 | ||
|     @SuppressWarnings("RedundantCast")
 | ||
|     private void setAdData(FrameLayout bannerContainer,View nativeView, TTNativeAd nativeAd) {
 | ||
|         ((TextView) nativeView.findViewById(R.id.tv_native_ad_title)).setText(nativeAd.getTitle());
 | ||
|         ((TextView) nativeView.findViewById(R.id.tv_native_ad_desc)).setText(nativeAd.getDescription());
 | ||
|         ImageView imgDislike = nativeView.findViewById(R.id.img_native_dislike);
 | ||
|         bindDislikeAction(  bannerContainer,nativeAd, imgDislike);
 | ||
|         if (nativeAd.getImageList() != null && !nativeAd.getImageList().isEmpty()) {
 | ||
|             TTImage image = nativeAd.getImageList().get(0);
 | ||
|             if (image != null && image.isValid()) {
 | ||
|                 mAQuery.id(nativeView.findViewById(R.id.iv_native_image)).image(image.getImageUrl());
 | ||
|             }
 | ||
|         }
 | ||
|         TTImage icon = nativeAd.getIcon();
 | ||
|         if (icon != null && icon.isValid()) {
 | ||
|             ImageOptions options = new ImageOptions();
 | ||
|             mAQuery.id((nativeView.findViewById(R.id.iv_native_icon))).image(icon.getImageUrl(), options);
 | ||
|         }
 | ||
|         mCreativeButton = (Button) nativeView.findViewById(R.id.btn_native_creative);
 | ||
|         //可根据广告类型,为交互区域设置不同提示信息
 | ||
|         switch (nativeAd.getInteractionType()) {
 | ||
|             case TTAdConstant.INTERACTION_TYPE_DOWNLOAD:
 | ||
|                 //如果初始化ttAdManager.createAdNative(getApplicationContext())没有传入activity 则需要在此传activity,否则影响使用Dislike逻辑
 | ||
|                 nativeAd.setActivityForDownloadApp(this);
 | ||
|                 mCreativeButton.setVisibility(View.VISIBLE);
 | ||
|                 nativeAd.setDownloadListener(mDownloadListener); // 注册下载监听器
 | ||
|                 break;
 | ||
|             case TTAdConstant.INTERACTION_TYPE_DIAL:
 | ||
|                 mCreativeButton.setVisibility(View.VISIBLE);
 | ||
|                 mCreativeButton.setText("立即拨打");
 | ||
|                 break;
 | ||
|             case TTAdConstant.INTERACTION_TYPE_LANDING_PAGE:
 | ||
|             case TTAdConstant.INTERACTION_TYPE_BROWSER:
 | ||
|                 mCreativeButton.setVisibility(View.VISIBLE);
 | ||
|                 mCreativeButton.setText("查看详情");
 | ||
|                 break;
 | ||
|             default:
 | ||
|                 mCreativeButton.setVisibility(View.GONE);
 | ||
|                // TToast.show(oContext, "交互类型异常");
 | ||
|         }
 | ||
| 
 | ||
|         //可以被点击的view, 也可以把nativeView放进来意味整个广告区域可被点击
 | ||
|         List<View> clickViewList = new ArrayList<>();
 | ||
|         clickViewList.add(nativeView);
 | ||
| 
 | ||
|         //触发创意广告的view(点击下载或拨打电话)
 | ||
|         List<View> creativeViewList = new ArrayList<>();
 | ||
|         //如果需要点击图文区域也能进行下载或者拨打电话动作,请将图文区域的view传入
 | ||
|         //creativeViewList.add(nativeView);
 | ||
|         creativeViewList.add(mCreativeButton);
 | ||
| 
 | ||
|         //重要! 这个涉及到广告计费,必须正确调用。convertView必须使用ViewGroup。
 | ||
|         nativeAd.registerViewForInteraction((ViewGroup) nativeView, clickViewList, creativeViewList, imgDislike, new TTNativeAd.AdInteractionListener() {
 | ||
|             @Override
 | ||
|             public void onAdClicked(View view, TTNativeAd ad) {
 | ||
|                 if (ad != null) {
 | ||
|                   //  TToast.show(oContext, "广告" + ad.getTitle() + "被点击");
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onAdCreativeClick(View view, TTNativeAd ad) {
 | ||
|                 if (ad != null) {
 | ||
|                    // TToast.show(oContext, "广告" + ad.getTitle() + "被创意按钮被点击");
 | ||
|                 }
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onAdShow(TTNativeAd ad) {
 | ||
|                 if (ad != null) {
 | ||
|                    // TToast.show(oContext, "广告" + ad.getTitle() + "展示");
 | ||
|                 }
 | ||
|             }
 | ||
|         });
 | ||
| 
 | ||
|     }
 | ||
| 
 | ||
|     //接入网盟的dislike 逻辑,有助于提示广告精准投放度
 | ||
|     private void bindDislikeAction(FrameLayout bannerContainer,TTNativeAd ad, View dislikeView) {
 | ||
|         final TTAdDislike ttAdDislike = ad.getDislikeDialog(this);
 | ||
|         if (ttAdDislike != null) {
 | ||
|             ttAdDislike.setDislikeInteractionCallback(new TTAdDislike.DislikeInteractionCallback() {
 | ||
|                 @Override
 | ||
|                 public void onSelected(int position, String value) {
 | ||
|                     bannerContainer.removeAllViews();
 | ||
|                 }
 | ||
| 
 | ||
|                 @Override
 | ||
|                 public void onCancel() {
 | ||
| 
 | ||
|                 }
 | ||
|             });
 | ||
|         }
 | ||
|         dislikeView.setOnClickListener(new View.OnClickListener() {
 | ||
|             @Override
 | ||
|             public void onClick(View v) {
 | ||
|                 if (ttAdDislike != null)
 | ||
|                     ttAdDislike.showDislikeDialog();
 | ||
|             }
 | ||
|         });
 | ||
|     }
 | ||
| 
 | ||
|     private final TTAppDownloadListener mDownloadListener = new TTAppDownloadListener() {
 | ||
|         @Override
 | ||
|         public void onIdle() {
 | ||
|             if (mCreativeButton != null) {
 | ||
|                 mCreativeButton.setText("开始下载");
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
 | ||
|             if (mCreativeButton != null) {
 | ||
|                 if (totalBytes <= 0L) {
 | ||
|                     mCreativeButton.setText("下载中 进度:0");
 | ||
|                 } else {
 | ||
|                     mCreativeButton.setText("下载中 进度:" + (currBytes * 100 / totalBytes));
 | ||
|                 }
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
 | ||
|             if (mCreativeButton != null) {
 | ||
|                 mCreativeButton.setText("下载暂停 进度:" + (currBytes * 100 / totalBytes));
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
 | ||
|             if (mCreativeButton != null) {
 | ||
|                 mCreativeButton.setText("重新下载");
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onInstalled(String fileName, String appName) {
 | ||
|             if (mCreativeButton != null) {
 | ||
|                 mCreativeButton.setText("点击打开");
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onDownloadFinished(long totalBytes, String fileName, String appName) {
 | ||
|             if (mCreativeButton != null) {
 | ||
|                 mCreativeButton.setText("点击安装");
 | ||
|             }
 | ||
|         }
 | ||
|     };
 | ||
| 
 | ||
|     public TTAdNative getAdNative(){
 | ||
|         return getTTAdNative();//mTTAdNative;
 | ||
|     }
 | ||
|      void loadInteractionAd(String codeId) {
 | ||
|         //step4:创建插屏广告请求参数AdSlot,具体参数含义参考文档
 | ||
|         if(getAdNative()==null){
 | ||
|             return;
 | ||
| 
 | ||
|         }
 | ||
|         AdSlot adSlot = new AdSlot.Builder()
 | ||
|                 .setCodeId(codeId)
 | ||
|                 .setSupportDeepLink(true)
 | ||
|                 .setImageAcceptedSize(600, 600) //根据广告平台选择的尺寸,传入同比例尺寸
 | ||
|                 .build();
 | ||
|         //step5:请求广告,调用插屏广告异步请求接口
 | ||
|         getAdNative().loadInteractionAd(adSlot, new TTAdNative.InteractionAdListener() {
 | ||
|             @Override
 | ||
|             public void onError(int code, String message) {
 | ||
|                 Log.e(TAG, String.format("loadInteractionAd onError: code %s ,message %s",code,message) );
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onInteractionAdLoad(TTInteractionAd ttInteractionAd) {
 | ||
|               //  TToast.show(getApplicationContext(), "type:  " + ttInteractionAd.getInteractionType());
 | ||
|                 ttInteractionAd.setAdInteractionListener(new TTInteractionAd.AdInteractionListener() {
 | ||
|                     @Override
 | ||
|                     public void onAdClicked() {
 | ||
|                         Log.d(TAG, "被点击");
 | ||
|                        // TToast.show(mContext, "广告被点击");
 | ||
|                     }
 | ||
| 
 | ||
|                     @Override
 | ||
|                     public void onAdShow() {
 | ||
|                         Log.d(TAG, "被展示");
 | ||
|                        // TToast.show(mContext, "广告被展示");
 | ||
|                     }
 | ||
| 
 | ||
|                     @Override
 | ||
|                     public void onAdDismiss() {
 | ||
|                         Log.d(TAG, "插屏广告消失");
 | ||
|                      //   TToast.show(mContext, "广告消失");
 | ||
|                     }
 | ||
|                 });
 | ||
|                 //如果是下载类型的广告,可以注册下载状态回调监听
 | ||
|                 if (ttInteractionAd.getInteractionType() == TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
 | ||
|                     ttInteractionAd.setDownloadListener(new TTAppDownloadListener() {
 | ||
|                         @Override
 | ||
|                         public void onIdle() {
 | ||
|                             Log.d(TAG, "点击开始下载");
 | ||
|                            // TToast.show(mContext, "点击开始下载");
 | ||
|                         }
 | ||
| 
 | ||
|                         @Override
 | ||
|                         public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
 | ||
|                             Log.d(TAG, "下载中");
 | ||
|                            // TToast.show(mContext, "下载中");
 | ||
|                         }
 | ||
| 
 | ||
|                         @Override
 | ||
|                         public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
 | ||
|                             Log.d(TAG, "下载暂停");
 | ||
|                            // TToast.show(mContext, "下载暂停");
 | ||
|                         }
 | ||
| 
 | ||
|                         @Override
 | ||
|                         public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
 | ||
|                             Log.d(TAG, "下载失败");
 | ||
|                           //  TToast.show(mContext, "下载失败");
 | ||
|                         }
 | ||
| 
 | ||
|                         @Override
 | ||
|                         public void onDownloadFinished(long totalBytes, String fileName, String appName) {
 | ||
|                             Log.d(TAG, "下载完成");
 | ||
|                            // TToast.show(mContext, "下载完成");
 | ||
|                         }
 | ||
| 
 | ||
|                         @Override
 | ||
|                         public void onInstalled(String fileName, String appName) {
 | ||
|                             Log.d(TAG, "安装完成");
 | ||
|                           //  TToast.show(mContext, "安装完成");
 | ||
|                         }
 | ||
|                     });
 | ||
|                 }
 | ||
|                 //弹出插屏广告
 | ||
|                 ttInteractionAd.showInteractionAd(oContext);
 | ||
|             }
 | ||
|         });
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 加载 toutiao feed广告
 | ||
|      */
 | ||
|     public void loadListAd_toutiao(BookListAdapter adapter, int adCnt, boolean addFooter) {
 | ||
|         //feed广告请求类型参数
 | ||
|         AdSlot adSlot = new AdSlot.Builder()
 | ||
|                 .setCodeId( Constants.AD_SETTING.getInformationFlow().getSlotId(Constants.AD_TOUTIAO)     )
 | ||
|                 .setSupportDeepLink(true)
 | ||
|                 .setImageAcceptedSize(228, 150)
 | ||
|                 .setAdCount(adCnt)
 | ||
|                 .build();
 | ||
|         //调用feed广告异步请求接口
 | ||
|           getAdNative().loadFeedAd(adSlot, new TTAdNative.FeedAdListener() {
 | ||
|             @Override
 | ||
|             public void onError(int code, String message) {
 | ||
| 
 | ||
|                 Log.e(TAG, String.format("loadListAd onError: code %s,msg %s",code,message) );
 | ||
| 
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onFeedAdLoad(List<TTFeedAd> ads) {
 | ||
|                 // myAdapter.changeMoreStatus(2);
 | ||
|                 if (ads == null || ads.isEmpty()) {
 | ||
|                     Log.e(TAG, "on FeedAdLoaded: ad is null!" );
 | ||
|                     return;
 | ||
|                 }
 | ||
|                 if(addFooter) {
 | ||
|                     adapter.AddFooterItem(ads);
 | ||
|                 }else{
 | ||
|                     adapter.AddHeaderItem(ads);
 | ||
|                 }
 | ||
| 
 | ||
|             }
 | ||
|         });
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
|     // qq ad ------------------------------begin
 | ||
|     //private AQuery mAQuery;
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|     BannerView bv;
 | ||
|     private void getBanner_qq() {
 | ||
| 
 | ||
|         if(this.bv != null){
 | ||
|           mBannerContainer.removeView(bv);
 | ||
|             bv.destroy();
 | ||
|         }
 | ||
|       if( mBannerContainer.getChildCount()>0){
 | ||
|           mBannerContainer.removeAllViews();
 | ||
|       }
 | ||
| 
 | ||
|         this.bv = new BannerView(this, com.qq.e.ads.banner.ADSize.BANNER,
 | ||
|                 getAppID(Constants.AD_TENCENT_QQ),
 | ||
|                 Constants.AD_SETTING.getChapterBanner().getSlotId(Constants.AD_TENCENT_QQ));
 | ||
|         // 注意:如果开发者的banner不是始终展示在屏幕中的话,请关闭自动刷新,否则将导致曝光率过低。
 | ||
|         // 并且应该自行处理:当banner广告区域出现在屏幕后,再手动loadAD。
 | ||
|       //  bv.setRefresh(0);
 | ||
|         bv.setADListener(new AbstractBannerADListener() {
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onNoAD(AdError error) {
 | ||
|                 Log.i(
 | ||
|                         "AD_DEMO",
 | ||
|                         String.format("Banner onNoAD,eCode = %d, eMsg = %s", error.getErrorCode(),
 | ||
|                                 error.getErrorMsg()));
 | ||
|             }
 | ||
| 
 | ||
|             @Override
 | ||
|             public void onADReceiv() {
 | ||
|                 Log.i("AD_DEMO", "ONBannerReceive");
 | ||
|             }
 | ||
|         });
 | ||
|         mBannerContainer.addView(bv);
 | ||
|         this.bv.loadAD();
 | ||
| 
 | ||
|         mLastBannerTime =new Date().getTime();
 | ||
|     }
 | ||
| 
 | ||
| 
 | ||
|     private void loadNativeBanner_qq(int adWidth,int adHeight) {
 | ||
|         try {
 | ||
| 
 | ||
|       /*      if(mAdList!=null && mAdList.size()>1){
 | ||
|                 nativeExpressADView = mAdList.get(0);
 | ||
|             //    bindAD();
 | ||
|                 Log.d(TAG, "loadNativeBanner_qq: mAdList.size()" +mAdList.size());
 | ||
|                 if(mAdList.size()>1){
 | ||
|                     return;
 | ||
|                 }
 | ||
|             }*/
 | ||
| 
 | ||
| 
 | ||
|             nativeExpressAD = new NativeExpressAD(this, getMyADSize(adWidth, adHeight),
 | ||
|                     getAppID(Constants.AD_TENCENT_QQ),
 | ||
|                     Constants.AD_SETTING.getChapterContentBanner().getSlotId(Constants.AD_TENCENT_QQ), this); // 这里的Context必须为Activity
 | ||
|            /* nativeExpressAD.setVideoOption(new VideoOption.Builder()
 | ||
|                     .setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // 设置什么网络环境下可以自动播放视频
 | ||
|                     .setAutoPlayMuted(true) // 设置自动播放视频时,是否静音
 | ||
|                     .build()); // setVideoOption是可选的,开发者可根据需要选择是否配置
 | ||
|             nativeExpressAD.setMaxVideoDuration(com.novelbook.android.AD.qq.Constants.VIDEO_DURATION_SETTING_MAX);*/
 | ||
|             nativeExpressAD.loadAD(1);
 | ||
| 
 | ||
|         } catch (NumberFormatException e) {
 | ||
|             Log.w(TAG, "ad size invalid.");
 | ||
|             Toast.makeText(this, "请输入合法的宽高数值", Toast.LENGTH_SHORT).show();
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     private ADSize getMyADSize(int adWidth,int adHeight) {
 | ||
| 
 | ||
|         return new ADSize(adWidth, adHeight);
 | ||
|     }
 | ||
| 
 | ||
|     /**
 | ||
|      * 获取广告数据
 | ||
|      *
 | ||
|      * @param nativeExpressADView
 | ||
|      * @return
 | ||
|      */
 | ||
|     private String getAdInfo(NativeExpressADView nativeExpressADView) {
 | ||
|         AdData adData = nativeExpressADView.getBoundData();
 | ||
|         if (adData != null) {
 | ||
|             StringBuilder infoBuilder = new StringBuilder();
 | ||
|             infoBuilder.append("title:").append(adData.getTitle()).append(",")
 | ||
|                     .append("desc:").append(adData.getDesc()).append(",")
 | ||
|                     .append("patternType:").append(adData.getAdPatternType());
 | ||
|             if (adData.getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
 | ||
|                 infoBuilder.append(", video info: ").append(getVideoInfo(adData.getProperty(AdData.VideoPlayer.class)));
 | ||
|             }
 | ||
|             return infoBuilder.toString();
 | ||
|         }
 | ||
|         return null;
 | ||
|     }
 | ||
|     /**
 | ||
|      * 获取播放器实例
 | ||
|      *
 | ||
|      * 仅当视频回调{@link NativeExpressMediaListener#onVideoInit(NativeExpressADView)}调用后才会有返回值
 | ||
|      *
 | ||
|      * @param videoPlayer
 | ||
|      * @return
 | ||
|      */
 | ||
|     private String getVideoInfo(AdData.VideoPlayer videoPlayer) {
 | ||
|         if (videoPlayer != null) {
 | ||
|             StringBuilder videoBuilder = new StringBuilder();
 | ||
|             videoBuilder.append("{state:").append(videoPlayer.getVideoState()).append(",")
 | ||
|                     .append("duration:").append(videoPlayer.getDuration()).append(",")
 | ||
|                     .append("position:").append(videoPlayer.getCurrentPosition()).append("}");
 | ||
|             return videoBuilder.toString();
 | ||
|         }
 | ||
|         return null;
 | ||
|     }
 | ||
| 
 | ||
|     FrameLayout getNativeContainer(){
 | ||
|        return mBannerContainer;
 | ||
|     }
 | ||
|     List<NativeExpressADView> mAdList;
 | ||
|     @Override
 | ||
|     public void onADLoaded(List<NativeExpressADView> adList){
 | ||
|         // 释放前一个展示的NativeExpressADView的资源
 | ||
| 
 | ||
|         if(mAdList==null){
 | ||
|             mAdList = new ArrayList<NativeExpressADView>();
 | ||
|         }
 | ||
|         boolean toBind = mAdList.size()==0;
 | ||
|         mAdList.addAll(0,adList);
 | ||
|         for(NativeExpressADView ad :mAdList){
 | ||
|             Log.d(TAG, "onADLoaded: ad is " +ad);
 | ||
|         }
 | ||
|        /* if(toBind ){
 | ||
|             bindAD(adList);
 | ||
|         }*/
 | ||
|         bindAD(adList);
 | ||
| 
 | ||
| 
 | ||
|     }
 | ||
|     void bindAD(List<NativeExpressADView> adList){
 | ||
|         if (nativeExpressADView != null) {
 | ||
|             nativeExpressADView.destroy();
 | ||
|         }
 | ||
|         FrameLayout bannerContainer = getNativeContainer();
 | ||
| 
 | ||
|         if (bannerContainer .getVisibility() != View.VISIBLE) {
 | ||
|             return;
 | ||
|             //  bannerContainer.setVisibility(View.VISIBLE);
 | ||
|         }
 | ||
| 
 | ||
|         if (bannerContainer.getChildCount() > 0) {
 | ||
|             bannerContainer.removeAllViews();
 | ||
|         }
 | ||
| 
 | ||
|         nativeExpressADView = adList.get(0);
 | ||
| 
 | ||
|      /*   Log.i(TAG, "onADLoaded, video info: " + getAdInfo(nativeExpressADView));
 | ||
|         if (nativeExpressADView.getBoundData().getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
 | ||
|             nativeExpressADView.setMediaListener(mediaListener);
 | ||
|         }*/
 | ||
|         // 广告可见才会产生曝光,否则将无法产生收益。
 | ||
|         bannerContainer.addView(nativeExpressADView);
 | ||
|         nativeExpressADView.render();
 | ||
|      //   mAdList.remove(0);
 | ||
|     }
 | ||
|     @Override
 | ||
|     public void onNoAD(AdError adError) {
 | ||
|         Log.i(
 | ||
|                 TAG,
 | ||
|                 String.format("onNoAD, error code: %d, error msg: %s", adError.getErrorCode(),
 | ||
|                         adError.getErrorMsg()));
 | ||
|     }
 | ||
| 
 | ||
|     @Override
 | ||
|     public void onRenderFail(NativeExpressADView adView) {
 | ||
|         Log.i(TAG, "onRenderFail");
 | ||
|     }
 | ||
| 
 | ||
|     @Override
 | ||
|     public void onRenderSuccess(NativeExpressADView adView) {
 | ||
|         Log.i(TAG, "onRenderSuccess");
 | ||
|     }
 | ||
| 
 | ||
|     @Override
 | ||
|     public void onADExposure(NativeExpressADView adView) {
 | ||
|         Log.i(TAG, "onADExposure");
 | ||
|     }
 | ||
| 
 | ||
|     @Override
 | ||
|     public void onADClicked(NativeExpressADView var1){
 | ||
| 
 | ||
|     }
 | ||
| 
 | ||
|     @Override
 | ||
|     public void onADClosed(NativeExpressADView var1) {
 | ||
|         FrameLayout bannerContainer = getNativeContainer();
 | ||
|         if (bannerContainer != null && bannerContainer.getChildCount() > 0) {
 | ||
|             bannerContainer.removeAllViews();
 | ||
|            // bannerContainer.setVisibility(View.GONE);
 | ||
|         }
 | ||
|     }
 | ||
|     @Override
 | ||
|     public void onADLeftApplication(NativeExpressADView var1){
 | ||
| 
 | ||
|     }
 | ||
| 
 | ||
|     @Override
 | ||
|     public  void onADOpenOverlay(NativeExpressADView var1){
 | ||
| 
 | ||
|     }
 | ||
| 
 | ||
|     @Override
 | ||
|     public void onADCloseOverlay(NativeExpressADView var1){
 | ||
| 
 | ||
|     }
 | ||
|     private NativeExpressMediaListener mediaListener = new NativeExpressMediaListener() {
 | ||
|         @Override
 | ||
|         public void onVideoInit(NativeExpressADView nativeExpressADView) {
 | ||
|             Log.i(TAG, "onVideoInit: "
 | ||
|                     + getVideoInfo(nativeExpressADView.getBoundData().getProperty(AdData.VideoPlayer.class)));
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onVideoLoading(NativeExpressADView nativeExpressADView) {
 | ||
|             Log.i(TAG, "onVideoLoading");
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onVideoReady(NativeExpressADView nativeExpressADView, long l) {
 | ||
|             Log.i(TAG, "onVideoReady");
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onVideoStart(NativeExpressADView nativeExpressADView) {
 | ||
|             Log.i(TAG, "onVideoStart: "
 | ||
|                     + getVideoInfo(nativeExpressADView.getBoundData().getProperty(AdData.VideoPlayer.class)));
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onVideoPause(NativeExpressADView nativeExpressADView) {
 | ||
|             Log.i(TAG, "onVideoPause: "
 | ||
|                     + getVideoInfo(nativeExpressADView.getBoundData().getProperty(AdData.VideoPlayer.class)));
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onVideoComplete(NativeExpressADView nativeExpressADView) {
 | ||
|             Log.i(TAG, "onVideoComplete: "
 | ||
|                     + getVideoInfo(nativeExpressADView.getBoundData().getProperty(AdData.VideoPlayer.class)));
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onVideoError(NativeExpressADView nativeExpressADView, AdError adError) {
 | ||
|             Log.i(TAG, "onVideoError");
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onVideoPageOpen(NativeExpressADView nativeExpressADView) {
 | ||
|             Log.i(TAG, "onVideoPageOpen");
 | ||
|         }
 | ||
| 
 | ||
|         @Override
 | ||
|         public void onVideoPageClose(NativeExpressADView nativeExpressADView) {
 | ||
|             Log.i(TAG, "onVideoPageClose");
 | ||
|         }
 | ||
|     };
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|     @Override
 | ||
|     protected void onResume() {
 | ||
|         super.onResume();
 | ||
|         //  MobclickAgent.onResume(this);
 | ||
|     }
 | ||
|     @Override
 | ||
|     protected void onPause(){
 | ||
|         super.onPause();
 | ||
|         hideProgress();
 | ||
|         //   MobclickAgent.onPause(this);
 | ||
|     }
 | ||
| 
 | ||
| }
 |