miércoles, 22 de mayo de 2013

Android Components

Android components
    - Activity
    - Service
    - Content Provider
    - Broadcast Receiver



Application components are the essential building blocks of an Android application. Each component is a different point through which the system can enter your application. Not all components are actual entry points for the user and some depend on each other, but each one exists as its own entity and plays a specific role—each one is a unique building block that helps define your application's overall behavior.

There are four different types of application components. Each type serves a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.

Here are the four types of application components:

ACTIVITIES [User Interaction]
    Activities are components that interact with the user and represents a single screen with a user interface, an activity is a single, focused thing that the user can do. For instance, an instant message application might have one activity that shows the list of contacts, another the conversation detail, and another activity for sending emails. Although all of them work together in an application, each one is independet of the others. As such, a different application could start any one of these activities.




  
 An activity is implemented as a subclass of Activity.

 public class MyActivity extends Activity {
     ...
 }


public class Activity extends ApplicationContext {

     protected void onCreate(Bundle savedInstanceState);

     protected void onStart();
    
     protected void onRestart();

     protected void onResume();

     protected void onPause();

     protected void onStop();

     protected void onDestroy();
 }



Declaring the activity in the manifest
<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...
</manifest >


Starting activity
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);


Android developers -> Activity
http://developer.android.com/reference/android/app/Activity.html
http://developer.android.com/guide/components/activities.html


SERVICES [Service Provider]
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. A service does not provide a user interface. For instance, a service might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.

Because it is sometime misundestood let's go to show what it is not
    - A Service is not a separate process.
    - A Service is not a thread.

Traditionally, there are two classes you can extend to create a started service:
    Service

        IntentService (This is a subclass of Service that uses a worker thread to handle all start requests, one at a time)

Declaring a service in the manifest
<manifest ... >
  ...
  <application ... >
      <service android:name=".ExampleService" />
      ...
  </application>
</manifest>


Starting a Service
    Intent intent = new Intent(this, HelloService.class);
    startService(intent);


Android developers -> Service
http://developer.android.com/reference/android/app/Service.html
http://developer.android.com/guide/components/services.html


CONTENT PROVIDERS [Data Provider]
Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process. Application could store the data in the file system, an SQLite database, on the web, or any other persistent storage location your application can access. Through the content provider, other applications can query or even modify the data.

    A content provider is implemented as a subclass of ContentProvider and must implement a standard set of APIs that enable other applications to perform transactions.
public class ExampleProvider extends ContentProvider {
...
 public Cursor query(
        Uri uri,
        String[] projection,
        String selection,
        String[] selectionArgs,
        String sortOrder) {
...
}



Android developers -> Content Provider
http://developer.android.com/guide/topics/providers/content-providers.html
http://developer.android.com/reference/android/content/ContentProvider.html


BROADCAST RECEIVER [System Event Listener]

A unique aspect of the Android system design is that any application can start another application’s component. For example, if you want the user to capture a photo with the device camera, there's probably another application that does that and your application can use it, instead of developing an activity to capture a photo yourself. You don't need to incorporate or even link to the code from the camera application. Instead, you can simply start the activity in the camera application that captures a photo. When complete, the photo is even returned to your application so you can use it. To the user, it seems as if the camera is actually a part of your application.

Because the system runs each application in a separate process with file permissions that restrict access to other applications, your application cannot directly activate a component from another application. The Android system, however, can. So, to activate a component in another application, you must deliver a message to the system that specifies your intent to start a particular component. The system then activates the component for you.

Broadcast Receivers enable reception of intents from the system or other applications without running other activities or services of the application.

A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object.

Android developers -> Broadcast Receiver
http://developer.android.com/reference/android/content/BroadcastReceiver.html


No hay comentarios:

Publicar un comentario