Thursday, 24 May 2018

Droid implicit activities

Explicit activities are intents that are within your package (.apk) and specifically identified, whereas implicit Activities do not specify the activity, but instead specify the operation you want completed and let's ART decide which activity to start; thing like

  • Dial Action: dial a number
  • Airplane mode action
  • View action: launch browser, show location, play music
  • Send to action: send email, text message,
  • Pick action: image, contact
  • etc

Typically implicit activities are used to launch activities outside of your package, but they can also launch activities inside your package. Implicit intents require several things to accomplish their task:

  • Action: the thing you want done
  • Data: the data you want passed to the activity
  • MIME Type: they type of information ART is going to have to operate on
  • Categories: Style of activity you want
  • Extras: more data to operate on

Not every field above is required, often times action and data are all you need.

Some actions are obvious as to which activity a user needs, for example the DialAction or ActionImageCapture, however others like the ViewAction are not and need a piece of data to help art route them appropriately.

Taking a photo

private void PhotoButton_Click(object sender, EventArgs e)
{
    var intent = new Intent();
    intent.SetAction(MediaStore.ActionImageCapture);

    if (intent.ResolveActivity(PackageManager) != null)
        base.StartActivity(intent);

}

Notice that since taking a photo is a trivial Action and all it needs to have is its action set and the activity will be correctly routed.

Showing a location is a dad more complicated.

private void GeoButton_Click(object sender, EventArgs e)
{
    var intent = new Intent();
    intent.SetAction(Intent.ActionView);
    intent.SetData(Android.Net.Uri.Parse("geo:46.5215962,6.6380333?z=17"));

    if (intent.ResolveActivity(PackageManager) != null)
        base.StartActivity(intent);

}

since the "View" action is more ambiguous it needs to be coupled with data that helps ART route your implicit activity correctly to the map activity.


Calling  a phone

private void PhoneButton_Click(object sender, EventArgs e)
{
    var intent = new Intent();
    intent.SetData(Android.Net.Uri.Parse("tel:(855) 926-2746"));

    if (intent.ResolveActivity(PackageManager) != null)
        base.StartActivity(intent);
}


again a simple action that all it needs is the data, the tel protocol lets ART know you want a dialer.

Sending an email

private void EmailButton_Click(object sender, EventArgs e)
{
    var intent = new Intent();
    intent.SetAction(Intent.ActionSendto);
    if (intent.ResolveActivity(PackageManager) != null)
    {
        intent.SetData(Android.Net.Uri.Parse("mailto:"));
        intent.PutExtra(Intent.ExtraEmail, new[] { "pawel.ciucias@fake.com" });
        intent.PutExtra(Intent.ExtraSubject, "Heyoh!!");

        base.StartActivity(intent);
    }

}

So to wrap up implicit intents set the the intent then call the start activity without specifying the activity and they allow ART to route them to the appropriate activity.