Tuesday, December 7, 2010

Work With Android

How Android applications work :

When the user runs an application, Android starts it and brings it to the foreground. From that application, the user might invoke another application, or another screen in the same application, and then another and another. All these programs and screens are recorded on the application stack by the system’s Activity Manager. At any time, the user can press the Back button to return to the previous screen on the stack. Each user interface screen is represented by an Activity class . Each activity has its own life cycle.
In Android, an application can be “alive” even if its process has been killed , ie. the activity life cycle is not tied to the process life cycle. Processes are just disposable containers for activities
Lets take a close look

During lifetime, each activity of an Android program can be in one of several states as shown

We override these these methods in our Activity class, and Android will call them at the appropriate time

    • onCreate(Bundle): This is called when the activity first starts up.

    • onStart( ): This indicates the activity is about to be displayed to the user

    • onResume( ): This is called when your activity can start interacting with the user. This is a good place to start animations and music.

    • onPause( ): This runs when the activity is about to go into the background, usually because another activity has been launched in front of it. This is where we should save our program’s persistent
      state, such as a database record being edited.

    • onStop( ): This is called when our activity is no longer visible to the user and it won’t be needed for a while. If memory is tight, onStop( ) may never be called (the system may simply terminate our process)

    • onRestart( ): If this method is called, it indicates our activity is being redisplayed to the user from a stopped state.

    • onDestroy( ): This is called right before our activity is destroyed. If memory is tight, onDestroy( ) may never be called (the system may simply terminate your process).

    • onSaveInstanceState(Bundle): Android will call this method to allow the activity to save per-instance state, such as a cursor position within a text field. Usually we won’t need to override it because the default implementation saves the state for all our user interface controls automatically.

    • onRestoreInstanceState(Bundle): This is called when the activity is being reinitialized from a state previously saved by the onSave-InstanceState( ) method. The default implementation restores the state of our user interface.

Basic Building Blocks Of an Android Application

Source: Google

Google wrote:
Anatomy of an Android Application

There are four building blocks to an Android application:

  • Activity

  • Intent Receiver

  • Service

  • Content Provider

Not every application needs to have all four, but your application will be written with some combination of these.

Once you have decided what components you need for your application, you should list them in a file called AndroidManifest.xml. This is an XML file where you declare the components of your application and what their capabilities and requirements are. See the Android manifest file documentation for complete details.

Activities are the most common of the four Android building blocks. An activity is usually a single screen in your application. Each activity is implemented as a single class that extends the Activity base class. Your class will display a user interface composed of Views and respond to events. Most applications consist of multiple screens. For example, a text messaging application might have one screen that shows a list of contacts to send messages to, a second screen to write the message to the chosen contact, and other screens to review old messages or change settings. Each of these screens would be implemented as an activity. Moving to another screen is accomplished by a starting a new activity. In some cases an activity may return a value to the previous activity -- for example an activity that lets the user pick a photo would return the chosen photo to the caller.

When a new screen opens, the previous screen is paused and put onto a history stack. The user can navigate backward through previously opened screens in the history. Screens can also choose to be removed from the history stack when it would be inappropriate for them to remain. Android retains history stacks for each application launched from the home screen.

Intent and Intent Filters
Android uses a special class called an Intent to move from screen to screen. An intent describes what an application wants done. The two most important parts of the intent data structure are the action and the data to act upon. Typical values for action are MAIN (the front door of the activity), VIEW, PICK, EDIT, etc. The data is expressed as a URI. For example, to view contact information for a person, you would create an intent with the VIEW action and the data set to a URI representing that person.

There is a related class called an IntentFilter. While an intent is effectively a request to do something, an intent filter is a description of what intents an activity (or intent receiver, see below) is capable of handling. An activity that is able to display contact information for a person would publish an IntentFilter that said that it knows how to handle the action VIEW when applied to data representing a person. Activities publish their IntentFilters in the AndroidManifest.xml file.

Navigating from screen to screen is accomplished by resolving intents. To navigate forward, an activity calls startActivity(myIntent). The system then looks at the intent filters for all installed applications and picks the activity whose intent filters best matches myIntent. The new activity is informed of the intent, which causes it to be launched. The process of resolving intents happens at run time when startActivity is called, which offers two key benefits:

* Activities can reuse functionality from other components simply by making a request in the form of an Intent
* Activities can be replaced at any time by a new Activity with an equivalent IntentFilter

Intent Receiver
You can use an IntentReceiver when you want code in your application to execute in reaction to an external event, for example, when the phone rings, or when the data network is available, or when it's midnight. Intent receivers do not display a UI, although they may use the NotificationManager to alert the user if something interesting has happened. Intent receivers are registered in AndroidManifest.xml, but you can also register them from code using Context.registerReceiver(). Your application does not have to be running for its intent receivers to be called; the system will start your application, if necessary, when an intent receiver is triggered. Applications can also send their own intent broadcasts to others with Context.broadcastIntent().

A Service is code that is long-lived and runs without a UI. A good example of this is a media player playing songs from a play list. In a media player application, there would probably be one or more activities that allow the user to choose songs and start playing them. However, the music playback itself should not be handled by an activity because the user will expect the music to keep playing even after navigating to a new screen. In this case, the media player activity could start a service using Context.startService() to to run in the background to keep the music going. The system will then keep the music playback service running until it has finished. (You can learn more about the priority given to services in the system by reading Lifecycle of an Android Application.) Note that you can connect to a service (and start it if it's not already running) with the Context.bindService() method. When connected to a service, you can communicate with it through an interface exposed by the service. For the music service, this might allow you to pause, rewind, etc.

Content Provider
Applications can store their data in files, an SQLite database, or any other mechanism that makes sense. A content provider, however, is useful if you want your application's data to be shared with other applications. A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider.

To get more details on content providers, see Accessing Content Providers.


vivek pathak said...

nice start ....bt dont u update this blog of yours?????

Post a Comment

Total Pageviews