Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set action, category and data for intent?

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?

like image 543
Wei Yang Avatar asked Apr 03 '13 14:04

Wei Yang


People also ask

Which method can be used to set the action of intent object?

The action in an Intent object can be set by the setAction() method and read by getAction().

How do I assign more data to intent?

For sending the value we will use intent. putExtra("key", Value); and during receive intent on another activity we will use intent. getStringExtra("key"); to get the intent data as String or use some other available method to get other types of data ( Integer , Boolean , etc.).

What is category in intent?

Standard categories are defined in the Intent class as CATEGORY_name constants. The name assigned here can be derived from those constants by prefixing " android. intent. category. " to the name that follows CATEGORY_ . For example, the string value for CATEGORY_LAUNCHER is " android.

What is category in intent filter?

Categories are used for implicit Intents. So, If your Activity can be started by an implicit Intent when no other specific category is assigned to activity, activity's Intent filter should include this category. (even if you have other categories in the Intent filter).


1 Answers

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:

  • Android content sharing
  • Android intents - under the various intent actions
like image 106
Inon Stelman Avatar answered Oct 18 '22 15:10

Inon Stelman