CSE2102 Introduction to Software Engineering

Download Report

Transcript CSE2102 Introduction to Software Engineering

Introduction to Android
http://developer.android.com/training/index.html
Outline
1.
Android Basics
1.
2.
3.
2.
Activity and Services
1.
2.
3.
Intent Objects, Intent Resolution, Intent Filters
Building User Interfaces
1.
6.
https://github.com/sob05001/android-patient-database
Android Intents
1.
5.
Function, Lifecycle, Coding Assignment
https://github.com/sob05001/android-sample-activity-service-msg-passing
Content Providers and SQLite Databases
1.
4.
Background
Setup Environment
Architecture Diagram
https://github.com/sob05001/android-patientlist
References
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
Android Basics - Setup Environment
• Download Android Developer Tools (ADT)
http://developer.android.com/sdk/index.html
• Open Android Developer Tools (ADT)
• Create an empty Android Project
• Sit together in teams such that everyone has
an ADT screen in front.
Android Basics - Architecture Diagram
http://developer.android.com/images/system-architecture.jpg
Activity and Services What are the functions of an activity?
1.
2.
3.
4.
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
1.
5.
setContentView(R.layout.home_activity);
Methods
1.
2.
3.
4.
5.
6.
7.
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
Activity and Services What is the lifecycle of an activity?
http://developer.android.com/training/basics/activity-lifecycle/starting.html
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
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.
2.
3.
4.
5.
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
Activity and Services Service - Function
Services must be declared in AndroidManifest.xml
<service
android:name=“path.to.MyAppService"
android:label="@string/service_name">
</service>
Remember to update the string resource file!
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);
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
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?
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.
2.
3.
4.
Faster access to locally stored data
Less internet traffic Less power consumption
Application works offline
Increased data privacy
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 (http://www.sqlite.org/)
– Visibility: Only to application that created it.
– Open-source (http://www.sqlite.org/copyright.html)
– Standard-compliant (ISO/IEC 9075)
• http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detai
l.htm?csnumber=53685
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
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”
http://developer.android.com/guide/topics/providers/contacts-provider.html
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?
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
Android Intents http://developer.android.com/guide/components/intents-filters.html
START/FINISH
Activity
Explicit
Service
Intent
Activity
Implicit
Service
BROAD
CAST MSG
Android Intents
Activity 2
Activity 1
App 1
Activity 1
Activity 2
App 1
App 2
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?
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<ContactVO> cList = intent.getParcelableArrayListExtra("data");
render(cList);}};
Activity B
Intent i = new Intent("BROADCASTNAME");
i.putParcelableArrayListExtra("data", cListNew);
sendBroadcast(i);
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?
Android Intents - Coding Assignment
1.
2.
3.
4.
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?
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.
Building User Interfaces Assign UI to Activity
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
http://developer.android.com/guide/topics/ui/declaring-layout.html
Building User Interfaces Introducing Layouts
http://developer.android.com/guide/topics/ui/declaring-layout.html
Building User Interfaces Defining Layouts
Building User Interfaces Defining Layouts
• You can define the layout using:
– XML
– Programmatically (Java Code)
Building User Interfaces Code Review
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
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. http://developer.android.com/index.html
Building User Interfaces Layout – Activity Pair
Layout Generates
Layout
R.Java
Used by Activity
Activity
Building User Interfaces Sample calculator design
etNum1
etNum2
btnAdd
btnDiv
btnSub
btnMult
tvResult
http://startandroid.ru/en/lessons/complete-list/225-lesson-19-creating-a-simple-calculator.html
Building User Interfaces Coding Assignment
• https://github.com/sob05001/androidcalculator
• Add a tip calculator function that adds 15% to
the sum of the value of both fields.
References
• http://developer.android.com/training/index.
html
• Professional Android 4 Application
Development (Reto Meier, 2012)
• http://startandroid.ru/en/lessons/completelist/225-lesson-19-creating-a-simplecalculator.html