码迷,mamicode.com
首页 > 移动开发 > 详细

App启动广告

时间:2017-08-08 13:53:23      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:链接   adl   logo   can   java   dbi   owa   index   load   

需求:

App启动的时候获得广告图片链接,如果已经存在,判断是否和本地的相同,不相同才去下载到本地。

流程图:

技术分享

 这些都在广告页的前一个页面操作(logo页或者Application)

import android.content.Intent;
import android.text.TextUtils;

import com.xuehu365.xuehu.R;
import com.xuehu365.xuehu.business.SplashImgDownLoader;
import com.xuehu365.xuehu.data.UserData;
import com.xuehu365.xuehu.model.response.AdResponseEntity;
import com.xuehu365.xuehu.netinterface.AdAPI;
import com.xuehu365.xuehu.netinterface.retrofit.BaseCallBack;

import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Response;

public class LogoActivity extends BaseFragmentActivity {

    @Override
    protected Object getCotentView() {
        return R.layout.activity_logo;
    }

    @Override
    protected void initView() {
        super.initView();
        checkAdUrl();
        jump();
    }

    private void checkAdUrl() {
        AdAPI.getAd(new BaseCallBack<AdResponseEntity>() {
            @Override
            public void onSuccess(Response<AdResponseEntity> response) {
                AdResponseEntity.Data data = response.body().getData();
                if (null == data) {
                    return;
                }
                String url = data.getUrl();
                if (TextUtils.isEmpty(url)) {
                    return;
                }
                String localAd = UserData.getAdBitmap();
                if (!TextUtils.isEmpty(localAd)) {
                    String urlFileName = url.substring(url.lastIndexOf("/") + 1);
                    String localFileName = localAd.substring(localAd.lastIndexOf("/") + 1);
                    if (urlFileName.equals(localFileName)) {
                        return;
                    }
                }
                SplashImgDownLoader.downLoad(url);
            }
        });
    }

    private void jump() {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (null != timer) {
                    timer.cancel();
                    startActivity(new Intent(LogoActivity.this, SplashActivity.class));
                    finish();
                }
            }
        }, 1000);
    }
}

 

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.xuehu365.xuehu.data.UserData;
import com.xuehu365.xuehu.utils.PathUtil;

import java.io.File;

/**
 * Created by Administrator on 2017/8/8.
 */
public class SplashImgDownLoader {
    public static void downLoad(String url) {
        String fileName = url.substring(url.lastIndexOf("/") + 1);
        String filePath = PathUtil.getInstance().getImagePath() + File.separator + fileName;
        BaseDownloadTask task = FileDownloader.getImpl().create(url).setPath(filePath).setListener(new FileDownloadListener() {
            @Override
            protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {

            }

            @Override
            protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {

            }

            @Override
            protected void completed(BaseDownloadTask task) {
                UserData.saveAdBitmap(task.getPath());
            }

            @Override
            protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

            }

            @Override
            protected void error(BaseDownloadTask task, Throwable e) {

            }

            @Override
            protected void warn(BaseDownloadTask task) {

            }
        });
        task.start();
    }
}

 

然后,广告页只需要去本地拿地址然后加载就可以了

App启动广告

标签:链接   adl   logo   can   java   dbi   owa   index   load   

原文地址:http://www.cnblogs.com/anni-qianqian/p/7306024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!