最近做了一个欢迎页的广告功能,分析一下需求,主要就是在程序启动是,需要展示一个广告暂定时间为三秒。
分析一下遇到的问题,首先广告图片是从服务器获取的,在请求服务器成功之后,才能出现这个广告页。处理广告的逻辑写在连接网络之后会出现一个等该时间,这个时间屏幕是白色的。其次,就是这个imageLoader处理图片会有一个不同步的情况,这个比较麻烦,第三个就是倒计时的问题了。
首先,第一个问题,解决是用到缓存,用sp,先保存拿到的服务器传过来的信息。因为程序第一次安装启动是不需要广告页,是四张图片的viewpager,这个时候正好村下载,下次启动再使用。
请求成功之后保存
HttpInterfaceListener getSplashAdListener = new HttpInterfaceListener() {
@Override
public void onResult(BeanParent bean) {
// TODO Auto-generated method stub
SplashAd splashAd = (SplashAd) bean;
int err_code = 0;
if (splashAd.isErr()) {
err_code = splashAd.getErr_code();
if (err_code == BeanParent.CODE_TIMEOUT || err_code == AppConfig.CODE_TIMEOUT_ONSER) {
ToastUtil.showToast(getApplicationContext(), BeanParent.MSG_TIMEOUT, Toast.LENGTH_SHORT);
}
} else {
mBid = splashAd.getBid();
mImageUrl = splashAd.getUrl();
mPic = splashAd.getPic();
mTime = splashAd.getTime();
SharedPreferences settings = getSharedPreferences("config", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("newBid", mBid);
editor.putString("newImageUrl", mImageUrl);
editor.putString("newPic", mPic);
editor.putString("newTime", mTime);
editor.commit();
}
}
};
在程序入口判断网络,并请求网络
//版本号检测
if (GeneralUtil.getLatestEnter(this).equals(
AppConstant.getVersion(this))) {
//获取本地广告页面信息
SharedPreferences sp = getSharedPreferences("config", Activity.MODE_PRIVATE);
saveBid = sp.getString("newBid", "");
saveImageUrl = sp.getString("newImageUrl", "");
savePic = sp.getString("newPic", "");
saveTime = sp.getString("newTime", "3");
//如果有网络、本地广告信息不为空则显示广告页面
if (Xs8_Application.isHaveInternet()) {
update();
new HttpInterfaceTask(this, getSplashAdListener).execute(HttpInterface.TASK_SPLASH_GET_AD);
} else {
//启动跳转至书架
setContentView(R.layout.xs8_news_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(
Xs8_News_SplashActivity.this,
Xs8_News_Bookself.class);
if (mAlipay.isOk()) {
GlobalValues.ISALIPAY = true;
mAlipay.writeToIntent(intent);
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Xs8_News_SplashActivity.this.finish();
ActivityAnimation
.defaultAnimation(Xs8_News_SplashActivity.this);
}
}, 1200);
}
} else {
if (Xs8_Application.isHaveInternet())
new HttpInterfaceTask(this, getSplashAdListener).execute(HttpInterface.TASK_SPLASH_GET_AD);
initFirstEnterView();
}
有网络走update()方法,开始时间循环,并处理广告操作,广告可以点击也可以跳过。
public void update() {
if (!TextUtils.isEmpty(saveTime) && TextUtils.isDigitsOnly(saveTime)) {
int i = Integer.valueOf(saveTime).intValue();
recLen = i + 1;
}
setContentView(R.layout.xs8_news_splash);
mWelImage = (ImageView) findViewById(R.id.activity_main);
mDefaultImage = (ImageView) findViewById(R.id.activity_main_image);
mTimeCount = (TextView) findViewById(R.id.time_text);
mButton = (Button) findViewById(R.id.skip);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gotoBookShelf();
}
});
if (!TextUtils.isEmpty(savePic)) {
mWelImage.setVisibility(View.VISIBLE);
mDefaultImage.setVisibility(View.GONE);
ImageLoader.getInstance().displayImage(savePic, mWelImage,
GlobalValues.welspash);
mTimeCount.setVisibility(View.VISIBLE);
mButton.setVisibility(View.VISIBLE);
mWelImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!TextUtils.isEmpty(saveBid)) {
Intent intent = new Intent(Xs8_News_SplashActivity.this,
Xs8_News_Corver.class);
intent.putExtra(Xs8_News_BaseActivity.INTTENT_BID,
saveBid);
intent.putExtra(Xs8_News_BaseActivity.INTENT, SplashTag);
startActivity(intent);
if(timer!=null)
timer.cancel();
ActivityAnimation
.animation_2in(Xs8_News_SplashActivity.this);
Xs8_News_SplashActivity.this.finish();
} else if (TextUtils.isEmpty(saveBid)&& !TextUtils.isEmpty(saveImageUrl)) {
Intent intent1 = new Intent(Xs8_News_SplashActivity.this,
Xs8_News_Bookself.class);
startActivity(intent1);
Uri uri = Uri.parse(saveImageUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
if(timer!=null)
timer.cancel();
ActivityAnimation
.animation_2in(Xs8_News_SplashActivity.this);
Xs8_News_SplashActivity.this.finish();
} else if (TextUtils.isEmpty(mBid)&& TextUtils.isEmpty(saveImageUrl)) {
gotoBookShelf();
}
}
});
mDefaultImage.setVisibility(View.GONE);
timer.schedule(task, 0, 1000);
} else {
setContentView(R.layout.xs8_news_splash);
mDefaultImage.setVisibility(View.VISIBLE);
mWelImage.setVisibility(View.GONE);
gotoBookShelf();
}
最后就是时间Timer倒计时了,还有倒计时开始的时候跳转后记得timer.cancel.结束倒计时
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() { // UI thread
@Override
public void run() {
recLen--;
if (recLen > 0) {
mTimeCount.setText("" + recLen);
}else{
if (timer!=null)
timer.cancel();
Intent intent = new Intent();
intent.setClass(Xs8_News_SplashActivity.this,
Xs8_News_Bookself.class);
if (mAlipay.isOk()) {
GlobalValues.ISALIPAY = true;
mAlipay.writeToIntent(intent);
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Xs8_News_SplashActivity.this.finish();
ActivityAnimation
.defaultAnimation(Xs8_News_SplashActivity.this);
}
}
});
}
};