<<  The 28 th of February Kharkov National Medical University  >>
Programming the Android
Programming the Android
Welcome
Welcome
Outline of the talk
Outline of the talk
Section I
Section I
Why is mobile programming different
Why is mobile programming different
Practical Matters
Practical Matters
Installing the SDK
Installing the SDK
Section II
Section II
The Android Approach
The Android Approach
Activities
Activities
The evolution of a Activity
The evolution of a Activity
Steps to developing an Activity
Steps to developing an Activity
Intents
Intents
Services
Services
Storing long term data
Storing long term data
Content Providers
Content Providers
So, how do I design my program
So, how do I design my program
Section III
Section III
Designing our first Android App
Designing our first Android App
What type of input do we have
What type of input do we have
Our program design
Our program design
What next
What next
Thanks
Thanks

: Programming the Android. : kmicinski. : Programming the Android.ppt. zip-: 611 .

...

Programming the Android

Programming the Android.ppt
1 Programming the Android

Programming the Android

Kristopher Micinski, Secretary, ACM Chapter, MSU ACM Fall Activity Series, 2010 October 21st, 2010

2 Welcome

Welcome

Welcome to the first talk in the ACM Activity series for Fall 2010: Okay, so this one is more of a lecture. But theres pizza over there. The goal of the Activity series is to cover topics that you might be interested in, but wont cover in class because they take too much time or dont directly relate to the material. So any of your ideas would really help for future events: LaTeX? Linux kernel internals. How does your toolchain really work? An introduction to F#.

Believe it or not, when you search Google for LaTeX, you dont get the software package

3 Outline of the talk

Outline of the talk

Today were talking about how to write applications on the Android operating system. First: What makes mobile application development different? A broad overview of the Android approach. Android programming: Covering the three basic classes: Activities Services Content providers Example application: MSU Cafeteria menus What should I take out of the talk? Hopefully you get a broad overview of the system, but youll have to go read about / play around with the sample code and experiment to understand more.

4 Section I

Section I

Introduction, Mobile Programming, Android overview

5 Why is mobile programming different

Why is mobile programming different

So why cant we just put our desktop code on a mobile device verbatim? Easy answer: mobile devices have different types of resources (RAM, long term storage, etc) than your laptop. Slightly more difficult answer: The field is moving quickly, driven by industry, companies are driven by time to market. Consequence: APIs are specific to devices.

Mobile devices have increased RAM daily, so why dont we wait until we have enough? Easy answer, we never will Harder answer: Other differences too. Device manufacturers or other big companies give you the tool chain. The API is restricted: Dont want a program to send data or text without your knowledge! User interaction is different on a mobile device (small screen?)

6 Practical Matters

Practical Matters

Youll write your code in Java, and using the android API. It might not do everything you want! I had to write my own class to output DOM based XML. Also, it changes, your app might require the newer APIs. The SDK, emulator, etc... Is all free! Uses Eclipse for the IDE, you might be able to get VS to work? A few basic classes, then lots of smaller utility classes: Some classes are Android specific I strongly encourage you to do the following: Go through the slides in this talk first, and then the sample application. Go to the Android developer documentation, and read through the main pages / classes. Start working on your own applications, and then when you get stuck, read the documentation

7 Installing the SDK

Installing the SDK

http://developer.android.com/sdk/installing.html Describes in lengthy detail how to do this, but Ill give a quick overview. First download Eclipse: http://www.eclipse.org/ Next, download the Android SDK: http://developer.android.com/sdk/index.html Unzip the SDK and run the SDK manager in the directory you unzip, then install all of the extra required packages, etc Be sure to poke around in the SDK directory, there are lots of samples, documentation, and more! Create a new virtual device so that you can run an emulator. Then go into eclipse and download the Android developer tools: Go to Help->Install new software and add the following URL https://dl-ssl.google.com/android/eclipse/ Now add a new name (just make one up?) and download You should be good to go from here! Try doing a New->Project and selecting a sample application!

8 Section II

Section II

Basic Android programming overview

9 The Android Approach

The Android Approach

Based on the Model View Controller design pattern. Dont think of your program as a linear execution model: Think of your program as existing in logical blocks, each of which performs some actions. The blocks communicate back and forth via message passing, etc Added advantage, physical user interaction (screen clicks) and inter process interaction can have the same programming interface Also the scheduler can bring different pieces of the app to life depending on memory needs and program use

For each distinct logical piece of program behavior youll write a Java class (derived from a base class). Activities: Things the user can see on the screen. Basically, each different screen in your program. Services: Code that isnt associated with a screen (background, fairly common) Content providers: Provides an interface to exchange data between programs (usually SQL based) Kind of vague, but well given an example.

10 Activities

Activities

Activities describe individual screens with which a user could be interacting. Your program specifies a top level screen that runs upon application startup. Each Activity performs its own actions, to execute a method in another Activity use an Intent. The Activity base class already provides you with enough functionality to have a screen which does nothing Provides you with an empty canvas! No pun intended

The activity allows you to set the top level GUI container. Then you instantiate some Views (widgets), put them in a container View, and set the container as the Activitys top level View: This is what gets displayed on the screen when the Activity is running. We wont go too in depth on GUI programming here, lots of documentation. The Activity is loaded by the Android OS, then the appropriate methods are called based on user interaction (back button?)

11 The evolution of a Activity

The evolution of a Activity

The Activity has a number of predefined functions that you override to handle events from the system. If you dont specify what should be done the system will perform the default actions to handle events. Why would you want to handle events such as onPause(), etc ? You will probably want to do things like release resources, stop network connections, etc

