Starting app components

All apps have several components that make up the app. These can be screens, chunks of work, and listeners that wait for an event to happen somewhere.

How to do it...

There are three basic components to any app: the Activity, the Service, and the BroadcastReceiver component:

  1. The most common and most visible component is the Activity component:
    [Activity]
    public class DestinationActivity : Activity {
      protected override void OnCreate(Bundle savedState) {
        base.OnCreate(savedState);
      }
    }
  2. To start or display an activity, we simply invoke the StartActivity() method, as follows:
    var intent = new Intent(this, typeof(DestinationActivity));
    StartActivity(intent);
  3. The next component that we use is a Service, or the chunk of work:
    [Service]
    public class DestinationService : IntentService {
      protected override void OnHandleIntent(Intent intent) {
      }
    }
  4. Similar to activities, we start a Service using the StartService() method:
    var intent = new Intent(this, typeof(DestinationService));
    StartService(intent);
  5. Finally, there is the BroadcastReceiver, which is a listener for global events:
    [BroadcastReceiver]
    public class DestinationReceiver : BroadcastReceiver {
      public override void OnReceive(
      Context context, Intent intent) {
      }
    }
  6. The broadcast receiver can be started or reached using the SendBroadcast() method, as follows:
    var intent = new Intent(this, typeof(DestinationReceiver));
    SendBroadcast(intent);

Data can be passed to each of the components using the intents that are used to start them:

  1. To attach data to an Intent instance, we make use of the extras feature:
    intent.PutExtra("SomeKey", "Some String Value");
    intent.PutExtra("AnotherKey", 123);
  2. Once in a component, we can access the data using the various getter methods:
    if (Intent.HasExtra("SomeKey")) {
      var text = Intent.GetStringExtra("SomeKey");
    }
    if (Intent.HasExtra("AnotherKey")) {
      var integer = Intent.GetIntExtra("AnotherKey");
    }

How it works...

Each of the three main app components is used for very specific tasks. The Activity represents a single screen in an app. The activities are loosely bound to each other to create the visual aspect of an app. The Service represents a long-running, background task that can still run when the activities are terminated. The BroadcastReceiver is registered with the system and then waits for a specific event or message to be sent out.

Tip

Usually, a BroadcastReceiver will simply start a Service, which does the actual work.

Activities, services, and broadcast receivers are registered with the Android system using various attributes. Activities require the [Activity] attribute, services require the [Service] attribute, and broadcast receivers require the [BroadcastReceiver] attribute.

Tip

Attributes can be applied to the app components instead of manually updating the AndroidManifest.xml file.

Each component can be started directly using various methods found on the Context type. Activities are displayed using the StartActivity() method. However, if a result is desired from an activity, we use the StartActivityForResult() method instead. Services are started using the StartService() method. Broadcast receivers are initiated from either the SendBroadcast(), SendOrderedBroadcast(), or SendStickyBroadcast() methods.

When starting any app component, we provide an Intent instance. This intent holds the information that lets the Android system know which component to start and what data is required for it to start.

Tip

An Intent instance can hold any extra data that needs to be passed to the app component that is going to be started.

In order to pass data into a component when it is started, we add the data to the intent. Data is added using the extras mechanism. Each piece of simple data is added as a key-value pair through the PutExtra() method. The data is usually primitive data types or arrays of primitive data types, but there is the option to add IParcelable objects such as another Bundle instance.

When we wish to retrieve data from the intent, we make use of the getter methods. These methods are named in the form Get<Type>Extra. For example, to obtain a string or an integer, we use the GetStringExtra() or GetIntExtra() methods respectively.

There's more...

When intents specify the exact type of the component to start, we call them explicit intents. These intents are usually used to start components within the app.

If we want to launch a component in another app, we cannot use explicit intents. This is because we may not know the fully-qualified name of the component or the user may not have installed the particular app. To get around this, we use implicit intents. These intents specify a general action and then hand it off to the system. This allows either the user, or the system, to specify a particular app to use with that action.

Note

Explicit intents are created using a specific type to start a specific component. Implicit intents are created to initiate a general action.

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

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