android连adb收短信,可以用廉价手机当短信平台啦

import subprocess
import datetime,time
import platform

adb = r'c:\adb\adb.exe'
sqlite_exe = 'su -c \'sqlite3 /data/data/com.android.providers.telephony/databases/mmssms.db \"%s\"\''
    
current_time = int(subprocess.check_output([adb,'shell','date +%s']))*1000
last_updated = str(current_time)



while True:
    print 'last_updated:' + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(int(last_updated)/1000))
    sql = sqlite_exe % ('SELECT address, date, body, type FROM sms WHERE date > ' + last_updated + ' order by date desc')
    sms_dump = subprocess.check_output([adb,'shell',sql])

    for i,line in enumerate(sms_dump.split('\n')):
        part = line.split('|')
        if len(part) > 3 and part[3].strip() == '1':
            #SMS
            if platform.system() == 'Windows':
                print str(i) + 'th: ' + 'Sender:' + part[0] + ',time:' + time.strftime("%Y-%m-%dT %H:%M:%S",time.localtime(int(part[1])/1000)) + ',Body:' + part[2].decode('utf8').encode('gbk') + ',type:' + part[3]
            else:
                print str(i) + 'th: ' + 'Sender:' + part[0] + ',time:' + time.strftime("%Y-%m-%dT %H:%M:%S",time.localtime(int(part[1])/1000)) + ',Body:' + part[2] + ',type:' + part[3]

            if i == 0:
                last_updated = part[1]

    time.sleep(10)

 平台:python 2.7

posted @ 2013-06-20 19:35  latyas  阅读(915)  评论(0编辑  收藏  举报