SmsMessage Demo : SMS « Network « Android






SmsMessage Demo

 
package app.test;

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

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;

 class MySMSMonitor extends BroadcastReceiver {

  private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

  @Override
    public void onReceive(Context context, Intent intent)
    {
    if(intent!=null && intent.getAction()!=null && 
          ACTION.compareToIgnoreCase(intent.getAction())==0)
      {
          Object[]pduArray = (Object[]) intent.getExtras().get("pdus");
          SmsMessage[] messages = new SmsMessage[pduArray.length];
          for (int i = 0; i<pduArray.length; i++) {
              messages[i] = SmsMessage.createFromPdu((byte[])pduArray [i]);
              Log.d("MySMSMonitor", "From: " + messages[i].getOriginatingAddress());
              Log.d("MySMSMonitor", "Msg: " + messages[i].getMessageBody());
          }
          Log.d("MySMSMonitor","SMS Message Received.");
      }
  }
}

public class Test extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
    }
  public void doSend(View view) {
        EditText addrTxt = 
                (EditText)findViewById(R.id.addrEditText);

        EditText msgTxt = 
                (EditText)findViewById(R.id.msgEditText);

        try {
            sendSmsMessage(
                 addrTxt.getText().toString(),
                msgTxt.getText().toString());
            Toast.makeText(this, "SMS Sent", 
                  Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(this, "Failed to send SMS", 
                  Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void sendSmsMessage(String address,String message)throws Exception
    {
        SmsManager smsMgr = SmsManager.getDefault();
        smsMgr.sendTextMessage(address, null, message, null, null);
    }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:layout_width="wrap_content"
      android:layout_height="wrap_content" android:text="Destination Address:" />

    <EditText android:id="@+id/addrEditText"
      android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:phoneNumber="true" android:text="9045551212" />

  </LinearLayout>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:layout_width="wrap_content"
      android:layout_height="wrap_content" android:text="Text Message:" />

    <EditText android:id="@+id/msgEditText" android:layout_width="fill_parent"
      android:layout_height="wrap_content" android:text="hello sms" />

  </LinearLayout>

  <Button android:id="@+id/sendSmsBtn" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Send Text Message"
    android:onClick="doSend" />

</LinearLayout>

   
  








Related examples in the same category

1.SMS Intent
2.Sends an SMS message to another device
3.extends BroadcastReceiver to create SMS Receiver
4.SMS Inbox Demo
5.Sms Messaging Demo
6.start SMS Intent