Opening external applications

In this recipe, we will create and explain all the code that is necessary to open external applications with Intents.

Getting ready

Create a new solution named Intent_Project and open the project of the same name that Xamarin Studio has created for you.

How to do it...

  1. Add the following code in the OnCreate() method to the MainActivity class so that it looks like the following:
    using System;
    
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    
    namespace intent_project {
      [Activity (Label = "intent_project", MainLauncher = true)]
      public class MainActivity : Activity {
        protected override void OnCreate (Bundle bundle) {
          base.OnCreate (bundle);
    
          var geoUri = Android.Net.Uri.Parse ("geo:37.8143849,-122.4719223");
          var mapIntent = new Intent (Intent.ActionView, geoUri);
          StartActivity (mapIntent);
    
        }
      }
    }
  2. Run your application.
  3. If you're running your code on the emulator, then the following exception should appear:
    How to do it...

    Note

    Note that this exception won't occur if you have an application that can handle the Intent on your emulator. However, it is unlikely unless you have installed additional applications.

  4. If you're on a real device, the Maps application should open as follows:
    How to do it...

How it works...

First of all, let's take a look at the little code snippet that we've written in the previous section:

var geoUri = Android.Net.Uri.Parse ("geo:37.8143849,-122.4719223");
  var mapIntent = new Intent (Intent.ActionView, geoUri);
  StartActivity (mapIntent);

With this code, we created an URI out of "geo:37.8143849,-122.4719223" and then an Intent with two arguments: Intent.ActionView and the geoUri. Finally, we started an Activity and sent the intent. Here, we asked the Android OS to perform a view operation of a URI containing the geographic coordinates. The OS is responsible for mapping this request to the appropriate application: Google Maps or any application capable of handling coordinates. However, if no application can handle what we ask, then the application we have created will yield an exception. On one hand, we abstractly describe what we expect from the OS and what action we intend to take, but on the other hand, this abstraction has some limitations as we are not sure that an app can perform this operation.

Tip

If several applications are able to handle the request, the user will be prompted with a dialog to choose which one they want to use, as displayed in the following example screenshot where several applications can open a PDF attachment:

How it works...

In the same fashion as the ActionView element, the Intent class proposes (at the time of writing this), 114 other actions that you can use. The most common actions are as follows:

  • CALL: This performs a call to the contact that is specified in the data.
  • CALL_BUTTON: The user can press the Call button to go to the dialer or other appropriate UI for placing a call
  • EDIT: This provides explicit editable access to the given data.
  • OPEN_DOCUMENT: This allows the user to select and return one or more existing documents
  • DIAL: This dials a number as specified by the data
  • GET_CONTENT: This allows the user to select a particular kind of data and return it
  • SEND: This delivers some data to someone
  • SENDTO: This sends a message to the contact specified in the data
  • VIEW: This displays the data to the user
  • WEB_SEARCH: This performs a web search

The way in which the OS actually handles the intents requests, fold in the Android system programming and isn't within the scope of this book

There's more...

Let's look at how to dial a number and open a web page using Intents.

Dialing a number

var phoneURI = Android.Net.Uri.Parse ("tel:5147778888");

var phoneIntent = new Intent (Intent.ActionView, phoneURI);

StartActivity (phoneIntent);

In the previous code sample, I renamed the variable name to keep the code coherent, but the only true change is the string passed to Android.Net.Uri.Parse, which we changed from "geo:37.8143849,-122.4719223" to "tel:5147778888". Pretty straightforward, isn't it?

Nonetheless, dialing on behalf of the user requires the CALL_PHONE permission. In order to allow your application to do so, tick the CallPhone checkbox on the Properties/AndroidManifest.xml file as shown in the following screenshot:

Dialing a number

Next, run your application. You will see the following:

Dialing a number

Note

It is noteworthy that the back button of the phone will redirect the user toward the origin of the intent action—your application.

Opening a web page

In order to open a web page using intents, we will have to do the following:

var uri = Android.Net.Uri.Parse ("https://www.packtpub.com/");
var intent = new Intent (Intent.ActionView, uri);
StartActivity (intent);

On successful completion, the following should appear on your emulator:

Opening a web page

In conclusion, pass a string that can be parsed into a URI and the OS will do its best to find a matching application.

See also

Refer to the next recipe to see how to use an Intent to enhance our applications instead of redirecting toward another application. Also, refer to the following URLs for more information:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.45.162