package com.sms.smstest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static final String TAG = "SmsTest/MainActivity";

    private TextView mSender;
    private TextView mContent;
    private IntentFilter mIntentFilter;
    private MessageReciever mMessageReceiver;

    private EditText mTo;
    private EditText mMsgInput;
    private Button mSendMessage;

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

        mSender = (TextView) findViewById(R.id.sender);
        mContent = (TextView) findViewById(R.id.content);
        mTo = (EditText) findViewById(R.id.to);
        mMsgInput = (EditText) findViewById(R.id.msg_input);
        mSendMessage = (Button) findViewById(R.id.send);
        mSendMessage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mTo.getText().toString().length() > 0
                        && mMsgInput.getText().toString().length() > 0) {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(mTo.getText().toString(), null, mMsgInput.getText()
                            .toString(), null, null);
                }
            }
        });

        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        mIntentFilter.setPriority(100);
        mMessageReceiver = new MessageReciever();
        registerReceiver(mMessageReceiver, mIntentFilter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

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

    class MessageReciever extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "Received intent " + intent);
            Bundle bundle = intent.getExtras();
            Object[] pdus = (Object[]) bundle.get("pdus"); // get SMS message
            SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < messages.length; ++i) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }
            String address = messages[0].getOriginatingAddress();
            String fullMessage = "";
            for (SmsMessage message : messages) {
                fullMessage += message.getMessageBody();
            }
            Log.i(TAG, "address is " + address);
            Log.i(TAG, "fullMessage is " + fullMessage);

            mSender.setText(address);
            mContent.setText(fullMessage);

            // filter this message by stop this broadcast
            abortBroadcast();
        }
    }
}

@@ -48,16 +48,50 @@
 tivity;%0A
+import android.app.PendingIntent;%0A
 import a
@@ -120,24 +120,24 @@
 stReceiver;%0A
-
 import andro
@@ -566,16 +566,45 @@
 extView;
+%0Aimport android.widget.Toast;
 %0A%0Apublic
@@ -946,16 +946,107 @@
 ssage;%0A%0A
+    private IntentFilter mSendFilter;%0A    private SendStatusReceiver mSendStatusReceiver;%0A%0A
     @Ove
@@ -1738,24 +1738,273 @@
 th() %3E 0) %7B%0A
+                    mMsgInput.setText(%22%22);%0A                    Intent intent = new Intent(%22SENT_SMS_ACTION%22);%0A                    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,%0A                            intent, 0);%0A
             
@@ -2200,20 +2200,29 @@
 ring(), 
-null
+pendingIntent
 , null);
@@ -2531,24 +2531,233 @@
 tentFilter);
+%0A%0A        mSendFilter = new IntentFilter();%0A        mSendFilter.addAction(%22SENT_SMS_ACTION%22);%0A        mSendStatusReceiver = new SendStatusReceiver();%0A        registerReceiver(mSendStatusReceiver, mSendFilter);
 %0A    %7D%0A%0A    
@@ -3516,24 +3516,73 @@
 eReceiver);%0A
+        unregisterReceiver(mSendStatusReceiver);%0A
         supe
@@ -4625,24 +4625,24 @@
 roadcast();%0A
-
         %7D%0A  
@@ -4644,11 +4644,422 @@
  %7D%0A    %7D
+%0A%0A    class SendStatusReceiver extends BroadcastReceiver %7B%0A%0A        @Override%0A        public void onReceive(Context context, Intent intent) %7B%0A            if (getResultCode() == RESULT_OK) %7B%0A                Toast.makeText(context, %22Send succeeded%22, Toast.LENGTH_SHORT).show();%0A            %7D else %7B%0A                Toast.makeText(context, %22Send failed.%22, Toast.LENGTH_SHORT).show();%0A            %7D%0A        %7D%0A%0A    %7D
 %0A%7D%0A

set sentIntent and listen