Android development - the basics, MFF UK, 2014
-
Upload
tomas-kypta -
Category
Technology
-
view
219 -
download
2
description
Transcript of Android development - the basics, MFF UK, 2014
![Page 1: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/1.jpg)
Android Development - the basics
Tomáš Kypta
![Page 2: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/2.jpg)
• Android platform and ecosystem"• Android SDK and development tools"• Hello World"• building blocks of Android apps & the
manifest file"• activities, widgets, intents"• toasts
Agenda
![Page 3: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/3.jpg)
• Linux-based operating system"
• open-source (http://source.android.com/)"
• originally phone OS"
• tablet support (since Honeycomb, Android 3.0)
Android platform
![Page 4: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/4.jpg)
• Google TV"
• Google Glass"
• Google Wear
Android platform
![Page 5: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/5.jpg)
• 2003, Android inc."
• 2005, acquired by Google"
• Sep 2008, the first Android phone"
– T-Mobile G1"
• May 2010, Froyo (Android 2.2)"
• Feb 2011, Honeycomb (Android 3.0)
History
![Page 6: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/6.jpg)
• Oct 2011, Ice Cream Sandwich (Android 4.0)"
• July 2012, Jelly Bean (Android 4.1)"
• July 2013, Jelly Bean (Android 4.3)"
• Oct 2013, KitKat (Android 4.4)"
• June 2014, Android L (developer preview)
History
![Page 7: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/7.jpg)
Platform Versions
![Page 8: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/8.jpg)
• thousands of devices"• the most popular mobile platform"• 1.5 million new devices activated every day"– Q2 2013"
• September 3, 2013, 1 billion Android devices have been activated"
• most devices made by Samsung "– 65%, Feb 2014
Android ecosystem
![Page 9: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/9.jpg)
• apps are distributed by app stores"– Google Play, http://play.google.com"– other stores (Amazon, Samsung, …)"
• > 50 billion apps have been installed from Google Play"
• > 1.3 million apps
Google Play
![Page 10: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/10.jpg)
• customers can purchase"
• developers can sell"
• Play Music "
• Play Books "
• Play Movies
Google Play
![Page 11: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/11.jpg)
• selling apps"
– 15 min return period"
• in-app billing"
– freemium model"
• ads"
– AdMob, ...
Monetization
![Page 12: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/12.jpg)
• fragmentation"• manufacturer/carrier enhancements"• updates & support"• openness - low quality apps in Google Play"• malware"– users
Android “problems”
![Page 13: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/13.jpg)
• app can be installed directly"
– .apk file"
• user accepts app permissions when installing or updating the app
Android security
![Page 14: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/14.jpg)
• Verify Apps (Android 2.3+)"
– checks every app install"
• Google Play can remotely uninstall harmful apps
Android security
![Page 15: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/15.jpg)
![Page 16: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/16.jpg)
![Page 17: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/17.jpg)
• programming in “Java”"
– Java SE 7 (KitKat)"
• native apps possible (C++)"
• development tools platform friendly"
–Windows, Linux, Mac OS X
Development
![Page 18: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/18.jpg)
• IDE support"
– Android Studio, IntelliJ IDEA"
– ADT plugin for Eclipse"
– Netbeans"
• you can freely develop on any device
Development
![Page 19: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/19.jpg)
• android - Android SDK and AVD Manager"
• adb - Android Debug Bridge"
• monitor - (ddms & hierarchyviewer)"
• emulator"
• lint, Traceview, ProGuard"
• docs, samples
Android SDK
![Page 20: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/20.jpg)
• compatibility libraries"– v4 - backports lots of newer functionality
to Android 1.6+"– Fragments, NotificationCompat, ViewPager"
– v7"– AppCompat"
– v8"– v13
Support Libraries
![Page 21: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/21.jpg)
• Google Maps"
• In-app Billing"
• Games"
• Google+"
• Authorization
Google Play Services
![Page 22: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/22.jpg)
• AdMob"
• Google Analytics, Flurry, Crittercism
Libraries
![Page 23: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/23.jpg)
![Page 24: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/24.jpg)
• Activity"
• Service"
• Content provider"
• Broadcast receiver"
• AndroidManifest.xml
Android building blocks
![Page 25: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/25.jpg)
• screen with user interface"
• the only visual component"
• example - an email app"
– list of emails"
– details of an email"
– email composition
Activity
![Page 26: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/26.jpg)
• has no UI"
• long-running tasks"
• examples"
–music playback service"
– download service"
– sync service
Service
![Page 27: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/27.jpg)
• managers and shares application data"• data storage doesn’t matter (db, web,
filesystem)"• apps can query and modify data through
content provider"• r/w permissions can be defined"• examples - all system dbs (SMS,
contacts, ...)
Content Provider
![Page 28: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/28.jpg)
• responds to broadcasts"• broadcasts are system wide"• can be registered statically or dynamically"• system or custom messages"• examples - incoming SMS, incoming call,
screen turned off, low baterry, removed SD card, BT device available, ...
Broadcast Receiver
![Page 29: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/29.jpg)
• defines what parts the app have"
• defines which endpoints are exposed"
• minimum/maximum API level"
• permissions"
• declare hardware and software features"
• require configuration
AndroidManifest.xml
![Page 30: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/30.jpg)
• asynchronous message"
• binds components together (all except Content Provider)"
• starting activities"
• starting services and binding to services"
• sending broadcasts
Intent
![Page 31: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/31.jpg)
Hello World
![Page 32: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/32.jpg)
Build
![Page 33: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/33.jpg)
![Page 34: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/34.jpg)
![Page 35: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/35.jpg)
• a subclass of android.app.Activity"
• app usually has many activities"
• activities managed in activity stack"
– newly started activity is placed on the top of the stack
Activity
![Page 36: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/36.jpg)
• activity can be in different states during its lifecycle"
– foreground, visible, stopped, killed"
• when activity state changes a system callback is called
Activity Lifecycle
![Page 37: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/37.jpg)
• onCreate() - activity created"
• onStart() - activity visible for the user"
• onResume() - activity gains user focus
Activity callbacks
![Page 38: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/38.jpg)
• onPause() - system resuming another activity"
• onStop() - activity becoming invisible to the user"
• onDestroy() - before activity is destroyed
Activity callbacks
![Page 39: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/39.jpg)
• onRestart() - called if activity was previously stopped, called prior to onStart()
Activity callbacks
![Page 40: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/40.jpg)
![Page 41: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/41.jpg)
![Page 42: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/42.jpg)
• when configuration changes, activities are destroyed and recreated"
– default behaviour, can be changed"
• properly handle config changes"– onSaveInstanceState(Bundle)
Configuration changes
![Page 43: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/43.jpg)
• starting activity explicitly"– new Intent(context, MyActivity.class)!
• starting activity implicitly"– new Intent(Intent.ACTION_VIEW,
Uri.parse(“http://developer.android.com”))!
• starting activity for result
Intent & Activity
![Page 44: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/44.jpg)
• defined by a hierarchy of views"
• layouts = containers"– LinearLayout, RelativeLayout, FrameLayout, ...
User Interface
![Page 45: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/45.jpg)
• widgets"
– UI objects"
– Button, TextView, EditText, RadioButton, ..."
–WebView
User Interface
![Page 46: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/46.jpg)
• list widgets"
– subclasses of AdapterView"
– display a list of items"
– use adapter to bind list do data"– ListView, GridView, Spinner, ...
User Interface
![Page 47: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/47.jpg)
• provide data for Adapter views"
• are responsible for the creation of items
Adapters
![Page 48: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/48.jpg)
• drawables"
– bitmaps"
– 9-patch png"
– state lists"
– layer lists"
– shape drawables
Resources
![Page 49: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/49.jpg)
• layout"
• strings"
• colors"
• menus"
• dimensions"
• animations
Resources
![Page 50: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/50.jpg)
• arrays"
• ids"
• raw"
• xml"
• ...
Resources
![Page 51: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/51.jpg)
Screen sizes and densities
![Page 52: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/52.jpg)
• How to handle different screen sizes and densities?
Screen sizes and densities
![Page 53: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/53.jpg)
• resources can be created in several versions"
– the best version is selected according to current device configuration in runtime
Resources
![Page 54: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/54.jpg)
• resource units"
– dp - density-independent pixel"
– sp - scale-independent pixel (for fonts)"
– never use px!!!
Resources
![Page 55: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/55.jpg)
• suffixes for resource folders"
– drawables, drawable-mdpi, ..."
– values, values-cs"
– layout, layout-sw640dp"
– drawable-hdpi-v11
Resource qualifiers
![Page 56: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/56.jpg)
• screen density - ldpi, mdpi, hdpi, xhdpi, ..."
• screen size - small, normal, large, xlarge"
• screen orientation - port, land"
• language - en, cs, sk, en-rGB…"
• version - v11, v14, ...
Resource qualifiers
![Page 57: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/57.jpg)
• since Android 3.2"
• w<N>dp - available screen width, w600dp"
• h<N>dp - available screen heights, h720dp"
• sw<N>dp - smallest width (does not change with orientation)
Resource qualifiers
![Page 58: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/58.jpg)
• accessed from code via generated R.java file and resource ids"– view.findViewById(R.id.txt_name)!
– txtName.setText(R.string.txt_name_label)
Resources
![Page 59: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/59.jpg)
Localization?
![Page 60: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/60.jpg)
• How to handle different API levels available on different devices?
Android version fragmentation
![Page 61: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/61.jpg)
• build target"
– API level the app is compiled against"
• AndroidManifest.xml"– <uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="16" />
Android version fragmentation
![Page 62: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/62.jpg)
• handling versions in code"if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {!
! // code for Android < 2.3!
}
Android version fragmentation
![Page 63: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/63.jpg)
private boolean functionalitySupported = false;!
static {!
try {!
checkFunctionalitySupported();!
} catch (NoClassDefFoundError e) {!
!functionalitySupported = false;!
}!
}!
private static void checkFunctionalitySupported() throws ! NoClassDefFoundError {!
! functionalitySupported = android.app.Fragment.class != null;!
}!
Android version fragmentation
![Page 64: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/64.jpg)
• main thread = UI thread"
• do not ever block the UI thread!!!"
• use worker threads for time consuming operations"
• UI toolkit not thread safe - never manipulate UI from a worker thread
Threads
![Page 65: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/65.jpg)
• java.util.logging.Logger
• android.util.Log
Logging
![Page 66: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/66.jpg)
• simple non-modal information"• displayed for a short period of time"• doesn’t have user focus
Toast
![Page 67: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/67.jpg)
SharedPreferences prefs = PreferenceManager!
! .getDefaultSharedPreferences(context);!
SharedPreferences prefs = !
! config.getSharedPreferences(PREFS_FILE_NAME,!
! Activity.MODE_PRIVATE);!
"int storedValue = prefs.getInt(SOME_KEY, defaultValue);!
"SharedPreferences.Editor editor = prefs.edit();!
editor.putInt(SOME_KEY, storedValue);!
editor.commit();
Preferences
![Page 68: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/68.jpg)
• developer.android.com"• android-developers.blogspot.com"• source.android.com"• stackoverflow.com"• youtube.com/androiddevelopers"• svetandroida.cz
Sources
![Page 69: Android development - the basics, MFF UK, 2014](https://reader035.fdocuments.in/reader035/viewer/2022070321/558e46471a28ab5d268b4648/html5/thumbnails/69.jpg)
THE END