Thursday, 27 October 2011

For sending message from android application

first create the sms.java activity

package com.sms;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Sms extends Activity
{
    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);       

        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);

        btnSendSMS.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {               
                String phoneNo = "121";
                String message = "Start";               
                if (phoneNo.length()>0 && message.length()>0)               
                    //sendSMS(phoneNo, message);
                 background.start();
                              
                else
                    Toast.makeText(getBaseContext(),
                        "Please enter both phone number and message.",
                        Toast.LENGTH_SHORT).show();
            }
        });       
    } 
   
  //---sends an SMS message to another device---
   
    private void sendSMS(String phoneNumber, String message)
    {       
        PendingIntent pi = PendingIntent.getActivity(this, 0,
            new Intent(this, Sms.class), 0); 
             android.telephony.SmsManager sms=android.telephony.SmsManager.getDefault();
                sms.sendTextMessage(phoneNumber, null, message, pi, null);       
    }   
   
    Thread background = new Thread (new Runnable() {
        public void run() {
            try {
                // enter the code to be run while displaying the progressbar.
                //
                // This example is just going to increment the progress bar:
                // So keep running until the progress value reaches maximum value
               /* while (dialog.getProgress()<= dialog.getMax()) {
                    // wait 500ms between each update
                    Thread.sleep(500);
                    // active the update handler
                    progressHandler.sendMessage(progressHandler.obtainMessage());
                }
                */
             sendSMS("121", "start");
             //Thread.sleep(500);
           
            } catch (Exception e) {
                // if something fails do something smart
            }
        }
     });
}


for sending an sms we have to set some permissions in androidManifest.xml ,as shown below

xml version="1.0" encoding="utf-8"?>manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sms"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Sms"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="ReadSms">
<intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter>
</

</application>
</manifest>

Here

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

means ReadSms is a broadcastreciever ,this class represents the reading an sms which is coming newly to the mobile

ReadSms.java


package com.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class ReadSms extends BroadcastReceiver{
 @Override
 public void onReceive(Context context, Intent intent) {
  Bundle myBundle = intent.getExtras();
        SmsMessage [] messages = null;
        String strMessage = "";
        if (myBundle != null)
        {
            Object [] pdus = (Object[]) myBundle.get("new message");
            messages = new SmsMessage[pdus.length];
            for (int i = 0; i < messages.length; i++)
            {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                strMessage += "SMS From: " + messages[i].getOriginatingAddress();
                strMessage += " : ";
                strMessage += messages[i].getMessageBody();
                strMessage += "\n";
            }
            Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
        }
    }
}

Main.Xml

<?
<
xml version="1.0" encoding="utf-8"?>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Enter the phone number of recipient"
/> <EditText android:id="@+id/txtPhoneNo" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Message"
/> <EditText android:id="@+id/txtMessage" android:layout_width="fill_parent" android:layout_height="150px"
android:gravity="top" /> <Button android:id="@+id/btnSendSMS" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Send SMS"
/>
</
LinearLayout>

receiver>

<?
<

No comments:

Post a Comment