Message intent

  • Replies:4
  • Answered
Linu S.
  • Forum posts: 77

Aug 11, 2016, 1:56:08 PM via Website

I need to send a message from my app to phone contact.
By using the following code i can pick the phone contact,
but message can't be send.. any threads??

Uri uri = Uri.parse("content://contacts");
Intent it = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
it.putExtra("sms_body", "The SMS text");
startActivity(it);

???

Reply
van hoa
  • Forum posts: 27

Aug 11, 2016, 5:44:52 PM via Website

u want to send sms programmatically or send though message application?
You can send sms programmatically in onActivityResult after choosing contact by code

try {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phone, null, content, null, null);
} catch (Exception e) {
    Toast.makeText(context, "error: "+e.getMessage(), Toast.LENGTH_SHORT).show();
}

Linu S.

Reply
Linu S.
  • Forum posts: 77

Aug 12, 2016, 7:15:45 AM via Website

I need to send through message application..

Reply
van hoa
  • Forum posts: 27

Aug 12, 2016, 8:45:52 PM via Website

put code below in onActivityResult

switch (requestCode) {
        case (PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {
                Uri uri = data.getData();
                String[] projection = { ContactsContract.CommonDataKinds.Phone.NUMBER};
                Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
                if(cursor.moveToFirst()) {
                    int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    String number = cursor.getString(numberColumnIndex);
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + number));
                    intent.putExtra("sms_body", "content");
                    startActivity(intent);
                }
                cursor.close();
            }
            break;
    }

— modified on Aug 12, 2016, 8:46:52 PM

Linu S.

Reply
Jackie Chen
  • Forum posts: 16

Sep 7, 2016, 5:31:01 AM via Website

Perhaps you should set your MIME_Type, try it.setType("text/plain");

Linu S.

Reply