Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Android

Similar presentations

Presentation on theme: "Introduction to Android"— Presentation transcript:

1 Introduction to Android

2 Outline 1.Android Basics 1.Background 2.Setup Environment 3.Architecture Diagram 2.Activity and Services 1.Function, Lifecycle, Coding Assignment 2. 3.Content Providers and SQLite Databases 1. 4.Android Intents 1.Intent Objects, Intent Resolution, Intent Filters 5.Building User Interfaces 1. 6.References

3 Android Basics -Background October 2008: First Android version API L1.0 October 2013: Last Android version API L19.0 December 2013: ~4000 Android capable models December 2013: 800 Million active Android users

4 Android Basics - Setup Environment Download Android Developer Tools (ADT) Open Android Developer Tools (ADT) Create an empty Android Project Sit together in teams such that everyone has an ADT screen in front.

5 Android Basics - Architecture Diagram

6 Activity and Services - What are the functions of an activity? 1.Interact in most case with user 2.Usually applications have several activities 3.Each is implemented as a subclass of base Activity class 4.Creates the window to place UI 1.setContentView(R.layout.home_activity); 5.Methods 1.protected void onCreate(Bundle savedInstanceState); // initialize 2.protected void onStart(); //activity visible 3.protected void onRestart(); //before activity becomes visible 4.protected void onResume(); // activity visible after being paused 5.protected void onPause(); // another activity becomes visible 6.protected void onStop(); // activity not visible 7.protected void onDestroy(); // cleanup and destroy activity

7 Activity and Services - What is the lifecycle of an activity?

8 Activity and Services - Coding Assignment (5 min) 1.Start Android Developer Tools 2.Create a new Project with one Activity 3.Define all six methods in that activity

9 Activity and Services - What are the functions of a service? 1.Runs in the background – what does it mean? 2.Each is implemented as a subclass of base Service class 3.Examples – Music, TCP, REST, and other long running operations… 4.Methods 1.public void onCreate() // initialize 2.public void onStartCommand() // started 3.public void onBind() // activity to service communication 4.public void onDestroy() // cleanup resources 5.public void stopService() // stop service

10 Activity and Services - Service - Function Services must be declared in AndroidManifest.xml Remember to update the string resource file!

11 Activity and Services - What is the service lifecycle? 1.Does a Service have Lifecycle? 1.Arguably no, since there is no interaction with the user, internally yes. 2.Who can start a service? 1.Services, Receivers, and Activities 3.How do you start a service? – Intent i= new Intent(getApplicationContext, MyAppService.class); – i.putExtra(“key", “value"); – getApplicationContext.startService(i);

12 Activity and Services - Coding Assignment (5 min) Create a Service class Add 5 methods Start that Service through the Activity and pass a key/value pair Print out value/pair using activity on console

13 Activity and Services - Questions What are the four layers of the Android Architecture Stack? What is the function of an activity? What are the four states of an activity? What are the six methods that change the state of an activity? What is a Service? What are the 5 methods that implement a service? What is the difference between Activity and Service? Who can start service?

14 Content Providers and SQLite Databases 1.What if your app should open your X-Ray scan result. 2.Why data persistence on Android necessary? 1.Faster access to locally stored data 2.Less internet traffic  Less power consumption 3.Application works offline 4.Increased data privacy

