How do you rate this blog

Thursday, May 24, 2012

Simple SMS - Receive Feature


    In the previous post I had told about the method to send sms, in this post I will tell about how to receive SMS and how to test out the application.

We will use the same application which we used for sending SMS, here we will add one more class called Rec which will extend BroadcastReceiver have code to receive application.  Once you do this you need to implement onRecieve method as shown below and write an intent filter which will allow it to receive SMS.
/*to obtain the message*/        
public void onReceive(Context C, Intent I) {
                               
                                Bundle B=I.getExtras();
                                SmsMessage[] msgs=null;
                                String sms_str="";
/* Receive SMS and display*/                                   
                                if(B !=null){
                Object[] pdus=(Object[]) B.get("pdus");
                msgs=new SmsMessage[pdus.length];
                for(int i=0;i<msgs.length;i++){
/*To convert the sms which is in PDU format*/
                msgs[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
/*To display the number from which the message was sent*/
                sms_str+="Recieved SMS From : "+msgs[i].getOriginatingAddress() +" : ";
                sms_str+=msgs[i].getMessageBody().toString()+"\n";
                                                }
                Toast.makeText(C, sms_str, Toast.LENGTH_LONG).show();
                                }
The code for receive class will look like in the snapshot given below.



Now we need to modify the manifest.xml file to include the following

<receiver android:name=".Rec">
     <intent-filter>
           <action android:name="android.provider.Telephony.SMS_RECEIVED" />
     </intent-filter>
</receiver>

Which will display the message in the application and will display it as a Toast.
Also include the below code so that the application has the access to receive sms.
<uses-permission android:name="android.permission.RECEIVE_SMS">
    </uses-permission>
So the manifest file will look like in the image given below.



Once these are done the application is ready to receive the SMS. So now the question is how to test it. Inorder to do test if the application receives the the eclipse has a DDMS perspective which allows you to send voice or sms to the emulator. To access DDMS you can refer to the following link DDMS Link. Now type in the emulator number in this case it is 5554 and send as SMS as shown in the snapshot below. 



Once you click on Send button in DDMS you can see that the SMS is received in the application as shown in the snapshot shown below.


Saturday, May 5, 2012

Simple SMS - Send feature


SMS is the most used and one of the most popular features in the mobile phone. In this post I have explained a simple custom sms app which uses the API’s provided by android.

                In this app I will create a text box which will take only numbers and one more text box where in you can type in your message. Once these are done the user can click on the send button which will send the sms to the phone number mentioned.
So lets get started with the creation of the application-
Step 1 – Modification of AndroidManifest.xml file
                Since this application sends SMS we need to provide the application rights to access the SMS service in the mobile device. So we need to add the code below in the “androidmanifest.xml” file
<uses-permission android:name="android.permission.SEND_SMS">    </uses-permission>
The manifest file would like in the snapshot shown below.



Step 2 – Layout Creation
                The layout for this is kept very simple wherein we have one editable text box for phone number and other for message. The layout will be as shown in the snapshot given below.



Step 3 – Code to send SMS
                Open the java file in src folder, as you can see you already have basic code already setup for you. So we need to just implement the send sms functionality in this. Declare the buttons and editable text boxes and bind them to the respective controls in the UI as shown in the code below –
    Button send;
    EditText ph_num;
    EditText message;

/*bind the variables to the controls used in the UI*/
        send=(Button) findViewById(R.id.button1);
        ph_num=(EditText) findViewById(R.id.editText1);
        message=(EditText) findViewById(R.id.editText2);
       
Then implement the send SMS functionality when you click the Send SMS button, the code for which is given below.

send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*to convert the phone number and the message to string format*/
                String phone_num=ph_num.getText().toString();
                String sms=message.getText().toString();
/*we will go to send sms functionality only if there is message and phone number*/
                if(phone_num.length()>0 && sms.length() > 0)
                                send_sms(phone_num,sms);
                else
Toast.makeText(getBaseContext(), "Please enter both phone number and message", Toast.LENGTH_LONG).show();
                                                }
                                });

Now we will code the Send SMS functionality –
Inorder to send SMS android provides SmsManager, but this object cant be instantiated directly hence we need to make use of Pending Intent. A Pending Intent is a token that is provided to a foreign application which gives it right to use the applications permission to execute a predefined code in the application. This is done as per the code shown below
public void send_sms(String phone, String msg){
PendingIntent SMS_Sent=PendingIntent.getActivity(this, 0, new Intent(this,AndroidSMSActivity.class), 0);
                 SmsManager sms=SmsManager.getDefault();
                 sms.sendTextMessage(phone, null, msg, SMS_Sent, null);
                Toast.makeText(this, "SMS Sent", Toast.LENGTH_LONG).show();
    }
At the end we add a Toast to notify the user that the SMS has been sent. The code will be as shown below in the snapshot.


Now run the application through the emulator, although it doesnt send sms to any mobile device and the result will be as shown in the snapshot below. 



I will explain how to test out the application using 2 emulators in the post where I will explain how to receive SMS. Hope this was useful to you.