CHAPTER 3: Android GUI Design Concepts and UI programming
Android application component
Intent:
® Intents are asynchronous messages which allows application components
To request functionality from other android components.
® Intents allow you to interact with components from the same application as well as
with components contributed by other application.
® We can open new activity(screen) using intent using startActivity() method.
® Using startActivity() method you can define that the intent should be used to start
an Activity
Syntax:
Intent I = new Intent(this, second Activity name);
StartActivity(i);
® There are two types of intent
1. Explicit intent
2. Implicit intent
1) Explicit intent:
® An application can define the target component directly in the intent that is known
as explicit intent.
® Explicit intent explicitly defines the component which should be called by the
android system, by using the java class as a argument.
® putExtra() method sends an extra information to the new activity.
Syntax:
Intent I = new intent(this, class name)
Second Activity
i.putExtra(“value”, msg);
StartActivity(i);
2) implicit intent:
® Implicit intent used to call internal functionality of a android system.
® Following code make a call using ACTION_CALL content and we set the data
using setData() method that is our contact no and finally we starts the Activity to
make a call.
Intent ci= new Intent(Intent ACTION_call)
(i.setDATA(uri.parse(“tel: 9879161710”));
StartActivity (ci);
Broadcast receivers
Broadcast Receivers (commonly referred to simply as Receivers) are used to
listen for Broadcast Intents. For a Receiver to receive broadcasts, it must be
1
,ANDROID PROGRAMMING PIET-DS
registered, either in code or within the application manifest — the latter case is
referred to as a manifest Receiver In either case, use an Intent Filter to specify
which Intent actions and data your Receiver is listening for.
To create a new Broadcast Receiver, extend the Broadcast Receiver class and
override the on Re-ceive event handler:
Import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
//TODO: React to the Intent received.
}
}
The onReceive method will be executed on the main application thread when a
Broadcast Intent is received that matches the Intent Filter used to register the
Receiver.
The onReceive handler must complete within five seconds; otherwise, the Force
Close dialog will be displayed.
Activity
An activity represents a single screen with a user interface just like window or
frame of Java.Android activity is the subclass of ContextThemeWrapper class.
2
, ANDROID PROGRAMMING PIET-DS
Activity lifecycle
3