Professional Documents
Culture Documents
DEVELOPMENT
Working with Intents
Session 5
Although, the Android platform guarantees that certain intents will resolve to one of the
built-in apps (such as the Phone, Email, or Calendar app), you should always include a
verification step before invoking an intent.
Example:
If isIntentSafe is true, then at least one app will respond to the intent.
If isIntentSafe is false, then there aren't any apps to handle the intent.
*Note: You should check this before firing an Intent because if there would not be any
app installed in the users phone, then you might need to disable the functionality or if
you know the application which can perform that task, you can refer the user to
download that app for completing this action
For example, your app can start a camera app and receive the captured photo as
a result. Or, you might start the People app in order for the user to select a
contact and you'll receive the contact details as a result.
1. Activity
2. Services
3. ContentProvider
4. BroadcastReceiver
RESULT_CANCELED if the user backed out or the operation failed for some
reason
An Intent that carries the result data
ACTION_MAIN
ACTION_VIEWACTION_EDIT
ACTION_PICK
ACTION_CHOOSER
ACTION_DIAL
ACTION_CALL
ACTION_SEND
ACTION_SENDTO
ACTION_ANSWER
ACTION_SEARCH
ACTION_WEB_SEARCH
Bundle object is used to pass data between activities. You can use the putExtras
method to associate a bundle with an Intent.
Lets see the example code:
The code mentioned below describes as to how we collect data into the
Bundle object
1. True
2. False
This data can be of different types: String, Char, Boolean, Bundle, etc
This data gets set as the key value pair, which you can retrieve in the called activity
Example:
The code below describes as to how we collect data into the Intent object
What if I am trying to receive a parameter from the bundle, which I didnt send in the
previous activity ?
If you instead give the foreign application a PendingIntent you created using
your own permission, that application will execute the contained Intent using your
application's permission
If your app can perform an action that might be useful to another app, your app
should be prepared to respond to action requests from other apps
To allow other apps to start your activity, you need to add an <intent-
filter> element in your manifest file for the corresponding <activity> element
If your app will be installed on the users phone, then the system will identify
which activity can respond to the corresponding action and your app is compatible
then it will be listed in AppChooser for that implicit intent
In order to properly define which intents your activity can handle, each intent filter you
add should be as specific as possible in terms of the type of action and data the activity
accepts
The system may send a given Intent to an activity if that activity has an intent filter
which fulfills the following criteria of the Intent object:
Example:
Note: Each incoming intent specifies only one action and one data type, but it's OK
to declare multiple instances of the <action>, <category>, and <data> elements
in each <intent-filter>
If you want to return a result to the activity that invoked yours, simply call setResult() to
specify the result code and result Intent
When your operation is done and the user should return to the original activity,
call finish() to close (and destroy) your activity.
Example:
Can my application be called by any other applications? If yes, What kind of intent will it
be?