Android Tasker应用之自动查询并显示话费流量套餐信息

时间:2023-03-08 18:19:01
Android Tasker应用之自动查询并显示话费流量套餐信息

Android Tasker应用之自动查询并显示话费流量套餐信息

虽然Android平台有非常多的流量监控软件,但最准确的流量数据还是掌握在运营商手里。有些朋友可能像我一样时不时地发短信查询流量信息,这个操作在智能手机上显得太不智能了,本文将用Tasker让这个操作变得智能化。

1、功能需求

  • 每天早上闹钟响时,Tasker自动发送短信给运营商查询套餐信息。收到短信后Tasker自动分析短信内容,并将话费余额,流量信息等显示在桌面上。
  • 用户可以手动随时给运营商发短信查询,Tasker自动判断为套餐查询信息后提取信息并显示。
  • 显示的内容包括更新时间、当前话费、话费余额、剩余流量、剩余语音时长(由运营商提供的信息确定)

最终需要实现的效果:

Android Tasker应用之自动查询并显示话费流量套餐信息

2、实现分析

整个操作由两个部分组成:

  • 发送部分:早上闹钟响起时自动给运营商发送一条短信查询套餐信息;
  • 接收部分:当收到运营商的套餐信息(可以是自动查询或手动查询的结果)后自动分析短信内容并显示。
    发送部分比较比较好实现,可以建立一个自动发短信任务,这个任务由闹钟响起事件触发。另外为了避免其它时候的闹钟触发这个任务,可以增加一个时间限制,比如只有在早上7点到8点之间的闹钟才能触发。

接受部分由一个短信监听任务完成,每次收到短信时将触发这个任务。这个任务首先判断短信发送者是不是运营商,然后判断短信内容是不是查询套餐的短信(使用正则表达式匹配特征字符串),最后就是提取有用信息并发到前端(Minimalistic Text)显示。

3、自动发送短信

新建一个Profile,增加两个条件Context:事件条件为Alarm Clock,事件条件为7:00~9:00;

添加一个发送短信的任务,内容为101,接收者为10010(根据你的运营商和套餐查询命令而定)。

你可以直接导入我配置好的Profile(针对中国联通3G套餐),点击下载:send_sms_on_alarm.prf,导入后的Profile如下图:

<TaskerData sr="" dvi="1" tv="1.3.2b1m">
<Profile sr="prof23">
<cdate>1350492225043</cdate>
<edate>1352092212443</edate>
<id>23</id>
<mid0>27</mid0>
<nme>send sms on alarm</nme>
<Event sr="con0" ve="2">
<code>305</code>
<Str sr="arg0" ve="3"/>
</Event>
<Time sr="con1">
<fh>7</fh>
<fm>0</fm>
<th>9</th>
<tm>0</tm>
</Time>
</Profile>
<Task sr="task27">
<cdate>1350493303738</cdate>
<edate>1352092033132</edate>
<id>27</id>
<Action sr="act0" ve="3">
<code>41</code>
<lhs>%AIR</lhs>
<op>1</op>
<rhs>off</rhs>
<Str sr="arg0" ve="3">10010</Str>
<Str sr="arg1" ve="3">101</Str>
<Int sr="arg2" val="0"/>
</Action>
</Task>
</TaskerData>

Android Tasker应用之自动查询并显示话费流量套餐信息

4、短信内容提取

注:各个运营商的套餐查询短信不容,这里以中国联通为例。

新建一个Profile,事件EventReceived Text并且Sender为10010

添加一个匿名任务;

Profile里已经完成了短信发送者的过滤,因此在Task里收到的短信肯定是10010发的,但是10010发的不一定是套餐查询短信,

也可能是广告,因此需要判断是否是套餐短信,这里使用正则表达式来完成。联通3G短信的大致匹配为"^尊敬的[\s\S]您当前套餐为[\s\S]本月可用金额[\s\S]*含国内手机上网流量"。

确信短信内容无误后就可以进行信息提取了,下面是一条完整的话费查询信息:

尊敬的某某用户,您当前套餐为WCDMA(3G)-46元基本套餐B套餐。本月可用金额为115.43元。当前实时话费合计71.44元,3G套包内含国内语音拨打时长120分钟,现已使用20分钟;含国内手机上网流量40MB,现已使用0MB;含国内可视电话拨打时长5分钟,现已使用0分钟;含多媒体使用量3M,现已使用0M;含文本使用量5T,现已使用0T。本次查询结果存在时延,敬请留意。广东联通。【买3G就上 .com】

