ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Mobile Application Development 1
    COMP4124
    Progress0 / 33 topics
    Topics
    1. Mobiles Application Development Platform2. HTML5 for Mobiles3. Android OS: Architecture, Framework and Application Development4. iOS: Architecture, Framework5. Application Development with Windows Mobile6. Eclipse7. Fragments8. Calling Built-in Applications using Intents9. Displaying Notifications10. Components of a Screen11. Adapting to Display Orientation12. Managing Changes to Screen Orientation13. Utilizing the Action Bar14. Creating the User Interface15. Listening for UI Notifications16. Views17. User Preferences18. Persisting & Sharing Data19. Sending SMS Messages20. Getting Feedback21. Sending E-mail22. Displaying Maps23. Consuming Web Services Using HTTP24. Web Services: Accessing and Creating25. Threading26. Publishing Android Applications27. Deployment on App Stores28. Mobile Programming Languages29. Challenges with Mobility and Wireless Communication30. Location-aware Applications31. Performance/Power Tradeoffs32. Mobile Platform Constraints33. Emerging Technologies
    COMP4124›Calling Built-in Applications using Intents
    Mobile Application Development 1Topic 8 of 33

    Calling Built-in Applications using Intents

    6 minread
    1,078words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time6 min
      Word count1,078
      Code examples0
      DifficultyIntermediate