Application
Components
- Application components are the essential building blocks of an Android application
- Components that are defined in the application's manifest file.
- A component can be one of the following
- Activities and Intents
- Broadcast Receivers
- Services
- Content Providers
Activity:
- Presentation layer of an Android application, e.g. a screen which the user sees.
- Each activity is given a default window to draw in.
- Each activity is independent of the others.
- Application can have several activities and it can be switched between them during runtime
- Typically, one of the activities is marked as the first one that should be presented to the user when the launched.
- Moving from one activity to another is accomplished by having the current activity start the next one through so called intents.
Activity
Life Cycle
Most management of the life cycle is done
automatically by the system via the activity stack.
An
activity has essentially three states:
- Active or running
- Paused or
- Stopped
Active
or running:
- Foreground of the screen (at the top of the activity stack for the current task).
- This is the activity that is the focus for the user's actions.
Paused:
- It is paused if it has lost focus but is still visible to the user.
- That is, another activity lies on top of it and that new activity either is transparent or doesn't cover the full screen.
- A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
Stop
- It is stopped if it is completely obscured by another activity.
- It still retains all state and member information. However, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere
Activity
Call Back Methods
Method
|
Description
|
Killable
after?
|
Next
|
||
->Called
when the activity is first created.
->
Always followed by onStart().
|
No
|
onStart()
|
|||
Called
after the activity has been stopped, just prior to it being started again.
->Always
followed by onStart()
|
No
|
onStart()
|
|||
Called
just before the activity becomes visible to the user.
àFollowed
by onResume() if the activity comes to the foreground, or onStop() if it
becomes hidden.
|
No
|
onResume()
or onStop() |
|||
Called
just before the activity starts interacting with the user. At this point the
activity is at the top of the activity stack, with user input going to it.
->
Always followed by onPause().
|
No
|
onPause()
|
|||
Called
when the system is about to start resuming another activity.
->
Followed either by onResume() if the activity returns back to the front, or
by onStop() if it becomes invisible to the user.
|
Yes
|
onResume()
or onStop() |
|||
Called
when the activity is no longer visible to the user.
->Followed
either by onRestart() if the activity is coming back to interact with the
user, or by onDestroy() if this activity is going away.
|
Yes
|
onRestart()
or onDestroy() |
|||
Called
before the activity is destroyed.
|
Yes
|
nothing
|
Services:
- A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time.
- Each service class must have a corresponding <service>declaration in its package's AndroidManifest.xml.
- Run in the main thread of their hosting process.
- Blocking (such as networking, RSS exchange) operations, it should spawn its own thread
- Services can be started/stopped with
- Context.startService() and
- Context.bindService().
- stopService(…) and unbindService(…)
Service Lifecycle:
System
Service:
- TELEPHONY_SERVICE
- ALARM_SERVICE
- AUDIO_SERVICE
Broadcast
Receivers
- A broadcast receiver is a component that does nothing but receive and react to broadcast announcements.
- Broadcast Receivers is an Android implementation of system-wide publish/subscribe mechanism (more precisely, this is an Observer pattern).
- The system itself broadcasts events all the time. For example, when an SMS arrives, or call comes in, or battery runs low, or system gets booted, all those events are broadcasted and any number of receivers could be triggered by them.
- Broadcast receivers do not display a user interface
- This means that for longer-running operations you will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation
Content
Providers:
- Content providers store and retrieve data and make it accessible to all applications.
- They are the only way to share data across Android applications. There's no common storage area that all Android packages can access.
- Android ships with a number of content providers for common data types (audio, video, images, personal contact information, and so on).
- Content Providers are a data layer providing data abstraction for its clients and centralizing storage and retrieval routines in a single place.
- A Content Provider may use any form of data storage mechanism available on the Android platform, including files, SQLitedatabases, or even a memory-based hash map if data persistence is not required (Shared Preference).
Intent:
- Abstract description of an operation to be performed
- Most significant use is in the launching of activities (acts as glue between activities)
- Main arguments of Intent
are:
Action: The built-in action to be performed, such as ACTION_VIEW,ACTION_EDIT,ACTION_MAIN, …or user-created-activityData : The primary data to operate on, such as a phone number to be called (expressed as a Uri Eg :tel:/216 555-1234 , "http://maps.google.com”Typically intent is called as follows:
- Explicit Intent
- Implicit Intents
Explicit Intent
Explicitly designate
the target component
Example (Navigating between activities)
AndroidManifest.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ex"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk android:minSdkVersion="8"
/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name=".IntentsExampleActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="BBActivity"></activity>
<activity android:name="com.ex.AndroidActivity"></activity>
</application>
</manifest>
\res\layout\android.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/androidtextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Android !!!"
android:textStyle="normal" >
</TextView>
</LinearLayout>
\res\layout\bb.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/bbtextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Blackberry !!!"
android:textStyle="normal" >
</TextView>
</LinearLayout>
\res\layout\main.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Blackberry" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android" />
</LinearLayout>
\src\com\ex\AndroidActivity.java
package com.ex;
import
android.app.Activity;
import android.os.Bundle;
public class AndroidActivity extends Activity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
// TODO Auto-generated
method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.android);
}
}
\src\com\ex\BBActivity.java
package com.ex;
import com.ex.R;
import
android.app.Activity;
import android.os.Bundle;
public class BBActivity extends Activity{
@Override
protected void onCreate(Bundle
savedInstanceState) {
// TODO Auto-generated
method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bb);
}
}
\src\com\ex\IntentsExampleActivity.java
package com.ex;
import com.ex.R;
import
android.app.Activity;
import
android.content.Intent;
import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
/**
*
* @author rathika raj
*
*/
public class
IntentsExampleActivity extends Activity {
Button
bbButton = null;
Button
androidButton = null;
/** Called when the
activity is first created. */
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get the Ui
components
bbButton = (Button)
findViewById(R.id.button1);
androidButton = (Button)
findViewById(R.id.button2);
// BB Button Click
bbButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// Navigate to New
Screen
Intent
lEDOptionsIntent = null;
lEDOptionsIntent
= new
Intent(IntentsExampleActivity.this,
BBActivity.class);
startActivity(lEDOptionsIntent);
}
});
// Android Button
Click
androidButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// Navigate to New
Screen
Intent
lEDOptionsIntent = null;
lEDOptionsIntent
= new
Intent(IntentsExampleActivity.this,
AndroidActivity.class);
startActivity(lEDOptionsIntent);
}
});
}
}Output:
Implicit Intent
- Declare intent and leave it to the platform to find an activity that can respond to the intent.
- Declare the target component
- Used for activating components of other applications seamlessly
- Late Binding applies
Implicit
Intent Example:
1) View
Contacts in phone
2) Dial
phone number
The following example launches the android dialer with the specified telephone number(123).
AndroidManifest.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example" android:versionCode="1" android:versionName="1.0"
>
<uses-sdk android:minSdkVersion="8"
/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".IntentsEx1Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
main.xml
Holds a User Interface contain a simple button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="ImplicitIntent" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="ImplicitIntent" />
</LinearLayout>
IntentsEx1Activity.java
Each activity corresponds to a screen and onclick of the button an android dialer will be launched
package com.example;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentsEx1Activity extends Activity {
private Button buton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buton = (Button) findViewById(R.id.button);
final String number = "tel:123";
buton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse(number));
startActivity(intent);
}
});
}
}
Output :
No comments:
Post a Comment