用java将bing每日壁纸设置为win7壁纸

原文:

源代码下载地址:

之前发表了“用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 **/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 ");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("?format=xml&idx=0&n=8");document.normalize(); String relativePath =document.getElementsByTagName("url").item(0).getTextContent();String path =""+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壁纸

相关文章:

你感兴趣的文章:

标签云: