標籤:bing 每日壁紙 設定壁紙 java win7
原文:用java將bing每日壁紙設定為win7壁紙
原始碼:http://www.zuidaima.com/share/1550463716592640.htm
之前發表了“用java將bing每日壁紙設定為win7壁紙”,看了評論後就決定將這個想法在win7上實現。
其實在win7上的實現和在ubuntu上的實現並沒有很大的區別,前面解析xml和下載圖片是一樣的,區別是後面設定壁紙。
win7下設定壁紙的代碼有點麻煩,沒有linux直接調用命令那樣好用。後來在設定開機啟動項的時候發現,運行這個jar檔案的順序會先於啟動網路服務,於是我用一個do...while來解決這個問題(具體見main函數的代碼)。
編譯這個程式需要用到jna.jar包和jna-platform.jar包。
建議將其編譯打包成可執行jar檔案,然後點擊開始菜單----“所有程式”-----“啟動”右鍵選“開啟”,將jar檔案拖入到開啟的檔案夾,這樣每天開機的時候壁紙就能自動換了~~
package com.zuidaima;import java.io.IOException;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.xml.sax.SAXException;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URL;import java.util.HashMap;import com.sun.jna.Native;import com.sun.jna.platform.win32.WinDef.UINT_PTR;import com.sun.jna.win32.*;/***@author www.zuidaima.com**/public class MyWallpaper {public static void main(String[] argc) throws ParserConfigurationException, SAXException, IOException{MyWallpaper wallpaper = new MyWallpaper();do{String path = wallpaper.getThePath();wallpaper.downLoadWallpaper(path);wallpaper.settingWallpaper();}while(wallpaper.isConnect()!=true);}public interface SPI extends StdCallLibrary { long SPI_SETDESKWALLPAPER = 20; long SPIF_UPDATEINIFILE = 0x01; long SPIF_SENDWININICHANGE = 0x02; SPI INSTANCE = (SPI) Native.loadLibrary("user32", SPI.class, new HashMap<Object, Object>() { { put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE); put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE); } }); boolean SystemParametersInfo( UINT_PTR uiAction, UINT_PTR uiParam, String pvParam, UINT_PTR fWinIni ); }public boolean isConnect() throws IOException{Runtime runtime = Runtime.getRuntime();Process process = runtime.exec("ping www.baidu.com");InputStream is = process.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);if(br.readLine()==null){//System.out.println("The network is wrong!");return false;}else{//System.out.println("The network is well");return true;}}public String getThePath() throws ParserConfigurationException, SAXException, IOException{//getting the path of the bing jpg picture via analysis xmlDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse("http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=8");document.normalize(); String relativePath =document.getElementsByTagName("url").item(0).getTextContent();String path ="http://www.bing.com/"+relativePath;return path;}public void downLoadWallpaper(String path) throws IOException{//download the jpg fileURL url = new URL(path);DataInputStream dis = new DataInputStream(url.openStream());FileOutputStream fos = new FileOutputStream(new File("C:\\wallpaper.jpg"));byte[] buffer = new byte[1024];int length;while((length=dis.read(buffer))>0){fos.write(buffer,0,length);}dis.close();fos.close();}public void settingWallpaper(){ String localpath = "C:\\wallpaper.jpg"; SPI.INSTANCE.SystemParametersInfo( new UINT_PTR(SPI.SPI_SETDESKWALLPAPER), new UINT_PTR(0), localpath, new UINT_PTR(SPI.SPIF_UPDATEINIFILE | SPI.SPIF_SENDWININICHANGE));}}
用java將bing每日壁紙設定為win7壁紙