0% found this document useful (0 votes)
44 views4 pages

Calling Built-In Applications Using Intents

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

Calling Built-In Applications Using Intents

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Mobile Application Development

Topic: Calling Built-In Applications Using Intents

1. Key Definitions of Concepts


• Intent:
An Intent is a messaging object in Android used to request an action from another app or
system component.
Example: Launching camera, opening the dialer, sending an email.
• Explicit Intent:
Specifies the exact app/activity to be launched (e.g., navigating from one activity to another
within the same app).
• Implicit Intent:
Declares an action to perform, and Android finds the best app that can handle it.
Example: Send an email → OS opens the installed email app.
• Built-In Applications:
Pre-installed apps like Camera, Phone Dialer, SMS, Email, Maps, Browser.

2. Step-by-Step Explanation with Code Examples


How to Call Built-In Apps with Intents

(a) Opening the Phone Dialer

Intent intent = new Intent(Intent.ACTION_DIAL);


intent.setData(Uri.parse("tel:123456789"));
startActivity(intent);

• ACTION_DIAL opens the dialer with the number pre-filled.

(b) Making a Direct Phone Call (Needs Permission)

Intent intent = new Intent(Intent.ACTION_CALL);


intent.setData(Uri.parse("tel:123456789"));
startActivity(intent);

• Requires permission in AndroidManifest.xml:

<uses-permission android:name="android.permission.CALL_PHONE"/>
(c) Sending an SMS

Intent intent = new Intent(Intent.ACTION_VIEW);


intent.setData(Uri.parse("sms:123456789"));
intent.putExtra("sms_body", "Hello, this is a test message.");
startActivity(intent);

• Opens default SMS app with the number and message body pre-filled.
• Suppose you have an EditText for number and message

EditText numberInput = findViewById(R.id.editTextNumber);


EditText messageInput = findViewById(R.id.editTextMessage);

String phoneNumber = numberInput.getText().toString();


String message = messageInput.getText().toString();

Intent intent = new Intent(Intent.ACTION_VIEW);


intent.setData(Uri.parse("sms:" + phoneNumber));
intent.putExtra("sms_body", message);
startActivity(intent);

(d) Sending an Email

Intent emailIntent = new Intent(Intent.ACTION_SEND);


emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email body text");
startActivity(Intent.createChooser(emailIntent, "Choose an Email app:"));

(e) Opening a Web Page

Intent browserIntent = new Intent(Intent.ACTION_VIEW,


Uri.parse("https://www.google.com"));
startActivity(browserIntent);

(f) Launching Camera to Capture Photo

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


startActivity(cameraIntent);

• Opens the camera app to capture an image.

(g) Displaying a Location in Maps

Uri location = Uri.parse("geo:0,0?q=Lahore+Pakistan");


Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

3. Real-Life Practical Scenarios


• Food Delivery Apps (e.g., Foodpanda): Use intents to open Google Maps for delivery tracking.
• Ride-Hailing Apps (e.g., Careem/Uber): Use intents to call drivers or open maps.
• E-Commerce Apps (e.g., Daraz): Open email app for sending feedback.
• Contact Apps: Use intents to directly open phone dialer or send SMS.

4. Best Practices, Advantages, and Pitfalls


Best Practices:

• Use Intent.createChooser() for implicit intents, so the user can select their preferred app.
• Always check if there’s an app available to handle the intent:

if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}

• Request runtime permissions for sensitive actions (e.g., CALL_PHONE, camera).

Advantages:

• Reuse built-in app functionality instead of reinventing features.


• Enhances user experience by integrating with familiar apps.

Common Pitfalls:

• Forgetting to declare necessary permissions in AndroidManifest.xml.


• Crashes if no app is available to handle the intent.
• Not handling runtime permission requests properly (for Android 6.0+).

5. Summary Table for Quick Revision


Action Intent Code Snippet
Open dialer ACTION_DIAL with tel: URI

Make call ACTION_CALL with permission

Send SMS ACTION_VIEW with sms: URI

Send Email ACTION_SEND with MIME type message/rfc822

Open browser ACTION_VIEW with URL


Capture photo MediaStore.ACTION_IMAGE_CAPTURE

Show location in Google Maps ACTION_VIEW with geo: URI

Conclusion
• Intents are a powerful way to interact with built-in apps and other apps on the device.
• Using implicit intents makes apps more flexible and user-friendly.
• Always handle permissions and availability checks to avoid crashes.

You might also like