top of page

Android - Creating Intents: Text Messaging

Hello all,

I really enjoyed the Udacity Android Basics by Google lesson Practice with Intents so I am taking some time to fully explore each of the intents in the Android Common Intents documentation. The one I have most recently looked at is Messaging.

First I just created a simple button to activate the intent which looks like this:

Android App page showing a single TEXT MESSAGE button.

And the code in activity_main.xml looks like this:

<Button

android:id="@+id/messaging"

android:layout_width="@dimen/button_width"

android:layout_height="@dimen/button_height"

android:layout_gravity="center"

android:layout_margin="@dimen/margin"

android:onClick="sendMessage"

android:text=" Text Message"/>

Then I created an activity in MainActivity.java to call the intent when the button is clicked. The following code gets you to the Text Messaging App with the message “Hello World” and the number you want to send it to pre-inputted.

public void sendMessage(View view) {

Intent intent = new Intent(Intent.ACTION_SENDTO);

intent.setData(Uri.parse("smsto:012345678910"));

intent.putExtra("sms_body", "Hello World");

if (intent.resolveActivity(getPackageManager()) != null) {

startActivity(intent);

}

}

Unfortunately, that is as far as I got with this intent. I tried to attach an image to the Text Message but I couldn’t get it to work. The closest I got was this block of code:

public void sendMessage(View view) {

Uri imageFileToSend = Uri.parse("file://storage/external_SD/DCIM/Camera/20140401_221900.jpg");

Intent intent = new Intent(Intent.ACTION_SEND);

intent.putExtra("address","012345678910");

intent.putExtra("sms_body", "Hello World");

intent.putExtra(Intent.EXTRA_STREAM,

imageFileToSend); intent.setType("image/jpg");

if (intent.resolveActivity(getPackageManager()) != null) {

startActivity(intent);

}

}

However, for me, this activated the Text Messaging App with the message and number included but a pop-up told me that the “File could not be attached.”

I welcome any comments or questions about this, especially corrections of anything I can improve. For example, can you get the intent to successfully attach an image?

Links:


Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • LinkedIn Social Icon
  • Pinterest Social Icon
  • Facebook Basic Square

© 2023 by Coming Soon

Proudly created with Wix.com

bottom of page