用java将bing每日壁纸设置为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 xml
		DocumentBuilderFactory 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 file
		URL 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));
	}
}
	    			




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值