Tasker-安卓手机电量自动告警

tasker监控手机电量,达到设定的值(高于或低于),发出告警(企业微信)信息

1.任务

直接在task的配置文件tab中导入即可

效果:

当电量低于20%,会发送消息给微信,告知电量低,频率为每小时1次

当电量充满了,会发送消息给微信,告知充电已完成,频率为每小时1次

1.1 电量低告警

电量低.prf.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<TaskerData sr="" dvi="1" tv="5.9.1">
<Profile sr="prof9" ve="2">
<cdate>1577798812802</cdate>
<clp>true</clp>
<edate>1584810428753</edate>
<flags>8</flags>
<id>9</id>
<limit>true</limit>
<mid0>15</mid0>
<nme>电量低</nme>
<State sr="con0" ve="2">
<code>140</code>
<Int sr="arg0" val="0"/>
<Int sr="arg1" val="20"/>
</State>
<Time sr="con1">
<fh>-1</fh>
<fm>-1</fm>
<rep>1</rep>
<repval>1</repval>
<th>23</th>
<tm>59</tm>
</Time>
</Profile>
<Task sr="task15">
<cdate>1577803544370</cdate>
<edate>1645924392304</edate>
<id>15</id>
<nme>电量告警-微信</nme>
<pri>100</pri>
<Action sr="act0" ve="7">
<code>131</code>
<Str sr="arg0" ve="3">Tasker/JavaScript/Butter2WeChat.js</Str>
<Str sr="arg1" ve="3"/>
<Int sr="arg2" val="1"/>
<Int sr="arg3" val="45"/>
</Action>
</Task>
</TaskerData>

1.2充电已完成.prf.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<TaskerData sr="" dvi="1" tv="5.9.1">
<Profile sr="prof10" ve="2">
<cdate>1577798840202</cdate>
<clp>true</clp>
<edate>1578071190537</edate>
<flags>8</flags>
<id>10</id>
<limit>true</limit>
<mid0>15</mid0>
<nme>充电已完成</nme>
<State sr="con0" ve="2">
<code>10</code>
<Int sr="arg0" val="1"/>
</State>
<State sr="con1" ve="2">
<code>140</code>
<Int sr="arg0" val="99"/>
<Int sr="arg1" val="100"/>
</State>
<Time sr="con2">
<fh>-1</fh>
<fm>-1</fm>
<rep>1</rep>
<repval>1</repval>
<th>23</th>
<tm>59</tm>
</Time>
</Profile>
<Task sr="task15">
<cdate>1577803544370</cdate>
<edate>1645924392304</edate>
<id>15</id>
<nme>电量告警-微信</nme>
<pri>100</pri>
<Action sr="act0" ve="7">
<code>131</code>
<Str sr="arg0" ve="3">Tasker/JavaScript/Butter2WeChat.js</Str>
<Str sr="arg1" ve="3"/>
<Int sr="arg2" val="1"/>
<Int sr="arg3" val="45"/>
</Action>
</Task>
</TaskerData>

2.执行的脚本

将脚本放到Tasker/JavaScript/目录中

Butter2WeChat.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//微信信息
var ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var SECRET = "xxxxxxxxxxxxxxxxxxxxxxx";
var AGENTID = "xxxxxxxxxx";

//定义post方法
function posthttp(url, data) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
flash(this.responseText); //显示返回消息,可删除本行
}
});
xhr.open("POST", url, false);
xhr.send(data);
return xhr.responseText;
}

//定义get方法
function gethttp(url) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
flash(this.responseText); //显示返回消息,可删除本行
}
});
xhr.open("GET", url, false);
xhr.send();
return xhr.responseText;
}

//获取token
var gettoken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + ID + "&corpsecret=" + SECRET;
var ACCESS_TOKEN = JSON.parse(gethttp(gettoken)).access_token;

//发送消息(文本)
var SMSRF = global('SMSRF');
var BATT = global('BATT');
var CONTENT;
if (BATT > 99) {
CONTENT = "当前电量:" + BATT + "\n充电已完成,请断开电源!";
} else if (BATT < 20){
CONTENT = "当前电量:" + BATT + "\n电量低于20%,请及时充电!";
} else {
CONTENT = "当前电量:" + BATT
}
var message = JSON.stringify({
"touser": "@all",
"msgtype": "text",
"agentid": AGENTID,
"text": {
"content": CONTENT
},
"safe": 0
});
var send = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + ACCESS_TOKEN;
posthttp(send, message);