In Android development, Intents are a powerful mechanism for inter-component communication. They allow components of an application (like activities, services, and broadcast receivers) to communicate with each other, even across different applications. One of the most common uses of Intents is to invoke or launch built-in applications that come with Android, such as the Camera, Contacts, Messaging, Browser, and more.
When you want to call a built-in Android application (e.g., open the camera app, send a message, or open a webpage), you use an Intent to request that specific action. Intents abstract the underlying details of the action, allowing you to leverage built-in Android applications seamlessly.
An Intent is an object in Android that represents an application’s desire to perform an action. It provides a description of the operation to be performed and can be used to:
Intents can be explicit (targeting a specific component, such as an activity) or implicit (requesting an action from any app that can handle the request, such as opening a URL).
Explicit Intents: Used when you know the target component (e.g., a specific activity or service in your app).
Example: Launching a specific activity in your app:
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
Implicit Intents: Used when you don't know the target component but specify the action or category you want to perform (such as opening a webpage or sending an SMS). The system will choose the appropriate app or activity that can handle the intent.
Example: Opening a URL in a web browser:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(intent);
Android provides several built-in intents that allow you to interact with system applications. These intents are predefined and can be used to call common actions that are handled by Android's built-in apps.
To launch the camera app using an Intent, you use an implicit intent with the action MediaStore.ACTION_IMAGE_CAPTURE. This action tells Android that you want to take a picture.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CODE_CAMERA);
MediaStore.ACTION_IMAGE_CAPTURE: Opens the default camera app.startActivityForResult(): This method is used when you expect a result from the camera (e.g., the image taken). You will handle the result in onActivityResult().You can use an implicit intent to open the default messaging app and send an SMS. The intent should specify the action Intent.ACTION_VIEW and a data URI with the SMS protocol.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("sms:1234567890"));
intent.putExtra("sms_body", "Hello, this is a test message.");
startActivity(intent);
sms: URI scheme: Tells the system you want to send an SMS to the phone number 1234567890.putExtra(): Adds additional data, like the body of the SMS.You can open the dialer application with a specific phone number by using the ACTION_DIAL intent.
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:1234567890"));
startActivity(intent);
Intent.ACTION_DIAL: Opens the phone dialer app with the specified number, but it doesn’t automatically dial the number.If you want to make a call directly, you can use Intent.ACTION_CALL (but be sure to request permission for making calls):
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:1234567890"));
startActivity(intent);
ACTION_CALL: Directly dials the number.To open a webpage, you can use the Intent.ACTION_VIEW action along with a URI representing the URL you want to visit.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(intent);
Intent.ACTION_VIEW: Informs the system you want to view a resource (in this case, a webpage).Uri.parse("http://www.example.com"): The URL you want to open.To open the contacts app (where the user can select a contact), you can use the Intent.ACTION_PICK action with the ContactsContract.Contacts.CONTENT_URI.
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_PICK_CONTACT);
Intent.ACTION_PICK: Tells the system that you want to pick a contact.ContactsContract.Contacts.CONTENT_URI: The URI that refers to the contacts content provider.Android provides a sharing mechanism, allowing you to share content (text, images, etc.) with other apps installed on the device.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Hello, I'm sharing this text!");
startActivity(Intent.createChooser(intent, "Share with"));
Intent.ACTION_SEND: Specifies that you want to share content.setType("text/plain"): Defines the type of content being shared (plain text in this case).Intent.createChooser(): Opens a dialog that lets the user choose the app to share the content with.Sometimes you might launch an intent to perform an action and expect a result. For example, when using the camera or opening contacts, you want to get some data back (like the photo taken or the contact selected). This is done by calling startActivityForResult() and overriding onActivityResult().
Example with Camera:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
// Use the photo
}
}
When calling certain built-in applications (like dialing a number or sending a message), your app may need specific permissions. For example:
SEND_SMS permission.CALL_PHONE permission.You need to add these permissions to your app's AndroidManifest.xml and handle runtime permission requests for newer Android versions (Android 6.0 and above).
Example (in AndroidManifest.xml):
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
Example (for runtime permission):
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE_PERMISSION);
}
Using Intents to call built-in applications in Android allows you to leverage Android's powerful system applications with ease. You can launch the camera, send SMS messages, make calls, view web pages, and much more, all with simple and declarative code.
Intents help in creating a more flexible and interactive user experience by allowing your app to interact with other apps and system functionalities. Just remember to always check for the required permissions and handle runtime permissions properly for a smooth user experience.
Open this section to load past papers