SmsMessage Demo : SMS « Network « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » Network » SMS 
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.