Sie sind auf Seite 1von 10

Mobile application Development UNIT II

MOBILE APPLICATION DEVELOPMENT


UNIT II

Unit II contents at a glance:

1. Creating first android application,


2. Anatomy of android application,
3. Deploying Android app on USB connected Android device,
4. Android application components,
5. Activity life cycle,
6. Understanding activities,
7. Exploring Intent objects,
8. Intent Types,
9. Linking activities using intents(explicit and implicit intents)
10. lab programs (calculator, activity life cycle demonstration, intents etc)-refer class notes

1. Creating first android application:

1. Open Android Studio.


2. Go to file-->New--->New project

or

Under the "Quick Start" menu, select "Start a new Android Studio project."

3. On the "Create New Project" window that opens, name your project "HelloWorld".
4. If you choose to, set the company name as desired*.
5. Note where the project file location is and change it if desired.
6. Click "Next."
7. Make sure on that "Phone and Tablet" is the only box that is checked.
8. If you are planning to test the app on your phone, make sure the minimum SDK is below your phone's operating
system level.
9. Click "Next."
10. Select "Empty Activity."
11. Click "Next."
12. Leave all of the Activity name fields as they are.
13. Click "Finish."

1
VARDHAMAN COLLEGE OF ENGINEERING CSE Department
Mobile application Development UNIT II

2. Anatomy of android application:

The various files are as follows:


AndroidManifest.xml

This is the manifest file for your Android application. Here you specify the permissions needs by your application,
package name, icon launcher, application name, and other features (such as Intent-filters, activity, receives, etc.).

MainActivity.java

Located inside java/your_package/ -- this is source file for your project (JAVA language). Here, the MainActivity.java file
is the source file for your Activity. You write code for your application in this file. The java file is listed under package
name for your project,

res/drawable
The folder contains all assets used by your application, such as files, text files, images, etc.

res/layout
The folder contains all layout used by application, with XML format.
Here, define the user interface for the application, define components and attributes (such as id, default value, width,
height, position,...)

2
VARDHAMAN COLLEGE OF ENGINEERING CSE Department
Mobile application Development UNIT II

res/mipmap
The folder contains all icons used by application (icon launcher, icons button, etc.) with multiple resolution format (hdpi,
mdpi, xhdpi, xxhdpi, xxxhdpi). [about density]
res/values
This folder contains 3 files default: strings.xml (define STRINGs with NAME), colors.xml (defined COLORs code by
NAME), styles.xml. Here you save all the string contants in your application (objects that do not change later) , and when
want use only call NAME.

3. Deploying Android app on USB connected Android device

1. enable "USB debugging " option in mobile


2. enable "allow apps from external sources" option in mobile
3. connect your mobile to system using USB cable
4. run your app and select your mobile device shown under tab "connected devices"

4. Android Application components:

Android Application Components or Building blocks of Android App:

There are four types of App Components:

1. Activities
2. Services
3. Content Providers
4. Broadcast Receivers

1) Activities:

An activity represents a single screen with a user interface.


example 1, an email app might have one activity that shows a list of new emails, another activity to compose an
email, and another activity for reading emails.
When we switch to next activity, the previous activity is pushed into stack called back stack. Although the activities
work together to form a cohesive user experience in the email app, each one is independent of the others. As such, a
different app can start any one of these activities (if the email app allows it).
example2, a camera app can start the activity in the email app that composes new mail, in order for the user to share
a picture.

3
VARDHAMAN COLLEGE OF ENGINEERING CSE Department
Mobile application Development UNIT II

2) Services:
A service is a component that runs in the background to perform long-running operations or to perform work for
remote processes. A service does not provide a user interface. For example, a service might play music in the
background while the user is in a different app, or it 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.

3) Content Providers:
A content provider manages a shared set of app data. You can store the data in the file system, an SQLite database, on
the web, or any other persistent storage location your app can access. Through the content provider, other apps can
query or even modify the data (if the content provider allows it).
Content providers manages data sharing between applications.

4) Broadcast Receivers:
A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts
originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or
a picture was captured. Apps can also initiate broadcasts—for example, to let other apps know that some data has
been downloaded to the device and is available for them to use. Although broadcast receivers don't display a user
interface, they may create a status bar notification to alert the user when a broadcast event occurs.

INTENTS:
Three of the four component types—activities, services, and broadcast receivers—are activated by an
asynchronous message called an intent. Intents bind individual components to each other at runtime (you can think
of them as the messengers that request an action from other components), whether the component belongs to your
app or another.
For activities and services, an intent defines the action to perform (for example, to "view" or "send" something) and
may specify the URI of the data to act on (among other things that the component being started might need to know).
For example, an intent might convey a request for an activity to show an image or to open a web page.

The Manifest File


Before the Android system can start an app component, the system must know that the component exists by reading
the app's AndroidManifest.xml file (the "manifest" file). Your app must declare all its components in this file, which
must be at the root of the app project directory.

The manifest does a number of things in addition to declaring the app's components, such as:
 Identify any user permissions the app requires, such as Internet access or read-access to the user's contacts.
 Declare the minimum API Level required by the app, based on which APIs the app uses.
 Declare hardware and software features used or required by the app, such as a camera, bluetooth services, or
a multitouch screen.

Android Virtual Device (AVD)

It is used to test the android application without the need for mobile or tablet etc. It can be created in different
configurations to emulate different types of real devices.

Android Emulator is used to run, debug and test the android application. If you don't have the real device, it can be the

best way to run, debug and test the application.

4
VARDHAMAN COLLEGE OF ENGINEERING CSE Department
Mobile application Development UNIT II

5. Android Activity Life cycle:

 The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call
to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all remaining resources in
onDestroy(). For example, if it has a thread running in the background to download data from the network, it may
create that thread in onCreate() and then stop the thread in onDestroy().

 The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During
this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the
user. Between these two methods you can maintain resources that are needed to show the activity to the user. For
example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and
unregister it in onStop() when the user no longer sees what you are displaying. The onStart() and onStop() methods
can be called multiple times, as the activity becomes visible and hidden to the user.

 The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause().
During this time the activity is in front of all other activities and interacting with the user. An activity can frequently

5
VARDHAMAN COLLEGE OF ENGINEERING CSE Department
Mobile application Development UNIT II

go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is
delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.

The entire lifecycle of an activity is defined by the following Activity methods. All of these are hooks that you can
override to do appropriate work when the activity changes state. All activities will implement onCreate(Bundle) to do
their initial setup; many will also implement onPause() to commit changes to data and otherwise prepare to stop
interacting with the user. You should always call up to your superclass when implementing these methods.

Android activity life cycle methods:

6
VARDHAMAN COLLEGE OF ENGINEERING CSE Department
Mobile application Development UNIT II

Intents:

Three of the four component types—activities, services, and broadcast receivers—are activated by an
asynchronous message called an intent.

7
VARDHAMAN COLLEGE OF ENGINEERING CSE Department
Mobile application Development UNIT II

Types of Intents:

Sample code for explicit intent:


Intent i = new Intent(this,TargetActivity.class)
startActivity(i);

the above code launches an activity from same application. the activity name is Target Activity.

8
VARDHAMAN COLLEGE OF ENGINEERING CSE Department
Mobile application Development UNIT II

9
VARDHAMAN COLLEGE OF ENGINEERING CSE Department
Mobile application Development UNIT II

10
VARDHAMAN COLLEGE OF ENGINEERING CSE Department

Das könnte Ihnen auch gefallen