SL4A - Utilities using Python Facades



Email-based Applications

Email based applications are one of the most common utilities available in a mobile device. One can use the sendEmail API call available through the SL4A Android facade.

This function takes three parameters −

  • to_address − a comma-separated list of recipients.

  • title − represents the title of the email message.

  • message − represents the message to be sent.

import android,datetime,smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 

droid = android.Android() 
serv = ’smtp.gmail.com’ 
port = 587 
mailto = ’chris’ 
mailfrom = ’charley’ 
pass = ’pass@123’ 

msg = MIMEMultipart() 
msg[‘Subject’] = ’Tes Mail’ 
msg[‘To’] = mailto 
msg[‘From’] = mailfrom 

body = ’This is a test mail!!’ 
msg.attach(MIMEText(body,’plain’)) 

smtpCon = smtplib.SMTP(serv,port) 
smtpCon.starttls() 
smtpCon.login(mailfrom,pass) 
smtpSendmail(mailfrom,mailto,msg.as_string()) 
smtpCon.close()

The python library that have used to build the email program is smtplib. In addition, we have used the email library. This library contains a number of helper functions allowing us to construct our message in the correct form. The mimetypes library helps with the encoding of our message.

Wifi Scanner

The following code lists all available Wi-Fi access spots −

import android, time 

def main(): 
   global droid 
   droid = android.Android() 
	
while not droid.wifiStartScan().result: 
   time.sleep(0.25) 
	
networks = {} 

while not networks: 
   for ap in in droid.wifiGetScanResults().result: 
      networks[ap[‘bssid’]] = ap.copy() 
		
   droid.dialogCreateAlert(‘Access Points’) 
   droid.dialogSetItems([‘%(ssid)s,%(level)s,%(capabilities)s’ % 
      ap for ap in networks.values() ]) 
		
droid.dialogSetPositiveButtonText(‘OK’) 
dorid.dialogShow() 

if __name__=’__main__’: 
   main()

Call Logs

The code for call logs is given below.

import android 
droid = android.Android() 
mylog = droid.getConstants("android.provider.Calllog$Calls").result 
calls = droid.queryContent(mylog["CONTENT_URI"],["name","number","duration"]).result 

for c in calls: 
   print c
Advertisements