Slides for this Lesson

Download Report

Transcript Slides for this Lesson

PROG 38448
Mobile Java Application
Development
Developing Android Apps: Install and Intro
Download & Install
Android Developer’s Site:
http://developer.android.com/index.html
Click SDK tab
Under "Android SDK Starter
Package" heading in the left-hand
frame
click "Installing the SDK“
Read and follow all of the steps
4/6/2016
Wendi Jollymore, ACES
2
Download & Install
Grab Eclipse Classic
http://www.eclipse.org/downloads/
Unzip it into a new folder (you can have
multiple installs on same machine)
Download and run the Android SDK
installer
Run Eclipse
add the ADT Plugin
Run SDK manager and install the
necessary packages and components
4/6/2016
Wendi Jollymore, ACES
3
Application Components
Activities
a single screen with a UI
same as a window or dialog in a
desktop app, the web page in a web
app
the main interface to an app
implemented using the Activity class
4/6/2016
Wendi Jollymore, ACES
4
Application Components
Services
a component that runs in the
background
no UI
keeps running if needed, independent
of any activity
e.g. check for rss feed updates
implemented with the Service class
4/6/2016
Wendi Jollymore, ACES
5
Application Components
Content Providers
manages application data that can be shared
among applications
can be file system, SQLit, web, persistent
storage, etc
android encourages developers to make their
data available to other apps in addition to
your own apps
allows you complete control over how your
data gets accessed
can also be used for data used by your own
app - data that is private
implemented with ContentProvider class
4/6/2016
Wendi Jollymore, ACES
6
Application Components
Broadcast Receivers
responds to system wide broadcast
announcements
many originate from system e.g. low battery,
picture was captured, screen turned off
apps can initiate broadcasts e.g. let other
apps know some data was downloaded and
available
no UI, but could use some kind of status bar
notification
implemented using BroadcastReceiver class
4/6/2016
Wendi Jollymore, ACES
7
Application Components
Intents
notifies apps of different events
hardware state changes: sd card inserted
incoming data such as sms message
application events such as activity launched from
device menu
Messages between activities, services, and
broadcast receivers
you can respond to an intent
you can create your own intents to launch
other activities or let you know when specific
situations arise
4/6/2016
Wendi Jollymore, ACES
8
Application Components
Intents, continued
Intent objects define a message to activate a
specific component (explicit intent) or type of
component (implicit intent)
for activities and services
an intent defines the action to perform (e.g. send
an image to something)
an activity can receive the result of an intent and
send another result in an intent (e.g. issue an
intent to let user pick a contact, return intent
contains reference to that contact chosen)
for broadcast receivers
intent contains announcement to be broadcast
4/6/2016
Wendi Jollymore, ACES
9
Application Components
Any app can start another app's
component
e.g. your app can start the activity
of another app
Example: you want to use the email
screen in your app
4/6/2016
Wendi Jollymore, ACES
10
Hello World!
Start up a new project in your
Android version of Eclipse
See instructions online!
4/6/2016
Wendi Jollymore, ACES
11
Hello World!
package prog38448.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity
{
private TextView txtHello;
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
txtHello = new TextView(this);
txtHello.setText("Hello, Android!");
setContentView(txtHello);
}
}
4/6/2016
Wendi Jollymore, ACES
12
Project Structure
AndroidManifest.xml
system needs to know that a
component exists before it can be
started
AndroidManifest.xml manifest file
contains this info
all application components must be
declared in this file
4/6/2016
Wendi Jollymore, ACES
13
Project Structure
bin/
The compiled application
libs/
Third party JARs
res/
Resources such as icons, layouts, etc
src/
Source files
4/6/2016
Wendi Jollymore, ACES
14
Android UI
Activity Class
A way for the user to interact with
your application
Similar to RIM’s UIApplication class
Each activity has a window where you
can put the interface
4/6/2016
Wendi Jollymore, ACES
15
Android UI
Activity.onCreate(Bundle b)
Where the activity is initialized
Create components, layout interface, etc
Bundle b (savedInstanceState)
A collection of states for all the
components of your UI
Used by parent when you redraw this
screen after visiting another screen
Always call super.onCreate(b); first!
4/6/2016
Wendi Jollymore, ACES
16
Android UI
Activity.setContentView(View v)
Sets the content of this activity to a
specific view
Views can be components or layout
objects that contain components or
nested layouts
This method is always called by the
O/S when a new activity is started
4/6/2016
Wendi Jollymore, ACES
17
Android UI
UI Components are in the
android.widgets package
Some examples:
android.widget.TextView, EditText
android.widget.Button
android.widget.CheckBox
android.widget.RadioButton
android.widget.ListView
android.widget.DatePicker
4/6/2016
Wendi Jollymore, ACES
18
Android UI
Layout is done with View and
ViewGroup class
Most layout is actually done in an XML
file in the res/layout folder
main.xml
setContentView(R.layout.main);
If you create your own custom
components, you subclass View
ViewGroups are layout containers that
can contain other view groups or views
4/6/2016
Wendi Jollymore, ACES
19
For Next Class
Beginning Android 3 by Mark
Murphy
Chapter 8: Using XML-Based Layouts
Chapter 9: Employing Basic Widgets
4/6/2016
Wendi Jollymore, ACES
20