需求:
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(); } }
然后,广告页只需要去本地拿地址然后加载就可以了
时间: 2024-10-08 15:04:24