Tasker内置了字符串操作函数,我们可以直接使用它来切割字符串,我们将上面这段内容赋值给%mmsgb变量,下面是具体的步骤:

  • STEP 1
    输入:        mmsgb
    分隔符:"本月可用金额为"
    结果:
    mmsgb1: 尊敬的某某用户,您当前套餐为WCDMA(3G)-46元基本套餐B套餐。
    mmsgb2: 115.43元。当前实时话费合计71.74元,3G套包内含国内语音拨打时长120分钟,现已使用13分钟;含国内手机上网流量40MB,现已使用20MB;含国内可视电话拨打时长5分钟,现已使用0分钟;含多媒体使用量3M,现已使用0M;含文本使用量5T,现已使用0T。本次查询结果存在时延,敬请留意。广东联通。【买3G就上 10010.com】

  • STEP 2
    输入:      mmsgb2
    分隔符: "。当前实时话费合计"
    结果:
    mmsgb21: 115.43元 (话费余额信息)
    mmsgb22: 71.74元,3G套包内含国内语音拨打时长120分钟,现已使用13分钟;含国内手机上网流量40MB,现已使用20MB;含国内可视电话拨打时长5分钟,现已使用0分钟;含多媒体使用量3M,现已使用0M;含文本使用量5T,现已使用0T。本次查询结果存在时延,敬请留意。广东联通。【买3G就上 10010.com】

提示:Tasker的字符串分割功能会自动将结果保存到数组中,如果你学过编程的话,应该很容易看出mmsgb21其实就是mmsgb[2][1]的意思,只不过Tasker的数组没有方括号而已。

按上面的步骤依次提取剩下的信息,你也可以下载我配置好的Profile(只对联通3G套餐有效),点击下载:send_sms_to_check_phone_quota_recv_.prf

