Android App每日更換壁紙

  1. App的效果

    這裏寫圖片描述

    首先App中的圖片就是微軟Bing搜索中的壁紙,下一節中我會回答怎麼通過獲取Bing壁紙。接下來你打開http://cn.bing.com/,來驗證一下顯示的圖片是不是和下圖是一樣的。

這裏寫圖片描述

  1. 獲取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));
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章