Monday 31 December 2012

Send SMS Demo

Demo:





1)Activity:
 
package com.venky.sendsmsdemo;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

      Button btnSend;

      EditText edtPhNumber, edtMessage;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            btnSend = (Button) findViewById(R.id.btnSend);

            edtPhNumber = (EditText) findViewById(R.id.edtPhoneNumber);
            edtMessage = (EditText) findViewById(R.id.edtMessage);

            btnSend.setOnClickListener(new OnClickListener() {

                  @Override
                  public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                        SmsManager manager = SmsManager.getDefault();

                        String phoneNumber = edtPhNumber.getText().toString();
                        String message = edtMessage.getText().toString();

                        Toast.makeText(MainActivity.this, "Message Sending ...", 5000)
                                    .show();

                        manager.sendTextMessage(phoneNumber, null, message, null, null);

                  }
            });
      }

}


2)main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#5F04B4"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:text="Sending SMS"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Enter Phone Number"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

    <EditText
        android:id="@+id/edtPhoneNumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Enter Message"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

    <EditText
        android:id="@+id/edtMessage"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:hint="Enter message" />

    <Button
        android:id="@+id/btnSend"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Send" />

</LinearLayout>


3)Add this Permission in AndroidManifest.xml: 

    <uses-permission android:name="android.permission.SEND_SMS"/>


4)Download this Project Click Here

No comments:

Post a Comment