用java將bing每日壁紙設定為win7壁紙

來源:互聯網
上載者:User

標籤: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壁紙

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.