Download presentation
1
Introduction to Android
2
Outline Android Basics Activity and Services
Background Setup Environment Architecture Diagram Activity and Services Function, Lifecycle, Coding Assignment Content Providers and SQLite Databases Android Intents Intent Objects, Intent Resolution, Intent Filters Building User Interfaces 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?
Interact in most case with user Usually applications have several activities Each is implemented as a subclass of base Activity class Creates the window to place UI setContentView(R.layout.home_activity); Methods protected void onCreate(Bundle savedInstanceState); // initialize protected void onStart(); //activity visible protected void onRestart(); //before activity becomes visible protected void onResume(); // activity visible after being paused protected void onPause(); // another activity becomes visible protected void onStop(); // activity not visible 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)
Start Android Developer Tools Create a new Project with one Activity Define all six methods in that activity
9
Activity and Services - What are the functions of a service?
Runs in the background – what does it mean? Each is implemented as a subclass of base Service class Examples – Music, TCP, REST, and other long running operations… Methods public void onCreate() // initialize public void onStartCommand() // started public void onBind() // activity to service communication public void onDestroy() // cleanup resources public void stopService() // stop service
10
Activity and Services - Service - Function
Services must be declared in AndroidManifest.xml <service android:name=“path.to.MyAppService" </service> Remember to update the string resource file!
11
Activity and Services - What is the service lifecycle?
Does a Service have Lifecycle? Arguably no, since there is no interaction with the user, internally yes. Who can start a service? Services, Receivers, and Activities 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
What if your app should open your X-Ray scan result. Why data persistence on Android necessary? Faster access to locally stored data Less internet traffic Less power consumption Application works offline 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)
16
Content Providers and SQLite Databases - SQLite Database Steps
Create database if not created Within database create set of table schemas if not created Open database connection Insert, Delete, or Update table Commit (sometimes) 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
Create a SQLite table ‘patient’ on your phone with two columns (ID, Patient Name) Using the Content Provider get all contacts from your phone book with following criteria and store them in the ‘patient’ table Display name starts with letter ‘a’ or ‘A’ Phone number has CT area code 860 Sorted in alphabetical order of display name
20
Android Intents - http://developer. android
START/FINISH Activity Explicit Service Intent Activity Implicit Service BROAD CAST MSG
21
Android Intents App 1 App 1 App 2 Activity 1 Activity 2 Activity 2
22
Android Intents - Example
Intent intent = new Intent(Actvitity1.class, Activity2.class); startActivity(intent) ; Intent intent = new Intent (Intent.ACTION_DIAL, Uri.parse(“ ”); 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() public void onReceive(Context context, Intent intent) { ArrayList<ContactVO> 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
Create two activities a and b Start activity b with a In activity b send a broadcast message 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
Building User Interfaces - Defining Layouts
You can define the layout using: XML Programmatically (Java Code)
32
Building User Interfaces - Code Review
33
Building User Interfaces – Coding Assignment
Go through the patient database code and run it using ADT emulator. Go through the patient user interface list code run it using ADT emulator Combine code of 1. and 2. to save every newly added patient directly in the database
34
Building User Interfaces - Group Coding Exercise
Goal Create layouts based on templates Work with data inputs Listen to user events Update activity screen Spend time to read the PHA code
35
Building User Interfaces - Layout – Activity Pair
R.Java Layout Generates Used by Activity Layout Activity
36
Building User Interfaces - Sample calculator design
etNum1 etNum2 btnAdd btnDiv btnSub btnMult tvResult
37
Building User Interfaces - Coding Assignment
Add a tip calculator function that adds 15% to the sum of the value of both fields.
38
References http://developer.android.com/training/index.html
Professional Android 4 Application Development (Reto Meier, 2012)
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.