ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP4124Calling Builtin Apps I…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP4124

    Mobile Application Development 1

    Progress0 / 33 done
    Topics33
    1Mobiles Application Development Platform2HTML5 for Mobiles
    3Architecture, Framework and Application Development
    4Architecture, Framework5Application Development with Windows Mobile6Eclipse7Fragments8Calling Built-in Applications using Intents9Displaying Notifications10Components of a Screen11Adapting to Display Orientation12Managing Changes to Screen Orientation13Utilizing the Action Bar14Creating the User Interface15Listening for UI Notifications16Views17User Preferences18Persisting & Sharing Data19Sending SMS Messages20Getting Feedback21Sending E-mail22Displaying Maps23Consuming Web Services Using HTTP
    24Accessing and Creating25Threading26Publishing Android Applications27Deployment on App Stores28Mobile Programming Languages29Challenges with Mobility and Wireless Communication30Location-aware Applications31Performance/Power Tradeoffs32Mobile Platform Constraints33Emerging Technologies
    6 min read
    Topic 8 of 33
    Topic Study Note Illustration
    08

    Calling Built-in Applications using Intents

    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...

    Calling Built-in Applications using Intents in Android

    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.


    1. What is an Intent?

    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:

    • Launch an activity (e.g., opening a new screen).
    • Start a service (e.g., downloading a file in the background).
    • Send a broadcast (e.g., notifying other apps about a change).

    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).


    2. Types of Intents

    • 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);
      

    3. Calling Built-in Applications with Intents

    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.

    A. Opening the Camera App

    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().

    B. Sending an SMS

    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.

    C. Calling a Phone Number

    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.

    D. Opening the Web Browser

    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.

    E. Opening the Contacts App

    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.

    F. Sharing Content via Other Apps

    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.

    4. Handling Intent Results

    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
        }
    }
    

    5. Permissions and Security

    When calling certain built-in applications (like dialing a number or sending a message), your app may need specific permissions. For example:

    • Sending SMS: Requires the SEND_SMS permission.
    • Making a call: Requires the 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);
    }
    

    6. Conclusion

    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.

    ← Previous topic 7
    Fragments
    Next topic 9 →
    Displaying Notifications

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    6mremaining
    1.1kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Mobile Application Development 1