[autojs]安卓自动转发短信到微信
本帖最后由 Piz.liu 于 2022-4-12 11:06 编辑在公司专门放了个手机用来钉钉打卡,但是手机里有卡,短信没办法实时转发给我,
所以用autojs写了个脚本,通过企业微信实时转发到我的微信里面, 需要的可以自取
昨天试了把未读短信设置为已读 一直不成功,找了很多方法也没用 (有知道的大佬希望告知一下),所以临时设置了个ids 存放已经发送过的短信id ,避免重复发送
个别手机可能会报错应该是权限的异常,应该是没有授权读取短信的权限导致的,个别手机上没有弹出授权的提醒,
也不知道为什么,有知道的大神解释一下,不能换个手机试试,如果换手机可以,打个包,就行了
或者可以用24楼 rplyl1988朋友提供的 监听通知栏的方式获取信息试试
代码如下:
"ui";
ui.layout(
<vertical>
<horizontal>
<button id="开启短信监听" layout_width="0dp" layout_weight="1">
开启短信监听
</button>
<button id="关闭短信监听" layout_width="0dp" layout_weight="1">
关闭短信监听
</button>
</horizontal>
<button id="退出">退出</button>
<text id="messageToDisplay"></text>
</vertical>
);
importClass(android.net.Uri);
importClass(android.database.Cursor);
importClass(android.content.ContentResolver);
importClass(android.database.ContentObserver);
//定一个数组 保存已经发送的短信id 避免重复发送
var ids = []
var SMS_INBOX = Uri.parse("content://sms/inbox");
var cr = context.getApplicationContext().getContentResolver();
// type ALL = 0;
// INBOX = 1;
// SENT = 2;
// DRAFT = 3;
// OUTBOX = 4;
// FAILED = 5;
// QUEUED = 6;
var sendurl = ""
function getMsg() {
var cur = cr.query(
SMS_INBOX,
null,
"type = 1 and read = 0",
null,
"date desc"
);
log("未读短信数量:" + cur.getCount());
while (cur.moveToNext()) {
var id = cur.getString(cur.getColumnIndex("_id"));
var address = cur.getString(cur.getColumnIndex("address")); //发送人
var date = cur.getLong(cur.getColumnIndex("date")); //短信日期
var body = cur.getString(cur.getColumnIndex("body")); //短信内容
var date_str = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
new Date(date)
);
var a = ids.indexOf(id);
if (a == -1) {
log("发件人:" + address);
log("时间:" + date_str);
log("内容:" + body);
sendWeiChat(address, date_str, body);
ids.push(id)
}
}
}
function createSmsObserver() {
log("创建监听");
let smsObserver = new JavaAdapter(
ContentObserver,
{
onChange: (self, uri) =>{
if (!uri.toString().equals(SMS_INBOX)) {
if (!uri.toString().equals(sendurl)) {
log("监听事件函数 onChangeFunction");
sendurl = uri.toString();
getMsg();
}
}
}
},
new android.os.Handler()
);
return smsObserver;
}
function sendWeiChat(address, date_str, body) {
let send_data =
"发件人:" +
address +
"\n" +
"发送时间:" +
date_str +
"\n" +
"短信内容:" +
"\n" +
body;
var corpid = "xxxx"; //企业id自己改
var corpsecret = "xxx";//应用secret自己改
var HEADERS = { "Content-Type": "application/json ;charset=utf-8" };
var r = http.get(
"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" +
corpid +
"&corpsecret=" +
corpsecret,
{
headers: HEADERS,
}
);
var js = r.body.json();
var token = js.access_token;
var data = {
touser: "@all",
msgtype: "text",
agentid: 1000004, //应用id自己改
text: { content: send_data },
safe: 0,
enable_id_trans: 0,
enable_duplicate_check: 0,
duplicate_check_interval: 1800,
};
var wechaturl =
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token;
http.postJson(wechaturl, data, { headers: HEADERS });
}
ui.开启短信监听.click(function () {
//获取权限
runtime.requestPermissions(["read_sms"]);
toastLog("开启短信监听");
ui.messageToDisplay.setText("开启短信监听");
smsObserver = createSmsObserver();
context
.getContentResolver()
.registerContentObserver(SMS_INBOX, true, smsObserver);
});
ui.关闭短信监听.click(function () {
toastLog("关闭短信监听");
ui.messageToDisplay.setText("关闭短信监听");
if (smsObserver) {
context.getContentResolver().unregisterContentObserver(smsObserver);
}
});
ui.退出.click(function () {
toastLog("退出");
ids = []
if (smsObserver) {
context.getContentResolver().unregisterContentObserver(smsObserver);
}
engines.myEngine().forceStop();
});
events.on("exit", function () {
log("退出事件");
if (smsObserver) {
log("取消监听");
context.getContentResolver().unregisterContentObserver(smsObserver);
}
});
小米10S 双卡 运行之后 提示错误
Version: 461
Android: 30
java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=22861, uid=10274 requires android.permission.READ_SMS, or grantUriPermission()
org.mozilla.javascript.WrappedException:Wrapped java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=22861, uid=10274 requires android.permission.READ_SMS, or grantUriPermission() (/storage/emulated/0/脚本/短信发微信.js#43)
org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1914)
......
Caused by:
java.lang.SecurityException:Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=22861, uid=10274 requires android.permission.READ_SMS, or grantUriPermission()
android.os.Parcel.createExceptionOrNull(Parcel.java:2376)
android.os.Parcel.createException(Parcel.java:2360)
android.os.Parcel.readException(Parcel.java:2343)
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
android.content.ContentProviderProxy.query(ContentProviderNative.java:472)
android.content.ContentResolver.query(ContentResolver.java:1197)
android.content.ContentResolver.query(ContentResolver.java:1128)
android.content.ContentResolver.query(ContentResolver.java:1084)
java.lang.reflect.Method.invoke(Native Method)
org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1479)
org.mozilla.javascript.Interpreter.interpret(Interpreter.java:815)
org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:399)
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3452)
org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:107)
org.mozilla.javascript.ArrowFunction.call(ArrowFunction.java:43)
org.mozilla.javascript.JavaAdapter.doCall(JavaAdapter.java:606)
org.mozilla.javascript.JavaAdapter.callMethod(JavaAdapter.java:581)
adapter1.onChange(Unknown Source:36)
android.database.ContentObserver.onChange(ContentObserver.java:200)
android.database.ContentObserver.lambda$dispatchChange$0$ContentObserver(ContentObserver.java:282)
android.database.-$$Lambda$ContentObserver$MgqiYb2qvgLhoXTioYXq9MvvpNk.run(Unknown Source:10)
android.os.Handler.handleCallback(Handler.java:938)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:236)
android.app.ActivityThread.main(ActivityThread.java:8056)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 真好,比绿豆转发apk好多了,那个死贵 有搞头 冲冲冲 学习了,这个可以给娃整一个,方便。 不错,准备研究一下怎么同时发到邮箱 应用哪里是少一个符号吗 很实用,以前都是用Tasker的,现在这个更方便! 好东西,等待大佬完善 关注一下,6666 dingqh 发表于 2022-4-11 14:02
应用哪里是少一个符号吗
应用secret 少了个双引号{:1_893:}