Sunday, 30 December 2012

How to create an app like Airdroid (Part II)

If you have not read the Part I of this tutorial, do so now.

In my previous post we learned how to send an SMS using Android. Now we will add that functionality to our Airdroid like app. When this is ready, you should be able to send an SMS using your PC.

So, first of all, add the SEND_SMS permission to your manifest file. It should now have 4 permissions.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
We shall now create a new servlet, called SmsServlet that shows a form that accepts a phone number and a text message. It also has a Send button, on clicking which the SMS will be sent. As such there is nothing much in this servlet. It just has the following functionality,
  1. On doGet() it renders the form using HTML
  2. The form makes a POST request to the same servlet
  3. On doPost(), the servlet calls the sendSMS() method which actually sends the SMS.
Here is the code for SmsServlet.java,
package com.whycouch.httpserver;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import android.telephony.SmsManager;

/**
 * This servlet is used to send the SMS.
 * @author Hathy
 */
public class SmsServlet extends HttpServlet{
    
    private static final long serialVersionUID = -61789602L;    
    
    /**
     * This method is called when the user enters the URL in the
     * browser. 
     */
    @Override
    protected void doGet(HttpServletRequest req,
                         HttpServletResponse resp)
            throws ServletException, IOException {    
        resp.setContentType("text/html");
        resp.setStatus(HttpServletResponse.SC_OK);
        PrintWriter out=resp.getWriter();
        out.println("<html>" +
                "<title>SMS Form</title>" +
                "<body>" +
                "<form method='post'>" +
                "Phone # : "+
                "<input type='text' name='phone_number'/><br/>" +
                "Message : "+
                "<input type='text' name='message'/><br/>"+
                "<input type='submit' value='Send'/>" +
                "</form>" +
                "</body>" +
                "</html>");
    }
    
    /**
     * This method is called when the 'Send' button is pressed
     */    
    @Override
    protected void doPost(HttpServletRequest req,
                          HttpServletResponse resp)
            throws ServletException, IOException {    
        resp.setContentType("text/html");
        resp.setStatus(HttpServletResponse.SC_OK);
        PrintWriter out=resp.getWriter();
        String phoneNumber=req.getParameter("phone_number");
        String message=req.getParameter("message");
        
        if(phoneNumber==null || message==null) {
            out.println("Invalid input");
            return;
        }
        sendSMS(phoneNumber, message);
        
        out.println("<html>" +
                "<title>SMS Form</title>" +
                "<body>" +
                "Message sent to " + phoneNumber +
                "</body>" +
                "</html>");
    }
    
    private void sendSMS(String phoneNumber, String message){
        SmsManager sms=SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
}
Now that this servlet is ready, we must add it to our Jetty server. To do this, we must modify MyHttpServer.java. All you have to do is add one line to it in the construction. That line is,
handler.addServlet(new ServletHolder(new SmsServlet()),"/sms");
As you might have guessed, this line adds the servlet to the handler. The entire constructor should now look like this,
/**
 * Create the jetty server and let it listen at port 8080
 */
MyHttpServer(Context context){       
    this.context=context;    
    server=new Server(8080);
    handler=new ServletContextHandler();
    handler.setContextPath("/");
    handler.addServlet(new ServletHolder(new SmsServlet()),"/sms");
    server.setHandler(handler);
}
There you go. Our app is now ready to send SMS using a PC browser. Deploy it to your device, get its IP address, and on your PC browser, type in <YOUR_IP_ADDRESS>:8080/sms. You should see a form like this,
SMS Form on Firefox

5 comments:

  1. This is very cool! Thank you for showing this! Not really looking to do something like Airdroid (have a different idea) but I was wondering where to begin in order to get this idea off the ground. This tutorial helped me!

    ReplyDelete
  2. Really a very nice article...

    I tried the same example which you have given here but was getting a Class Not Found Exception for the Jettys server class , even after adding the 2 jar files in the project via the build path... can you please shed some light on it as to how we can resolve this

    Thank you..

    ReplyDelete
  3. I am a new developer and I like you tutorial. Do you send me your project, I think it will help me so much
    This is my email : tuchonghi@gmail.com
    Thank you

    ReplyDelete
  4. how can I upload and download data from browser to mobi

    ReplyDelete
  5. How can I display image in the servlet along with text..

    ReplyDelete