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.
There are three basic components to any app: the Activity
, the Service
, and the BroadcastReceiver
component:
Activity
component:[Activity] public class DestinationActivity : Activity { protected override void OnCreate(Bundle savedState) { base.OnCreate(savedState); } }
StartActivity()
method, as follows:var intent = new Intent(this, typeof(DestinationActivity)); StartActivity(intent);
Service
, or the chunk of work:[Service] public class DestinationService : IntentService { protected override void OnHandleIntent(Intent intent) { } }
Service
using the StartService()
method:var intent = new Intent(this, typeof(DestinationService)); StartService(intent);
BroadcastReceiver
, which is a listener for global events:[BroadcastReceiver] public class DestinationReceiver : BroadcastReceiver { public override void OnReceive( Context context, Intent intent) { } }
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:
Intent
instance, we make use of the extras feature:intent.PutExtra("SomeKey", "Some String Value"); intent.PutExtra("AnotherKey", 123);
if (Intent.HasExtra("SomeKey")) { var text = Intent.GetStringExtra("SomeKey"); } if (Intent.HasExtra("AnotherKey")) { var integer = Intent.GetIntExtra("AnotherKey"); }
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.
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.
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.
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.
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.