How to set action, category and data for intent?

25,652

Solution 1

Usually, for common tasks in Android there is a general Intent that you send on which other applications can register.
for example to share some text you would create an intent like:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

will prompt android's native share dialog on which the user can choose how he wants to share it.

Specifically for email you would do something like:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                  "mailto","[email protected]", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "This is my email subject");
startActivity(Intent.createChooser(intent, "Email"));


Other examples may be to launch the default sms application:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);         
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", getMessageBody());

Or open the phone's dialer:

Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+number));
        startActivity(intent);

You need to figure out what are the actions that you want to implement in your app and then figure out how to implement each of them.

You can find some more data here:

Solution 2

Try using category and actions in intent.

Intent mailIntent = new Intent(Intent.ACTION_SEND);
mailIntent.setType("text/plain");
mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Reporting mail");
mailIntent.putExtra(Intent.EXTRA_TEXT, "Some message");
mailIntent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
startActivity(mailIntent);

this is an example for sending email. For further details refer http://developer.android.com/guide/components/intents-common.html

Share:
25,652
Wei Yang
Author by

Wei Yang

http://publish.illinois.edu/weiyang-david/ http://web.engr.illinois.edu/~weiyang3/ http://youngwei.com/

Updated on July 09, 2022

Comments

  • Wei Yang
    Wei Yang almost 2 years

    I'm programming an Android application. I want to invoke other applications to perform certain operations(Sending emails etc.) How do I know which action and category to set for the intent? Should I look other application's intent filter? What if that application is not open source?

    Also, for the data or extra attribute, I don't know how the 3rd party application will handle my intent, so I do not know how to set the attributes. For example, I want one string as the title of the email, one string as the content of the email, and another string as the recipient, and a picture as the attachment. Can I include all these information in the intent? What if the 3rd party application don't provide any functionality to handle it?