<TaskerData sr="" dvi="1" tv="1.3.2b1m">
<Profile sr="prof28">
<cdate>1350493482359</cdate>
<edate>1352092746914</edate>
<id>28</id>
<mid0>18</mid0>
<nme>send sms to check phone quota(recv)</nme>
<Event sr="con0" ve="2">
<code>7</code>
<pri>0</pri>
<Int sr="arg0" val="2"/>
<Str sr="arg1" ve="3">10010</Str>
<Str sr="arg2" ve="3"/>
</Event>
</Profile>
<Task sr="task18">
<cdate>1350483797760</cdate>
<edate>1351329998620</edate>
<id>18</id>
<nme>sms query phone quota to MT</nme>
<pri>10</pri>
<Action sr="act0" ve="3">
<code>547</code>
<label>msg content</label>
<Str sr="arg0" ve="3">%mmsgb</Str>
<Str sr="arg1" ve="3">%SMSRB</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act1" ve="3">
<code>547</code>
<label>msg check result, default is 1(pass)</label>
<Str sr="arg0" ve="3">%mmsgcheck</Str>
<Str sr="arg1" ve="3">1</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act10" ve="3">
<code>590</code>
<Str sr="arg0" ve="3">%mmsgb2223</Str>
<Str sr="arg1" ve="3">;含国内可视电话拨打时长</Str>
<Int sr="arg2" val="0"/>
</Action>
<Action sr="act11" ve="3">
<code>547</code>
<label>save information</label>
<Str sr="arg0" ve="3">%mpquota</Str>
<Str sr="arg1" ve="3">更新时间: %SMSRD %SMSRT
可用余额: %mmsgb21
当前话费: %mmsgb221
语音时长: 已用 %mmsgb22221 / 本月可用 %mmsgb2221
套餐流量: 已用 %mmsgb22231 / 本月可用 %mmsgb22222</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act12" ve="3">
<code>14593</code>
<Bundle sr="arg0">
<Vals sr="val">
<com.twofortyfouram.locale.intent.extra.BLURB>%DPQUOTA = %mpquota</com.twofortyfouram.locale.intent.extra.BLURB>
<com.twofortyfouram.locale.intent.extra.BLURB-type>java.lang.String</com.twofortyfouram.locale.intent.extra.BLURB-type>
<de.devmil.minimaltext.locale.extras.VAR_NAME>%DPQUOTA</de.devmil.minimaltext.locale.extras.VAR_NAME>
<de.devmil.minimaltext.locale.extras.VAR_NAME-type>java.lang.String</de.devmil.minimaltext.locale.extras.VAR_NAME-type>
<de.devmil.minimaltext.locale.extras.VAR_TEXT>%mpquota</de.devmil.minimaltext.locale.extras.VAR_TEXT>
<de.devmil.minimaltext.locale.extras.VAR_TEXT-type>java.lang.String</de.devmil.minimaltext.locale.extras.VAR_TEXT-type>
<net.dinglisch.android.tasker.extras.VARIABLE_REPLACE_KEYS>de.devmil.minimaltext.locale.extras.VAR_TEXT</net.dinglisch.android.tasker.extras.VARIABLE_REPLACE_KEYS>
<net.dinglisch.android.tasker.extras.VARIABLE_REPLACE_KEYS-type>java.lang.String</net.dinglisch.android.tasker.extras.VARIABLE_REPLACE_KEYS-type>
<net.dinglisch.android.tasker.subbundled>true</net.dinglisch.android.tasker.subbundled>
<net.dinglisch.android.tasker.subbundled-type>java.lang.Boolean</net.dinglisch.android.tasker.subbundled-type>
</Vals>
</Bundle>
<Str sr="arg1" ve="3">de.devmil.minimaltext</Str>
<Str sr="arg2" ve="3">Minimalistic Text</Str>
</Action>
<Action sr="act13" ve="3">
<code>38</code>
</Action>
<Action sr="act2" ve="3">
<code>547</code>
<label>check if send by China Unicom</label>
<lhs>%SMSRF</lhs>
<op>2</op>
<rhs>10010</rhs>
<Str sr="arg0" ve="3">%mmsgcheck</Str>
<Str sr="arg1" ve="3">0</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act3" ve="3">
<code>547</code>
<label>check if msg pattern ok</label>
<lhs>%mmsgb</lhs>
<op>12</op>
<rhs>^尊敬的[\s\S]*您当前套餐为[\s\S]*本月可用金额[\s\S]*含国内手机上网流量</rhs>
<Str sr="arg0" ve="3">%mmsgcheck</Str>
<Str sr="arg1" ve="3">0</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
</Action>
<Action sr="act4" ve="3">
<code>37</code>
<label>msg check ok, do split</label>
<lhs>%mmsgcheck</lhs>
<op>5</op>
<rhs>1</rhs>
</Action>
<Action sr="act5" ve="3">
<code>590</code>
<Str sr="arg0" ve="3">%mmsgb</Str>
<Str sr="arg1" ve="3">本月可用金额为</Str>
<Int sr="arg2" val="0"/>
</Action>
<Action sr="act6" ve="3">
<code>590</code>
<Str sr="arg0" ve="3">%mmsgb2</Str>
<Str sr="arg1" ve="3">。当前实时话费合计</Str>
<Int sr="arg2" val="0"/>
</Action>
<Action sr="act7" ve="3">
<code>590</code>
<Str sr="arg0" ve="3">%mmsgb22</Str>
<Str sr="arg1" ve="3">,3G套包内含国内语音拨打时长</Str>
<Int sr="arg2" val="0"/>
</Action>
<Action sr="act8" ve="3">
<code>590</code>
<Str sr="arg0" ve="3">%mmsgb222</Str>
<Str sr="arg1" ve="3">,现已使用</Str>
<Int sr="arg2" val="0"/>
</Action>
<Action sr="act9" ve="3">
<code>590</code>
<Str sr="arg0" ve="3">%mmsgb2222</Str>
<Str sr="arg1" ve="3">;含国内手机上网流量</Str>
<Int sr="arg2" val="0"/>
</Action>
<Img sr="icn" ve="2">
<icn>2130837510</icn>
</Img>
</Task>
</TaskerData>

5、信息显示

前端依然使用Minimalistic Text来显示,在短信接收的Profile中有一个动作是将提取出的内容发送给 Minimalistic Text 的"%DPQUOTA"变量

我们只需在Minimalistic Text里配置显示就可以了。

先添加一个3x1的Widget,排版方式为自定义,然后添加一个本地变量就可以显示了,板式可以自己美化一下。

Android Tasker应用之自动查询并显示话费流量套餐信息

最后手动发条短信测试一下吧,如果显示没问题的话以后每天早上这个过程都可以由Tasker自动完成了。