您的位置:首页 > 移动开发

Android App每日更换壁纸

2015-06-05 21:53 309 查看
App的效果



首先App中的图片就是微软Bing搜索中的壁纸,下一节中我会回答怎么通过获取Bing壁纸。接下来你打开http://cn.bing.com/,来验证一下显示的图片是不是和下图是一样的。



获取Bing背景图片的链接

我们查看页面的源代码,可以发现类似下图的代码



我们把地址提取出来 http://s.cn.bing.net/az/hprichbg/rb/FlorenceView_ZH-CN14082192121_1920x1080.jpg,然后我们打开可以看到如下图



这是今天,也就是 2015年7月30号 显示的图片,当你看到这篇博文的时候肯定不是显示这章图片,可能上面的链接也打不开了。我们从链接知道 图片是1920x1080的,这在宽屏显示器上还行,可我们的手机是竖屏的,这可怎么办呢?

用过Winphone的朋友可能都知道,屏幕锁屏界面就是Bing图片,而Winphone就是竖屏的啊,况且笔者手中也有一个lumia手机。于是我碰运气的试了试,把上述链接的尺寸改变了一下,奇迹就发生了。

http://s.cn.bing.net/az/hprichbg/rb/FlorenceView_ZH-CN14082192121_720x1280.jpg

720x1280是目前主流手机的分辨率吧,看下图知道了。



接下来,我们只需要在App中对Bing发出请求,然后用正则分析出原始图片的地址,再把分辨率换成我们想要的就行了。这一切用正则就行了,代码如下:

String string = new String(responseBody, "UTF-8");
String match = "";
String reg = "http:\\/\\/s\\.cn\\.bing\\.net.{10,100}\\.jpg";
Matcher m = Pattern.compile(reg).matcher(string);
while (m.find()) {
match = m.group(0);
}
match = match.replace("1920", "720");
match = match.replace("1080", "1280");


3.App每天更换背景的逻辑

当用户启动App的时候,判断是否已经获取了今日的壁纸,如果有,那么设置壁纸为应用背景,否则,获取今日的壁纸,并且设置为今日壁纸

逻辑看起来很简单,分析起来其实挺复杂的,要考虑到一下几个问题:

1:当用户启动App时,手机没有联网

2:如何判断用户已经获取了壁纸

我的处理方法是:

onCreate():设置一个间隔为一分钟的计时器,当没有获取到壁纸,则一直运行下去,获取到壁纸,则取消计时器,并且向sharePreferences写入今日的日期。 然后判断是否已经获取今日壁纸是通过比对用户手机中的日期与sharePreferences中的日期是否一致进行的。

在onCreate()和onResume()中更新壁纸

还有诸多的细节问题,就看源代码吧

核心代码:

//onCreate()
loadBingPic();
timer.scheduleAtFixedRate(new getBingPic(), 5 * 1000, 60 * 1000);

//Class getBingPic
public class getBingPic extends TimerTask {
public void run() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
Date curDate = new Date(System.currentTimeMillis());
String today = formatter.format(curDate);
if (today.equals(sharedPreferences.getString(TODAY, ""))) {
timer.cancel();
return;
}
HttpRequest.get(bingUrl, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody)
throws UnsupportedEncodingException {
String string = new String(responseBody, "UTF-8"); String match = ""; String reg = "http:\\/\\/s\\.cn\\.bing\\.net.{10,100}\\.jpg"; Matcher m = Pattern.compile(reg).matcher(string); while (m.find()) { match = m.group(0); } match = match.replace("1920", "720"); match = match.replace("1080", "1280");
HttpRequest.get(match, new AsyncHttpResponseHandler() {
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody)
throws UnsupportedEncodingException {
String state = Environment
.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File sdcardDir = Environment
.getExternalStorageDirectory();
String path = sdcardDir + "/" + mkDir;
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyyMMdd");
Date curDate = new Date(System
.currentTimeMillis());
String today = formatter.format(curDate);
sharedPreferences.edit()
.putString(TODAY, today).commit();
File photo = new File(path, "bing.jpg");
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos = new FileOutputStream(
photo.getPath());
fos.write(responseBody);
timer.cancel();
Message message = new Message();
message.what = 1;
mHandler.sendMessage(message);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error)
throws UnsupportedEncodingException {
}
});
}

public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error)
throws UnsupportedEncodingException {
}
});
}
}

//loadBingPic()
public void loadBingPic() {
System.out.println("loadBingpic()");
String str = "";
if (!sharedPreferences.getString(TODAY, "").equals(str)) {
String photo = Environment.getExternalStorageDirectory().getPath()
+ "/" + mkDir + "/bing.jpg"; mainUI.setBackgroundDrawable(Drawable.createFromPath(photo));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息