上一篇说了 ios 的开屏广告,这次把 Android 的也发下。逻辑,界面和 ios一致。
需求:广告不是一成不变,要随时更换图片,上下架时间,以及要启动就展示,需要先存到本地,以及点击跳转等问题。
服务端返回的数据格式
adver = (
{
endtime = 1516622424;
id = 7;
link = "https://www.baidu.com";
pic = "/uploads/image/18/01/5a65608db3ebb.png";
}
);
if (data.has("adver") && data.getJSONArray("adver").length() != 0) {
//有广告字段
JSONObject adJsonObj = data.getJSONArray("adver").getJSONObject(0);
Long endtime = adJsonObj.optLong("endtime") * 1000;
String pic = adJsonObj.getString("pic");
String link = adJsonObj.optString("link");
String str_endtime = endtime + "";
//数据有效性判断
if (StringUtils.isEmpty(str_endtime) || StringUtils.isEmpty(pic)) {
return;
}
if (hasAd()) {
//本地有图片
if (adChanged(pic, str_endtime, link)) {
//是新的图片
SaveAdImage(str_endtime, pic, link);
} else {
//do nothing
}
} else {
//本地没图片
SaveAdImage(str_endtime, pic, link);
}
} else {
removeAd();
//没有广告字段
}
private void SaveAdImage(final String end_time, final String _url, final String link)
{
if (isAdPicDownloading) {
return;
}
isAdPicDownloading = true;
Handler handler = new Handler(Looper.getMainLooper());
// final String path = (AppContext.getAppContext().getExternalCacheDir() != null ? AppContext.getAppContext().getExternalCacheDir() : AppContext.getAppContext().getCacheDir()) + "/" + _url.substring(_url.lastIndexOf("/") + 1);
final String path = FileUtils.getSDCardPath() + "/" + AppContext.getAppContext().getPackageName() + "/" + _url.substring(_url.lastIndexOf("/") + 1);
//如果有上次下载失败有临时文件需要删除之后才能正常下载
if (new File(path).exists()) {
PreferenceHelper.write(AppContext.getAppContext(), AppConfig.APP_START, DATA_AD_ENDTIME, end_time);
PreferenceHelper.write(AppContext.getAppContext(), AppConfig.APP_START, DATA_AD_LINK, link);
PreferenceHelper.write(AppContext.getAppContext(), AppConfig.APP_START, DATA_AD_PIC, path);
return;
}
handler.post(new Runnable()
{
@Override
public void run()
{
mKjh.download(path,
URLs.makeOriginImageUrl(_url), new HttpCallBack()
{
@Override
public void onFailure(int errorNo, String strMsg)
{
super.onFailure(errorNo, strMsg);
}
@Override
public void onFinish()
{
isAdPicDownloading = false;
super.onFinish();
}
@Override
public void onSuccess(byte[] t)
{
super.onSuccess(t);
PreferenceHelper.write(AppContext.getAppContext(), AppConfig.APP_START, DATA_AD_ENDTIME, end_time);
PreferenceHelper.write(AppContext.getAppContext(), AppConfig.APP_START, DATA_AD_LINK, link);
if (!path.equals(PreferenceHelper.readString(AppContext.getAppContext(), AppConfig.APP_START, UpdateCommonUnitedTask.DATA_AD_PIC))) {
removeOldAdFile();
PreferenceHelper.write(AppContext.getAppContext(), AppConfig.APP_START, DATA_AD_PIC, path);
}
}
});
}
});
}
private void removeOldAdFile()
{
String oldPath = PreferenceHelper.readString(AppContext.getAppContext(), AppConfig.APP_START, UpdateCommonUnitedTask.DATA_AD_PIC);
if (!StringUtils.isEmpty(oldPath)) {
File oldFile = new File(oldPath);
if (oldFile.exists()) {
oldFile.delete();
}
}
}
activity_start.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_ad_app_start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:scaleType="centerCrop"
android:visibility="gone"></ImageView>
<LinearLayout
android:id="@+id/ll_ad_jump"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="right"
android:layout_marginRight="@dimen/space_15"
android:layout_marginTop="@dimen/space_15"
android:background="@drawable/btn_ad_jump"
android:gravity="center"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/space_10"
android:layout_marginRight="@dimen/space_2"
android:text="跳过"
android:textColor="@color/white"
android:textSize="@dimen/font_size_15"/>
<TextView
android:id="@+id/tv_count_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/space_2"
android:layout_marginRight="@dimen/space_10"
android:text="3"
android:textColor="#ff9900"
android:textSize="@dimen/font_size_15"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_default_app_start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/screen_bg"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="bottom|center">
<ImageView
android:id="@+id/img_start"
android:layout_width="188dp"
android:layout_height="61dp"
android:layout_marginBottom="@dimen/space_30"
android:scaleType="fitXY"
android:src="@drawable/screen"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:layout_marginBottom="@dimen/space_10"
android:layout_weight="1"
android:gravity="bottom|center"
android:text="@string/at365"
android:textColor="@color/screen_text_color"
android:textSize="@dimen/font_size_12"/>
</LinearLayout>
</FrameLayout>
@BindView(id = R.id.iv_ad_app_start)
ImageView ivAdAppStart;
@BindView(id = R.id.ll_default_app_start)
LinearLayout llDefaultAppStart;
@BindView(id = R.id.tv_count_down)
TextView tvCountDown;
@BindView(id = R.id.ll_ad_jump)
LinearLayout llAdJump;
@BindView(id = R.id.img_start)
private ImageView mImageView;
private void showAdAppStartView()
{
llDefaultAppStart.setVisibility(View.GONE);
final String path = PreferenceHelper.readString(AppContext.getAppContext(), AppConfig.APP_START, UpdateCommonUnitedTask.DATA_AD_PIC);
final String link = PreferenceHelper.readString(AppContext.getAppContext(), AppConfig.APP_START, UpdateCommonUnitedTask.DATA_AD_LINK);
AppContext.getAppContext().getKjBitmap().display(ivAdAppStart, path, DensityUtils.getScreenW(AppContext.getAppContext()), DensityUtils.getScreenH(AppContext.getAppContext()), null, null,
new BitmapCallBack()
{
@Override
public void onSuccess(Bitmap bitmap)
{
ivAdAppStart.setVisibility(View.VISIBLE);
llAdJump.setVisibility(View.VISIBLE);
final CountDownTimer countDownTimer = new CountDownTimer(3200, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{
tvCountDown.setText((millisUntilFinished) / 1000 + "");
}
@Override
public void onFinish()
{
tvCountDown.setText("0");
redirectTo();
}
}.start();
llAdJump.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
countDownTimer.cancel();
redirectTo();
}
});
ivAdAppStart.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (!StringUtils.isEmpty(link)) {
countDownTimer.cancel();
browserPath = link;
curMode = luchModeBrowser;
redirectTo();
}
}
});
super.onFinish();
super.onSuccess(bitmap);
}
@Override
public void onFailure(Exception e)
{
removeAd();
startDefaultAppStartView();
super.onFailure(e);
}
@Override
public void onFinish()
{
}
});
// timer.schedule(new TimerTask() {
// @Override
// public void run() {
////
// }
// }, 3 * 1000, 1000);
}
private void removeAd()
{
File file = new File(PreferenceHelper.readString(AppContext.getAppContext(), AppConfig.APP_START, UpdateCommonUnitedTask.DATA_AD_PIC));
if (file.exists()) {
file.delete();
}
PreferenceHelper.write(AppContext.getAppContext(), AppConfig.APP_START, UpdateCommonUnitedTask.DATA_AD_ENDTIME, "");
PreferenceHelper.write(AppContext.getAppContext(), AppConfig.APP_START, UpdateCommonUnitedTask.DATA_AD_PIC, "");
}
private void startDefaultAppStartView()
{
ivAdAppStart.setVisibility(View.GONE);
llDefaultAppStart.setVisibility(View.VISIBLE);
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.splash_start);
// 监听动画过程
animation.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{
}
@Override
public void onAnimationRepeat(Animation animation)
{
}
@Override
public void onAnimationEnd(Animation animation)
{
redirectTo();
}
});
mImageView.setAnimation(animation);
}
private boolean hasAd()
{
return !StringUtils.isEmpty(PreferenceHelper.readString(AppContext.getAppContext(), AppConfig.APP_START, UpdateCommonUnitedTask.DATA_AD_ENDTIME)) && !StringUtils.isEmpty(PreferenceHelper.readString(AppContext.getAppContext(), AppConfig.APP_START, UpdateCommonUnitedTask.DATA_AD_PIC));
}
这段 android 是我之前同事写的,个人感觉写的烂透了,时间容许我会给重构成 demo。先这样吧。。