赞
踩
此程序在手再也不怕女朋友跟你闹了!!!!自从有了女朋友比如:早安、晚安之类的问候语可不能断,但是也难免有时候会忘记那么该咋么办呢?很简单写一个程序么,近日闲来无趣想着用Java写一个自动发送微信的小程序,实现定时给指定的好友发送指定的消息,这不就很Nice了?本文主要包括实现的思路、代码的实现、打包为jar快捷方式!
原理其实很简单用Java模拟快捷键发送消息:
import org.springframework.util.StringUtils;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;
import java.util.*;
/**
* @Author MT
* @Date 2021/11/22 14:45
* @Description: 发送微信消息
*/
public class SendMsg {
//执行次数
private static int count = 0;
public static void main(String[] args) {
System.out.println("开始执行----------");
Scanner scanner = new Scanner(System.in);
System.out.println("请设置需发送的好友昵称:");
String friendNickName = scanner.next();
System.out.println("请设置需发送的内容:");
String content = scanner.next();
System.out.println("请输入发送时间的小时:");
Integer hour = scanner.nextInt();
System.out.println("请输入发送时间的分钟:");
Integer minute = scanner.nextInt();
System.out.println("请输入发送时间的秒:");
Integer second = scanner.nextInt();
System.out.println("您设定的发送时间为" + hour + "时" + minute + "分" + second + "秒!!!");
//执行时间
Date date = getDate(hour, minute, second);
//Timer定时器
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
searchPerson(friendNickName, content);
count++;
System.out.println("共发送了" + count + "天!!!");
}
};
//延迟date毫秒执行一次发送方法
timer.schedule(task, date);
}
//获取执行时间
public static Date getDate(Integer hour, Integer minute, Integer second) {
//设置执行时间
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);//每天
calendar.set(year, month, day, hour, minute, second);
Date date = calendar.getTime();
//如果启动时间超过执行时间,则明天执行
if (date.before(new Date())) {
date = addDay(date, 1);
}
return date;
}
// 增加或减少天数
public static Date addDay(Date date, int num) {
Calendar startDT = Calendar.getInstance();
startDT.setTime(date);
startDT.add(Calendar.DAY_OF_MONTH, num);
return startDT.getTime();
}
private static void searchPerson(String friendNickName, String content) {
// 创建Robot对象
Robot robot = getRobot();
//打开微信 Ctrl+Alt+W
assert robot != null;
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_W);
//释放Ctrl按键,像Ctrl,退格键,删除键这样的功能性按键,在按下后一定要释放
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_ALT);
// 该延迟不能少,否则无法搜索
robot.delay(1000);
// Ctrl + F 搜索指定好友
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_F);
robot.keyRelease(KeyEvent.VK_CONTROL);
// 将好友昵称发送到剪切板
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable tText = new StringSelection(friendNickName);
clip.setContents(tText, null);
// 以下两行按下了ctrl+v,完成粘贴功能
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(1000);
// 发送消息
try {
sendMsg(content);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void sendMsg(String content) throws InterruptedException {
if (!content.equals("发送默认的信息")) {
sendOneMsg(content);
} else {
String[] msgArr = {
"能不能发",
"能不能",
"能",
"鸹貔",
"[Emm]",
""
};
ArrayList<String> msgList = new ArrayList<>();
int i = 1;
while (i <= 10) {
msgList.add("能不能发!");
i++;
}
for (String item : msgList) {
sendOneMsg(item);
}
Thread.sleep(2000);
sendOneMsg("就问你能不能发!");
sendOneMsg("[坏笑]");
sendOneMsg("鸹貔");
}
}
private static Robot getRobot() {
// 创建Robot对象
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
return robot;
}
private static void sendOneMsg(String msg) {
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable tText;
//创建Robot对象
Robot robot = getRobot();
//延迟十秒,主要是为了预留出打开窗口的时间,括号内的单位为毫秒
if (null != robot) {
robot.delay(500);
tText = new StringSelection(msg);
clip.setContents(tText, null);
// 按下了ctrl+v,完成粘贴功能
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(500);
//回车发送消息
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(500);
}
}
}
我这里用的是IDEA进行打包,也可以输入命令
进入相应的目录运行jar(见3.6),之后就可以在规定的时间发送了;
命令:java -jar temp.jar
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。