Android实现欢迎页面换广告

思路:

        1.StartActivity,在setcontentview之间,检查缓存下是否有最新的广告,且广告符合,应该放置的时间。

        2.MainActivity,检查是否需要下载欢迎图片。


源码:

 /**
     * 检查是否需要换图片
     * @param view
     */
    private void check(LinearLayout view) {
    	//查看/cache/welcokeback/目录下是否有文件
    	<strong><span style="color:#ff0000;">String path = FileUtils.getAppCache(this, "welcomeback");</span></strong>
    	//得到此目录下所有文件
    	<span style="color:#ff0000;"><strong>List<File> files = FileUtils.listPathFiles(path);</strong></span>
    	if (!files.isEmpty()) {
    		//得到第一个文件
    		File f = files.get(0);
    		//分析图片广告要显示的时间
<span style="color:#ff0000;"><strong>    		long time[] = getTime(f.getName());
    		long today = StringUtils.getToday();</strong></span>
    		//对是否显示做判断
    		if (today >= time[0] && today <= time[1]) {
    			view.setBackgroundDrawable(Drawable.createFromPath(f.getAbsolutePath()));
    		}
    	}
    }



	/**
	 * 获取应用程序缓存文件夹下的指定目录
	 * @param context
	 * @param dir
	 * @return
	 */
	public static String getAppCache(Context context, String dir) {
		String savePath = context.getCacheDir().getAbsolutePath() + "/" + dir + "/";
		File savedir = new File(savePath);
		if (!savedir.exists()) {
			savedir.mkdirs();
		}
		savedir = null;
		return savePath;
	}



	/**
	 * 获取一个文件夹下的所有文件
	 * @param root
	 * @return
	 */
	public static List<File> listPathFiles(String root) {
		List<File> allDir = new ArrayList<File>();
		SecurityManager checker = new SecurityManager();
		File path = new File(root);
		checker.checkRead(root);
		File[] files = path.listFiles();
		for (File f : files) {
			if (f.isFile())
				allDir.add(f);
			else 
				<strong><span style="color:#ff0000;">listPath(f.getAbsolutePath());</span></strong>
		}
		return allDir;
	}


	/**
	 * 列出root目录下所有子目录
	 * 
	 * @param path
	 * @return 绝对路径
	 */
	public static List<String> listPath(String root) {
		List<String> allDir = new ArrayList<String>();
		SecurityManager checker = new SecurityManager();
		File path = new File(root);
		checker.checkRead(root);
		// 过滤掉以.开始的文件夹
		if (path.isDirectory()) {
			for (File f : path.listFiles()) {
				if (f.isDirectory() && !f.getName().startsWith(".")) {
					allDir.add(f.getAbsolutePath());
				}
			}
		}
		return allDir;
	}



    
    /**
     * 分析显示的时间
     * @param time
     * @return
     */
    private long[] getTime(String time) {
    	long res[] = new long[2];
    	try {
    		time = time.substring(0, time.indexOf("."));
        	String t[] = time.split("-");
        	res[0] = Long.parseLong(t[0]);
        	if (t.length >= 2) {
        		res[1] = Long.parseLong(t[1]);
        	} else {
        		res[1] = Long.parseLong(t[0]);
        	}
		} catch (Exception e) {
		}
    	return res;
    }



	/**
	 * 返回long类型的今天的日期
	 * 
	 * @return
	 */
	public static long getToday() {
		Calendar cal = Calendar.getInstance();
		String curDate = dateFormater2.get().format(cal.getTime());
		curDate = curDate.replace("-", "");
		return Long.parseLong(curDate);
	}



检查是否有更新。。。。



	private void checkBackGround() {
		if (!appContext.isNetworkConnected()) {
			return;
		}
		// 启动线程去检查服务器接口是否需要下载新的欢迎界面背景图片到手机
		new Thread(){
			public void run() {
				// 将图片下载下来
				try {
					<strong><span style="color:#ff0000;">ApiClient.checkBackGround(appContext);</span></strong>
				} catch (AppException e) {
				}
			}
		}.start();
	}



	
	/**
	 * 检测网络是否可用
	 * @return
	 */
	public boolean isNetworkConnected() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		return ni != null && ni.isConnectedOrConnecting();
	




/**
	 * 检查是否有可下载的欢迎界面图片
	 * @param appContext
	 * @return
	 * @throws AppException
	 */
	public static void checkBackGround(AppContext appContext) throws AppException {
		try{
			WellcomeImage update = WellcomeImage.parse(http_get(appContext, URLs.UPDATE_VERSION));
			String filePath = FileUtils.getAppCache(appContext, "welcomeback");
			// 如果没有图片的链接地址则返回
			if(StringUtils.isEmpty(update.getDownloadUrl())) {
				return;
			}
			if(update.isUpdate()) {
				String url = update.getDownloadUrl();
				String fileName = update.getStartDate().replace("-", "") + "-" + update.getEndDate().replace("-", "");
				List<File> files = FileUtils.listPathFiles(filePath);
				if (!files.isEmpty()) {
					if(files.get(0).getName().equalsIgnoreCase(fileName)) {
						return;
					}
				}
				Bitmap photo = getNetBitmap(url);
				ImageUtils.saveImageToSD(appContext,
						filePath + fileName + ".png", photo, 100);
			} else {
				FileUtils.clearFileWithPath(filePath);
			}
		}catch(Exception e){
			if(e instanceof AppException)
				throw (AppException)e;
			throw AppException.network(e);
		}
	}


	/**
	 * 清空一个文件夹
	 * @param files
	 */
	public static void clearFileWithPath(String filePath) {
		List<File> files = FileUtils.listPathFiles(filePath);
		if (files.isEmpty()) {
			return;
		}
		for (File f : files) {
			if (f.isDirectory()) {
				clearFileWithPath(f.getAbsolutePath());
			} else {
				f.delete();
			}
		}
	}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值