12 Steps to developing an Activity

Steps to developing an Activity

Create a new class in your project, using Activity as a base class. Then override onCreate(), onResume(), etc as you need! Put your Activity in the android manifest: This is a file that specifies the different classes in your app, what they do, and how they interact. Easiest to see examples of this, or you can use Eclipse to fill this out for you.

Specify a default View for the Activity. In the onCreate (usually) you set up handlers for your Views actions: Set an onClick handler for some button? Find out what other Activities you want to talk to: When do you want to change screens? For example, go to PassportBrowser from DocumentDisplay. Then use an Intent

13 Intents

Intents

BrowseContents Running

How does an Activity (or any other runnable Android object) get started? We use the Intent class to ask the Android OS to start some Activity, Service, etc Then the OS schedules that Activity to run, and that Activity has its onCreate (or onResume, etc) method called. Intents are used to represent most inter-process requests in Android: Dialing a number Sending a text Starting a new Activity within your application So the system will generate Intents, and so will your app!

EditContent running

Notice! Here the red transitions are the events initiated by the Android OS, and the green transitions are created by your application!

14 Services

Services

Services provide a way for your application to handle events in the background, without being explicitly associated with a View. However, services dont reside in their own thread! So dont perform things like network connections in a service, you will block the main thread! However, what can you do? Use your Service class to provide an interface to a background thread Can call back to main activity using a Handler class

Main Activity

Service

Downloading Threads (maybe thread pool?)

New worker thread

Worker Thread (downloading)

User selects item to download

15 Storing long term data

Storing long term data

Eventually youll want to store long term data in your program. You can use the SharedPreferences class to store simple key-value pairs Simple interface, call getSharedPreferences and then use call getString, getBoolean, etc However, youll probably want to use more complicated storage. Android provides an SQLite interface Also ContentProviders provide inter process data storage

The best solution is to use the Android interface to SQLite: Lightweight database based on SQL Fairly powerful, cant notice the difference between SQLite and SQL unless you have a large database You make queries to the database in standard SQL: SELECT ID, CITY, STATE FROM STATION WHERE LAT_N > 39.7; Then your application provides a handler to interface the SQL database to other applications via a content provider: Look at an example!

16 Content Providers

Content Providers

Content providers abstract data storage to other applications, activities, services, etc Again, fairly SQL based. You will construct a ContentProvider class that will override methods such as insert(), delete(), and update(). Then you register your content provider with a URI to handle different types of objects. Unique Resource Identifier (kind of like a URL)

For example, lets say we want our content provider to allow other applications to access our database of bicycles and also customers. We define methods for inserting, deleting, updating, etc bicycles and customers. Then we publish two URIs: BICYCLES_URI CUSTOMERS_URI Maybe more URIs for accessing bicycles indexed by serial number?

17 So, how do I design my program

So, how do I design my program

The way the system architecture is set up if fairly open: Program design is somewhat up to you, but you still have to live with the Android execution model. Start with the different screens that the user will see. These are the different Activities that will comprise your system. Think about the transitions between the screens, these will be the Intents passed between the Activities.

Think about what background Services you need to incorporate. Exchanging data Listening for connections? Periodically downloading network information from a server? Think about what information must be stored in long term memory and design a content provider around it. Now connect the Activities, services, etc with Intents! Dont forget good OOP ?

18 Section III

Section III

Sample Application: MSU Cafeteria Menu Browser

19 Designing our first Android App

Designing our first Android App

We want to design an application to view the menus for the MSU cafeterias. What is involved with this? What objects do we have to deal with? A menu A cafeteria An internet connection? For this application, Im going to use the ListActivity: This is an Activity with a top level container to display a list with which the user can interact. What background services are we going to need here? Im just going to use a thread here, not enough work to set up an activity

20 What type of input do we have

What type of input do we have

Lets visit http://www.eatatsate.com to find out! We can see that we can select a number of different cafeterias: Probably going to stick with this interface, so the main Activity of our application will let us select cafeterias. With each of those cafeterias we see that we can have a number of different stations: So down in the hierarchy the user will select the station in which they are interested. For each cafeteria we have an RSS feed describing the current cafeteria menu. So well need to keep track of what cafeterias go with what RSS feeds (URLS)

21 Our program design

Our program design

Android Browser

MainScreen (Activity)

MenuDownloadingThread

New()

CafeteriasContentProvider

CafeteriaMenuDownloader

22 What next

What next

What should you do now? Go download the Android SDK, even if you dont have an android phone this can still be fun! Then download the sample application and go through. Set breakpoints at onCreate() and onResume() on the main activities. Nice debugger. You can even debug two devices communicating to each other at the same time over Bluetooth on the same computer! Dig around in the documentation and read the docs on the classes I mentioned. Be careful, pressing next in the debugger doesnt always mean you will go to the next line. Sometimes you will fall into an Android binary class, so set lots of breakpoints. Now try to modify the demo application: Add caching! Create a add new cafeteria Activity that allows you to enter a title and URL of the RSS feed for cafeterias and add new ones to the database. Add a way to delete cafeterias from the database, maybe automatically delete them after the connection does not work for a while?

23 Thanks

Thanks

Thanks for coming to the first in the ACM Activity series! Maybe some still free pizza, grab some. Be sure to talk to me and tell me if you had any comments or questions Also, were going to use the ACM Listserv to support answers to questions for these events. Send your questions there. Join the MSU ACM Chapter!

Programming the Android
http://900igr.net/prezentacija/anglijskij-jazyk/programming-the-android-175550.html
c

46

29
900igr.net > > > Programming the Android