private void sendMessage() {
    AutoCompleteTextView gPerson = (AutoCompleteTextView) findViewById(R.id.contactPerson);
    String person = gPerson.getText().toString();

    Cursor cur =
        managedQuery(
            android.provider.Contacts.People.CONTENT_URI,
            null,
            android.provider.Contacts.PeopleColumns.NAME + "='" + person + "'",
            null);

    while (cur.next()) {
      String phone =
          cur.getString(cur.getColumnIndex(android.provider.Contacts.PhonesColumns.NUMBER));
      person = phone;
    }

    cur.close();

    EditText gMessage = (EditText) findViewById(R.id.textMessage);
    String message = gMessage.getText().toString();

    SmsManager manager = SmsManager.getDefault();
    manager.sendTextMessage(person, null, message, null, null, null);

    // save the sent message for the conversation.
    MessagesDbAdapter gDbAdapter = new MessagesDbAdapter(this);
    gDbAdapter.open();

    gDbAdapter.createMessage(person, message, 1, 1, System.currentTimeMillis());
    gDbAdapter.close();

    Toast.makeText(this, "Text message has been sent.", Toast.LENGTH_LONG).show();
    finish();
  }
 // Listen for results.
 protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) {
   // See which child activity is calling us back.
   if (data != null) {
     AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.contactPerson);
     Cursor cur = managedQuery(Uri.parse(data), null, null, null);
     if (cur.next()) {
       String name =
           cur.getString(cur.getColumnIndex(android.provider.Contacts.PeopleColumns.NAME));
       textView.setText(name);
       EditText gMessage = (EditText) findViewById(R.id.textMessage);
       gMessage.requestFocus(R.id.textMessage);
     }
   }
 }
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.createmessage);

    final Button cancelButton = (Button) findViewById(R.id.createCancelBtn);
    cancelButton.setOnClickListener(
        new Button.OnClickListener() {
          public void onClick(View v) {
            finish();
          }
        });

    final Button sendButton = (Button) findViewById(R.id.createSendBtn);
    sendButton.setOnClickListener(
        new Button.OnClickListener() {
          public void onClick(View v) {
            sendMessage();
          }
        });

    ContentResolver content = getContentResolver();
    Cursor cursor =
        content.query(
            Contacts.People.CONTENT_URI,
            PEOPLE_PROJECTION,
            null,
            null,
            Contacts.People.DEFAULT_SORT_ORDER);
    ContactListAdapter adapter = new ContactListAdapter(cursor, this);

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.contactPerson);
    EditText gMessage = (EditText) findViewById(R.id.textMessage);

    Bundle extras = getIntent().getExtras();

    if (extras != null) {
      Cursor cur =
          managedQuery(
              android.provider.Contacts.People.CONTENT_URI,
              null,
              android.provider.Contacts.PhonesColumns.NUMBER
                  + "='"
                  + extras.getString("contact")
                  + "'",
              null);
      String sender = extras.getString("contact");

      while (cur.next()) {
        sender = cur.getString(cur.getColumnIndex(android.provider.Contacts.PeopleColumns.NAME));
      }
      cur.close();
      textView.setText(sender);
      gMessage.requestFocus(R.id.textMessage);
    }

    if (getIntent().getData() != null) {
      StringTokenizer dataPieces = new StringTokenizer(getIntent().getData().toString(), ":");
      String[] tokens = new String[2];

      int i = 0;
      while (dataPieces.hasMoreTokens()) {
        tokens[i] = dataPieces.nextToken();
        i++;
      }

      String sender = tokens[1];
      Cursor cur =
          managedQuery(
              android.provider.Contacts.People.CONTENT_URI,
              null,
              android.provider.Contacts.PhonesColumns.NUMBER + "='" + sender + "'",
              null);

      while (cur.next()) {
        sender = cur.getString(cur.getColumnIndex(android.provider.Contacts.PeopleColumns.NAME));
      }

      cur.close();
      textView.setText(sender);
      gMessage.requestFocus(R.id.textMessage);
    }

    textView.setThreshold(1);
    textView.setAdapter(adapter);
  }