15 Content Providers and SQLite Databases Both integrated into every Android device Both return a cursor object referencing to the result set Content Providers – Visibility: Can be shared with other applications. – An API which exposes databases to other apps. – Access to phone databases (contacts, location,…) SQLite Database ( – Visibility: Only to application that created it. – Open-source ( ) – Standard-compliant (ISO/IEC 9075) l.htm?csnumber=53685 l.htm?csnumber=53685

16 Content Providers and SQLite Databases - SQLite Database Steps 1.Create database if not created 2.Within database create set of table schemas if not created 3.Open database connection 4.Insert, Delete, or Update table 5.Commit (sometimes) 6.Close database connection

17 Content Providers and SQLite Databases - Content Provider Content Resolver Query mCursor = getContentResolver().query( UserDictionary.Words.CONTENT_URI, // The content URI of the words table mProjection, // The columns to return for each row mSelectionClause // Selection criteria – WHERE col = ? mSelectionArgs, // Selection criteria – Argument list mSortOrder); // The sort order by cols for the returned rows Content Resolver Query Example Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); Find Johm Smith! mSelectionClause=ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +" = "+ ? mSelectionArgs=“John Smith”

18 Content Providers and SQLite Databases - Questions What are the benefits of using Android-based persistence? What is the main difference between and SQLite database and Content Provider? What are six common SQL steps to successfully access data in database?

19 Content Providers and SQLite Databases - Coding Assignment 1.Create a SQLite table ‘patient’ on your phone with two columns (ID, Patient Name) 2.Using the Content Provider get all contacts from your phone book with following criteria and store them in the ‘patient’ table 1.Display name starts with letter ‘a’ or ‘A’ 2.Phone number has CT area code 860 3.Sorted in alphabetical order of display name

20 Android Intents - Intent Activity Service Activity Service START/FINISH Explicit Implicit BROAD CAST MSG

21 Activity 2 Android Intents Activity 1 Activity 2 App 1 App 2

22 Android Intents - Example Intent intent = new Intent(Actvitity1.class, Activity2.class); startActivity(intent) ; Intent intent = new Intent (Intent.ACTION_DIAL, Uri.parse(“8601234567”); startActivity(intent) Which is the implicit/explicit activity start? Native Android Actions: ACTION_CALL, ACTION_DIAL, ACTION_ANSWER etc. About 17 activity actions, plus a number of broadcast actions. What are examples for Native Android Broadcast?

23 Android Intents – Broadcast Example Acvitity A registerReceiver(uiUpdated, new IntentFilter(“BROADCASTNAME")); private BroadcastReceiver uiUpdated = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { ArrayList cList = intent.getParcelableArrayListExtra("data"); render(cList);}}; Activity B Intent i = new Intent("BROADCASTNAME"); i.putParcelableArrayListExtra("data", cListNew); sendBroadcast(i);

24 Android Intents - Questions What are the 5 tasks of an intent? Can you explicitly start an activity in the same app? Can you explicitly start an activity in a different app? What are examples for implicitly starting an activity? How does this relate to the PHA app?

25 Android Intents - Coding Assignment 1.Create two activities a and b 2.Start activity b with a 3.In activity b send a broadcast message 4.Activity a listens to the broadcast and prints it Can you think of a scenario where this simple example is very useful?

26 Building User Interfaces - Fundamentals UI Design Views (or controls/widgets) – Base class for all visual interface elements View Groups – Extends views and contains multiple child views – Manage layout of child view Fragments (Android 3.0/API level 11) – Encapsulate portions of the UI layout Activities – Window that interacts with user.

27 Building User Interfaces - Assign UI to Activity

28 Building User Interfaces - Introducing Layouts FrameLayout – Most basic, pins each child view within frame. ListView Layout – View group that displays a list of scrollable items. LinearLayout – Aligns each child element along an vertical or horizontal line. RelativeLayout – Most flexible. Aligns child view relative to others. GridLayout (Android 4.0/API level 14) – Rectangular Grid

29 Building User Interfaces - Introducing Layouts

30 Building User Interfaces - Defining Layouts

31 You can define the layout using: – XML – Programmatically (Java Code)

32 Building User Interfaces - Code Review

33 Building User Interfaces – Coding Assignment 1.Go through the patient database code and run it using ADT emulator. 2.Go through the patient user interface list code run it using ADT emulator 3.Combine code of 1. and 2. to save every newly added patient directly in the database

34 Building User Interfaces - Group Coding Exercise 1.Goal 1.Create layouts based on templates 2.Work with data inputs 3.Listen to user events 4.Update activity screen 2.Spend time to read the PHA code 3.

35 Building User Interfaces - Layout – Activity Pair R.Java Layout Generates Used by Activity Activity Layout

36 Building User Interfaces - Sample calculator design etNum1etNum2 btnAdd btnSub btnDiv btnMult tvResult

37 Building User Interfaces - Coding Assignment calculator calculator Add a tip calculator function that adds 15% to the sum of the value of both fields.

38 References html html Professional Android 4 Application Development (Reto Meier, 2012) list/225-lesson-19-creating-a-simple- calculator.html

Download ppt "Introduction to Android"

Similar presentations

Ads by Google