Download Android Application Development

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Android Application
Development
Octav Chipara
Thursday, September 13, 12
What is Android
• A free, open source mobile platform
• A Linux-based, multiprocess, multithreaded OS
• Android is not a device or a product
• It’s not even limited to phones - you could build
• a DVR, a handheld GPS, an MP3 player, etc.
2
Thursday, September 13, 12
The Android stack
3
Thursday, September 13, 12
Android Application Framework
4
Thursday, September 13, 12
Android runtime
• Every Android app runs its own process, with its own instance of the Dalvik
virtual machine
• Dalvik has been written so that it can run multiple VMs efficiently
• Dalvik VM executes files in Dalvik Executable (.dex) format
• more details in later lectures
5
Thursday, September 13, 12
Basic Android application components
6
Thursday, September 13, 12
Hello World
Thursday, September 13, 12
Application directory structure
Directory
src/
res/layout
res/values
res/drawable-?dpi/
AndroidManifest.xml
gen/
Description
source files
contains the XML file the
specifies
screen layout
constants,the
etc.
pictures used by the app
the properties of the project
automatically generated code
8
Thursday, September 13, 12
Generating GUIs
• Two ways to create GUIs
• in XML - a declarative approach
• in code - discouraged!
• A lot of the GUI-related files will be located in
• res/layout
• res/values
• res/AndroidManifest.xml - key file in your development
Referencing resources
9
Thursday, September 13, 12
Views and View Groups
• Views are the building blocks
• TextView, EditText, or ListView, ...
• View Groups - define how the child views are laid out
• grid, lists
10
Thursday, September 13, 12
Properties of views and view components
• Three files you will be working on
• activity_main.xml, values.xml, and R.java
• you edit the xml files, R.java is automatically generated but used in your code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight = "1"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickMe"
android:text="@string/button_label"
/>
</LinearLayout>
res/layout /activity_main.xml
11
Thursday, September 13, 12
Properties of views and view components
<resources>
<string
<string
<string
<string
<string
<string
name="app_name">MyFirstApp</string>
name="hello_world">Hello world! This is a new string! </string>
name="menu_settings">Settings</string>
name="title_activity_main">MainActivity</string>
name="button_label">Send!</string>
name="title_activity_display">DisplayActivity</string>
</resources>
res/values /strings.xml
12
Thursday, September 13, 12
Properties of views and view components
public final class R {
...
public static final class id {
public static final int LinearLayout1=0x7f070001;
public static final int button1=0x7f070003;
public static final int editText1=0x7f070002;
public static final int menu_settings=0x7f070004;
public static final int textview=0x7f070000;
}
public static final class layout {
public static final int activity_display=0x7f030000;
public static final int activity_main=0x7f030001;
}
public static final class menu {
public static final int activity_display=0x7f060000;
public static final int activity_main=0x7f060001;
}
public static final class string {
...
}
}
res/gen/R.java
13
Thursday, September 13, 12
Properties of views and view components
• You can reference values between the files various files
• resources are defined in both the layout and values files
• resources are references with @[resource_type]/[name]
• uses @+ to defined new resources
14
Thursday, September 13, 12
Layout
• Controls how Views are laid out
• FrameLayout : each child a layer
• LinearLayout : single row or column
• RelativeLayout : relative to other Views
• TableLayout : rows and columns
• AbsoluteLayout : <x,y> coordinates
15
Thursday, September 13, 12
Layouts are resizable
16
Thursday, September 13, 12
Layout parameters
• Specify many aspects of what’s being rendered
• Examples:
• android:layout_height
• android:layout_width
• Tip: start with documentation for a specific View or Layout and then look at
what’s inherited from parent class
17
Thursday, September 13, 12
Activities
• Typically correspond to one screen in a UI
• if a UI is defined, then it has its own xml file under layouts
• however, they can
• be faceless
• be in a floating window
• return a value
18
Thursday, September 13, 12
Lifecycle of an Activity
transient
The KEY to a happy life with Android!
19
Thursday, September 13, 12
Static application states
• Resumed (aka running)
• activity is in foreground and user may interact with it
• Paused
• activity is partly obscured by another activity
• does not receive any user input
• Stopped (aka background)
• activity is not visible on the screen
20
Thursday, September 13, 12
What should go in each function call?
• protected void onCreate (Bundle savedInstanceState)
• here you can add different UI elements
• instantiate class-wide variables
• saveInstanceState - used to recreate an activity after it is destroyed
21
Thursday, September 13, 12
What should go in each function call?
• protected void onPause ()
• on pause can be both an indication of
• user briefly switched to a different activity OR user switched apps
• you may want to suspend some actions of your activity
• e.g., playback audio or video
• release system resources
• e.g., camera or gps
• guidelines: you should avoid delay intensive operations (this should go onStop())
• e.g., writing to files
• protected void onResume ()
• (re) acquire the necessary to run the application
• e.g., cameras
22
Thursday, September 13, 12
What should go in each function call?
• protected void onStop ()
• note that your activity is still in memory
• release as many resources as necessary
• save data to persistent storage
• protected void onStart() or protected void onRestart()
• verify that the right resources are available for your application
• e.g., GPS, network connections, WiFi
23
Thursday, September 13, 12
Intents
• Think of Intents as a verb and object; a description of what you want done
• examples: VIEW, CALL, PLAY, etc.
• System matches Intent with Activity that can best provide that service
• Activities and BroadcastReceivers describe what Intents they can service in their
IntentFilters (via AndroidManifest.xml)
24
Thursday, September 13, 12
Broadcast receivers
• Components designed to respond to broadcast Intents
• think of them as a way to respond to external notifications or alarms
• Applications can invent and broadcast their own Intents as well
25
Thursday, September 13, 12
Services
• Faceless components that run in the background
• example: music player, network download, etc.
• Bind your code to a running service via a remote-able interface defined in an
IDL
• Can run in your own process or separate process
26
Thursday, September 13, 12
Content Providers
• Enables sharing of data across applications
• examples: address book, photo gallery, etc.
• Provides uniform APIs for:
• querying (returns a Cursor)
• delete, update, and insert rows
• Content is represented by URI and MIME type
27
Thursday, September 13, 12
Android Debug Bridge
• Provides command line access to Android devicds
• adb shell -- starts the shell
• adb pull/push -- copy data from/to device
• adb uninstall - deletes an application
28
Thursday, September 13, 12