The Email Results Function
The first thing we want to do is to setup a function that will give the user a choice on how to proceed. The choices we want to use are "Text" and "Excel." The solution we are going with is an AlertDialog that will display the interface. The user will be asked if he wants to email results as plain text or as an Excel spreadsheet.
The AlertDialog object will be set to a new AlertDialog,Builder(this) that will use three methods:
- setMessage - this sets the message to display
- setPositiveButton - this sets the text and an onClickListener to use when the positive button is pressed
- setNegativeButton - this sets the text and an onClickListener to use when the negative button is pressed
The positive button will handle sending plain text to the email application. The negative button will handle sending the Excel file to the email application as a spreadsheet. The positive button onClickListener will call the emailPlainText() function. Since the jxl package is used to help create Excel spreadsheets, the negative button will have the emailExcelSpreadsheet in a try-catch block that will catch an IOException and a WriteException since it is usually thrown when generating an Excel file using the API. Once done, our code snippet should look like this:
Email as PlainText Function
This is self-explanatory as all is done here is concatenate all the data into a String representing your message. The next step is to create a new Intent with the ACTION.SEND attribute that will deliver the data to someone else (like an email). The putExtra method is called three times that will encompass EXTRA_EMAIL (email destinations, usually blank), EXTRA_SUBJECT (subject line) and EXTRA_TEXT (the body of the message). The setType method is set to "plain/text" and the activity is started. Once done, our code snippet would look similar to this:
Email Excel Spreadsheet Function
This is more complicated because if the user were to send an Excel spreadsheet as an attachment, it must first exist as a file before it can be sent. The first step is to create a File object that will get the path. Next, the filename must be set. Another File object is used to set it to the path and the filename in the path. A check of the exists method for the File object to call to determine if the file exists. If false, the createSpreadsheet function is used to build the spreadsheet. The Intent and the putExtras are next as before with one exception. In this case, the EXTRA_STREAM is used and is set to the file that is parsed by a Uri object. For Excel spreadsheets, the setType method is set to "application/excel" and the activity starts. Once done, our code snippet should look like this:
Email Results Interface In Action
Now we head back to the Email Results interface from before, Once "Text" is clicked, you get the results in plain text. Once "Excel" is clicked, you get the file attached to an email document similar to this:
This is your end result. Please stay tuned to this channel for more bright ideas related to building cool apps.
No comments:
Post a Comment