28
Android Application Development Octav Chipara Thursday, September 13, 12

Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

Android Application Development

Octav Chipara

Thursday, September 13, 12

Page 2: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 3: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

The Android stack

3

Thursday, September 13, 12

Page 4: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

Android Application Framework

4

Thursday, September 13, 12

Page 5: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 6: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

Basic Android application components

6

Thursday, September 13, 12

Page 7: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

Hello World

Thursday, September 13, 12

Page 8: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

Application directory structure

8

Directory Descriptionsrc/ source files

res/layout contains the XML file the specifies the screen layoutres/values constants, etc.

res/drawable-?dpi/ pictures used by the appAndroidManifest.xml the properties of the project

gen/ automatically generated code

Thursday, September 13, 12

Page 9: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

9

Referencing resources

Thursday, September 13, 12

Page 10: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 11: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

11

<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

Thursday, September 13, 12

Page 12: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

Properties of views and view components

12

<resources>

<string name="app_name">MyFirstApp</string> <string name="hello_world">Hello world! This is a new string! </string> <string name="menu_settings">Settings</string> <string name="title_activity_main">MainActivity</string> <string name="button_label">Send!</string> <string name="title_activity_display">DisplayActivity</string>

</resources>

res/values /strings.xml

Thursday, September 13, 12

Page 13: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

Properties of views and view components

13

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

Thursday, September 13, 12

Page 14: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 15: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 16: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

Layouts are resizable

16

Thursday, September 13, 12

Page 17: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 18: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 19: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

Lifecycle of an Activity

19

The KEY to a happy life with Android!

transient

Thursday, September 13, 12

Page 20: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

Static application states

20

• 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

Thursday, September 13, 12

Page 21: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 22: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 23: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 24: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 25: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 26: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 27: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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

Page 28: Android Application Developmenthomepage.divms.uiowa.edu/~ochipara/classes/sensing_the_world/S… · What is Android • A free, open source mobile platform • A Linux-based, multiprocess,

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