package com.samples.telephony.sendsms;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SendSmsActivity extends Activity implements OnClickListener {
	
    private EditText textSms;
    private EditText textNumber;
    private Button bSend;
    private SmsManager manager;
    private PendingIntent intent;
    
    @Override
    public void onCreate(final Bundle icicle) {

        super.onCreate(icicle);
        this.setContentView(R.layout.main);

        textNumber = (EditText) findViewById(R.id.number);
        textSms = (EditText) findViewById(R.id.text);
        bSend = (Button) findViewById(R.id.bSend);

        manager = SmsManager.getDefault();
        

        bSend.setOnClickListener(this);
    }

	@Override
	public void onClick(View arg0) {
        String dest = textNumber.getText().toString();
        
        try {
            if (PhoneNumberUtils.isWellFormedSmsAddress(dest)) {
            	intent = PendingIntent.getActivity(
                    this, 0, new Intent(this, SendSmsActivity.class), 0);
            	
                manager.sendTextMessage("5556", null, 
            		textSms.getText().toString(), intent, null);

                Toast.makeText(SendSmsActivity.this, 
                    "SMS sent", Toast.LENGTH_LONG).show();
        	}
            else {
            	Toast.makeText(SendSmsActivity.this, 
            		"Error formed SMS adress", Toast.LENGTH_LONG).show();
            }    
        }
        catch (Exception e) {
            Toast.makeText(SendSmsActivity.this, e.toString(), 
            		Toast.LENGTH_LONG).show();
        }
	}
}