Wednesday, April 17, 2013

Python SL4A script to mark specific unread sms message as read


Python SL4A script to mark specific unread sms message as read

 

In my previous entry, I post an idea about how to mark any specific unread sms message as read via sqlite 3 command.

 

However this method has a serious flaw which could not be fixed anyway.

 

Via sqlite 3 command, you really are able to access the message database and change the specific message state from unread to read, but the problem is this operation could not affect the android message API. Everything you have done is done just within the message database. That means if you go back to the message inbox after you done with sqlite 3 command, you will see the specific message which you were trying to mark as read is still in the state of unread. (PS. Please note, not only sqlite 3 command, but also via python sl4a script, both methods could not update/remove the message app unread notify displayed on the launcher desktop or notification bar. Cause this is related/linked with the android default notification system instead.)

 

Maybe you will say it does not matter at all even if sqlite 3 command has this problem.

 

I should let you know it does matter indeed. If you ever use tasker unread text context to perform some tasks, you will soon find out that the importance to be able to change the message unread state within the message API. Cause tasker will consider there is still an unread text out there and then fire the profile, which you did not expect to be done.

 

So what is the workaround? Python sl4a script should be the way out of this.

 

After you input the hereinafter python script into the sl4a client for android, you may run this script in tasker via run sl4a script function.

 

Allright, let us get into the business.

 

Please be discreet of the indentation of each line, cause it is the basic rule of python language. Also you had better be careful of the text coding format, if you are about to copy it and paste. Cause it might change the coding format into windows coding or ACSII format, which the script would fail to perform.

 

NB. XXXXX stands for the specific sender cellphone number, and YYYYYYY is the specific  keyword of the incoming unread message which you expect to mark it as read.

 

If you have any questions, please feel free to leave me a comment, I will see what I can do for you.

 

 

Script content:

 

 

 

# coding:utf-8

 

import android

 

droid = android.Android()

 

 

msgIDs = droid.smsGetMessageIds(True, 'inbox').result

if msgIDs > 0:

 

  for msgID in msgIDs:

 

    message = droid.smsGetMessageById(msgID, ['address','body']).result

 

    str1 = message['address'].encode('utf-8')

 

    str2 = message['body'].encode('utf-8')

 

    if str1.find('XXXXXXX') >= 0 and str2.find('YYYYYYY') >= 0:

 

      msgRead=[0]

 

      msgRead[0]=msgID

 

      droid.smsMarkMessageRead(msgRead,1)

6 comments:

  1. Hey - awesome post! i've been looking for something like this for awhile now, but i've had no idea how to code it. A question about the variables "XXXXXXXXXXX" and "YYYYYYYYY" - can i insert tasker variables into these spaces, or does it need to be some other way. If it is some other way, how do I go about it?

    ReplyDelete
    Replies
    1. Hi,

      of course you can.

      i think i have already posted this method in other entries.

      just use tasker variable to replace XXXX and YYYY. and remember to pass variable to sl4a in tasker.

      Cathy

      Delete
  2. Hi, Cathy!

    Now I can clear the sms unread notify on notification bar via Tasker. You can try it. (http://noteofirefly.blogspot.com/2013/09/mark-unread-sms-as-read-thoroughly-via.html)

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. EDIT: So with this script, if I understand correctly, the unread count on the icon will not be removed? If I open the messaging app, it still appears as unread and has the number of unread messages at the top. Is this expected behaviour?

    ReplyDelete
  5. How about using Compose SMS, wait 1 sec and Go Home. Works for non rooted devices. Limitations: Sender has to be in contacts.

    I have a time triggered sending sms to my provider, waiting for the response (4 messages) with time remaining info for my program (splitters and values used on Zooper Widget). So I wanted to erase those messages from SPECIFIC sender (cell povider) which I added on contacts, and then added those 3 commands to the end of the splitting task. Works like a charm.

    Chris from Greece :D

    ReplyDelete