Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Application Development Workshop 1 JAVA Introduction Application Fundamentals.

Similar presentations


Presentation on theme: "Android Application Development Workshop 1 JAVA Introduction Application Fundamentals."— Presentation transcript:

1 Android Application Development Workshop 1 JAVA Introduction Application Fundamentals

2 Agenda  Classes, objects, and methods are the basic components used in Java programming.  We have discuss: How to define a class How to create objects How to add data fields and methods to classes How to access data fields and methods to classes Android Application Development Workshop 2

3 Java Indroduction  Java is an object-oriented language, with a syntax similar to C Structured around objects and methods A method is an action or something you do with the object Android Application Development Workshop3

4 Classes  A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Android Application Development Workshop 4 Circle centre radius circumference() area() class classname { field declarations { initialization code } Constructors Methods }

5 Classes  The basic syntax for a class definition:  Bare bone class – no fields, no methods Android Application Development Workshop 5 class ClassName [extends SuperClassName] { [fields declaration] [methods declaration] } public class Circle { // my circle class }

6 Adding Fields: Class Circle with fields Android Application Development Workshop 6  Add fields  The fields (data) are also called the instance varaibles. public class Circle { public double x, y; // centre coordinate public double r; // radius of the circle }

7 Adding Methods Android Application Development Workshop 7  A class with only data fields has no life. Objects created by such a class cannot respond to any messages.  Methods are declared inside the body of the class but immediately after the declaration of data fields.  The general form of a method declaration is: type MethodName (parameter-list) { Method-body; }

8 Adding Methods to Class Circle Android Application Development Workshop 8 public class Circle { public double x, y; // centre of the circle public double r; // radius of circle //Methods to return circumference and area public double circumference() { return 2*3.14*r; } public double area() { return 3.14 * r * r; }

9 Data Abstraction Android Application Development Workshop 9  Declare the Circle class, have created a new data type – Data Abstraction  Can define variables (objects) of that type: Circle aCircle; Circle bCircle;

10 Creating objects of a class Android Application Development Workshop 10  Objects are created dynamically using the new keyword.  aCircle and bCircle refer to Circle objects aCircle = new Circle() ; bCircle = new Circle() ;

11 Accessing Object/Circle Data  Similar to C syntax for accessing data defined in a structure. Android Application Development Workshop 11 ObjectName.VariableName ObjectName.MethodName(parameter-list) Circle aCircle = new Circle(); aCircle.x = 2.0 // initialize center and radius aCircle.y = 2.0 aCircle.r = 1.0

12 Android Application Development Workshop 12 Executing Methods in Object/Circle  Using Object Methods: Circle aCircle = new Circle(); double area; aCircle.r = 1.0; area = aCircle.area(); sent ‘message’ to aCircle

13 Using Circle Class Android Application Development Workshop 13 // Circle.java: Contains both Circle class and its user class //Add Circle class code here class MyMain { public static void main(String args[]) { Circle aCircle; // creating reference aCircle = new Circle(); // creating object aCircle.x = 10; // assigning value to data field aCircle.y = 20; aCircle.r = 5; double area = aCircle.area(); // invoking method double circumf = aCircle.circumference(); System.out.println("Radius="+aCircle.r+" Area="+area); System.out.println("Radius="+aCircle.r+" Circumference ="+circumf); } Output: Radius=5.0 Area=78.5 Radius=5.0 Circumference =31.400000000000002

14 Constructors Android Application Development Workshop 14  constructor: Initializes the state of new objects.  has the same name as the class  can have any of the same access modifiers as class members  similar to methods. A class can have multiple constructors as long as they have different parameter list. Constructors have NO return type.  Constructors with no arguments are called no-arg constructors.

15 Constructor example Android Application Development Workshop 15 class Body { private long idNum; private String name= “empty”; private Body orbits; private static long nextID = 0; Body( ) { idNum = nextID++; } Body(String bodyName, Body orbitsAround) { this( ); name = bodyName; orbits = orbitsAround; }

16 Usage of this  inside a constructor, you can use this to invoke another constructor in the same class. This is called explicit constructor invocation. It MUST be the first statement in the constructor body if exists.  this can also be used as a reference of the current object. It CANNOT be used in a static method Android Application Development Workshop 16

17 Example: usage of this as a reference of the current object class Body { private long idNum; private String name; private Body orbits; private static long nextID = 0; private static LinkedList bodyList = new LinkedList();... Body(String name, Body orbits) { this.name = name; this.orbits = orbits; }... private void inQueue() { bodyList.add(this); }... } Android Application Development Workshop 17

18 Inheritance  Reusability is achieved by INHERITANCE  Java classes Can be Reused by extending a class. Extending an existing class is nothing but reusing properties of the existing classes.  The class whose properties are extended is known as super or base or parent class.  The class which extends the properties of super class is known as sub or derived or child class  A class can either extends another class or can implement an interface Android Application Development Workshop 18

19 Inheritance Android Application Development Workshop 19  class B extends A { ….. } A super class B sub class A B > class B implements A { ….. } A interface B sub class A B >

20 interface implementation  A class can implement an interface, this means that it provides implementations for all the methods in the interface.  Java classes can implement any number of interfaces (multiple interface inheritance). Android Application Development Workshop 20

21 Various Forms of Inheritance Android Application Development Workshop 21 A B A B Hierarchical Inheritance X ABC X ABC MultiLevel Inheritance A B C A B C AB C Multiple Inheritance NOT SUPPORTED BY JAVA AB C SUPPORTED BY JAVA Single Inheritance

22 Forms of Inheritance Android Application Development Workshop 22  Multiple Inheritance can be implemented by implementing multiple interfaces not by extending multiple classes Example : class Z extends A implements C, D { …………} OK AC D Z class Z extends A,B class Z extends A extends B { { OR } WRONG

23 Abstract Class modifier  Abstract modifier means that the class can be used as a superclass only. no objects of this class can be created. can have attributes, even code  all are inherited  methods can be overridden  Used in inheritance hierarchies Android Application Development Workshop 23

24 Interesting Method Modifiers Android Application Development Workshop 24  private/protected/public : protected means private to all but subclasses what if none of these specified?  abstract : no implementation given, must be supplied by subclass. the class itself must also be declared abstract  final : the method cannot be changed by a subclass (no alternative implementation can be provided by a subclass).

25 Interesting Method Modifiers Android Application Development Workshop 25 native : the method is written in some local code (C/C++) - the implementation is not provided in Java (recall assembler routines linked with C) synchronized : only one thread at a time can call the method

26 USE OF super KEYWORD Android Application Development Workshop 26  Can be used to call super class constrctor super();  Can refer to super class instance variables/Methods super.

27 Android Application Development Workshop 27 class A { private int a; A( int a) { this.a =a; System.out.println("This is constructor of class A"); } void print() { System.out.println("a="+a); } void display() { System.out.println("hello This is Display in A"); } } // End of class A class B extends A { private int b; private double c; B(int a,int b,double c) { super(a); this.b=b; this.c=c; System.out.println("This is constructor of class B"); } void show() { print(); System.out.println("b="+b); System.out.println("c="+c); } } // End of class B

28 Packages Android Application Development Workshop 28  Classes can be grouped in a collection called package  Java’s standard library consists of hierarchical packages, such as java.lang and java.util http://java.sun.com/j2se/1.4.2/docs/api  Main reason to use package is to guarantee the uniqueness of class names classes with same names can be encapsulated in different packages tradition of package name: reverse of the company’s Internet domain name e.g. hostname.com -> com.hostname

29 Class importation Android Application Development Workshop 29  Two ways of accessing PUBLIC classes of another package 1) explicitly give the full package name before the class name.  E.g. java.util.Date today = new java.util.Date( ); 2) import the package by using the import statement at the top of your source files (but below package statements). No need to give package name any more.  to import a single class from the java.util package import java.util.Date; Date today = new Date( );  to import all the public classes from the java.util package import java.util.*; Date today = new Date( );  * is used to import classes at the current package level. It will NOT import classes in a sub-package.

30 Naming conventions Android Application Development Workshop 30  Package names: start with lowercase letter  E.g.java.util, java.net, java.io...  Class names: start with uppercase letter  E.g.File, Math...  avoid name conflicts with packages  avoid name conflicts with standard keywords in java system  Variable, field and method names: start with lowercase letter  E.g.x, out, abs...  Constant names: all uppercase letters  E.g.PI...  Multi-word names: capitalize the first letter of each word after the first one  E.g.HelloWorldApp, getName...  Exception class names: (1) start with uppercase letter (2) end with “Exception” with normal exception and “Error” with fatal exception  E.g.OutOfMemoryError, FileNotFoundException

31 Example:Shapes Android Application Development Workshop 31  Shape: color, layerfields draw() draw itself on the screen calcArea() calculates it's own area. serialize() generate a string that can be saved and later used to re-generate the object.

32 Kinds of Shapes Android Application Development Workshop 32  Rectangle  Triangle  Circle Each could be a kind of shape (could be specializations of the shape class). Each knows how to draw itself, etc. Could write code to have all shapes draw themselves, or save the whole collection to a file.

33 Android Application Development Workshop 33 XML Introduction Application Fundamentals

34 Overview  Data exchanges between your app and some other application you might be exchanging data in an open format like Extensible Markup language or XML.  It is a set of rules for encoding documents in machine-readable form.  sharing data on the internet. Android Application Development Workshop 34

35 Overview  XML is plain text.  XML represents data without defining how the data should be displayed.  XML can be transformed into other formats via XSL.  XML can be easily processed via standard parsers. XML files are hierarchical. Android Application Development Workshop 35

36 Overview  A XML document consists out of elements, each element has a start tag, content and an end tag.  A XML document must have exactly one root element,  e.g. one tag which encloses the remaining tags. XML differentiates between capital and non-capital letters. Android Application Development Workshop 36

37 Overview  The Java programming language provides several standard libraries for processing XML files.  The SAX and the DOM XML parsers are available on Android.  Android it is recommended to use the XmlPullParser.  It has a relatively simple API compared to SAX and DOM and is fast and requires less memory then the DOM API. Android Application Development Workshop 37

38 Android Application Development Workshop 38 Android Introduction Application Fundamentals

39 What is Android?  Android is a software package and Linux based operating system for mobile devices  Android is a software stack for mobile devices that includes: Android Application Development Workshop 39 Operating System Linux version 2.6 Services include hardware drivers, power, process and memory management; security and network. The Linux 2.6 kernel handles core system services

40 What is Android? Android Application Development Workshop 40  Middleware  Libraries (i.e. SQLite, OpenGL, WebKit, etc)  Android Runtime (Dalvik Virtual Machine and core libraries)  Application Framework  Abstraction for hardware access; manages application resources and the UI; provides classes for developing applications for Android Applications  Native apps: Contacts, Phone, Browser, etc.  Third-party apps: developer’s applications.

41 History  Andy Rubin has been credited as the father of the Android platform -2003  In Aug 17,2005 Google acquired Android Inc. and later Handed to Open Handset Alliances(OHA)  OHA is consortium of 84 companies such as google, samsung, AKM, Ebay, Intel,LG etc. – established on 5 th Nov 2007  OHA is a business alliance comprised of many of the largest and most successful mobile companies on the planet. Android Application Development Workshop 41

42 History  Google hosts the Android open source project  provides online Android documentation, tools, forums, and the Software Development Kit (SDK) for developers.  Key employees of Android Inc. are Andy Robin, Rich Miner, Nick Sear Android Application Development Workshop 42

43 Version  2008 Google sponsors 1 st Android Developer Challenge T-Mobile G1 announced SDK 1.0 released Android released open source (Apache License) Android Dev Phone 1 released  2009 SDK 1.5 (Cupcake)  New soft keyboard with “autocomplete” feature SDK 1.6 (Donut)  Support Wide VGA SDK 2.0/2.0.1/2.1 (Eclair)  Revamped UI, browser Android Application Development Workshop 43

44  2010 Nexus One released to the public SDK 2.2 (Froyo)  Flash support, tethering SDK 2.3 (Gingerbread)  UI update, system-wide copy-paste  2011 SDK 3.0/3.1/3.2 (Honeycomb) for tablets only  New UI for tablets, support multi-core processors SDK 4.0/4.0.1/4.0.2/4.0.3 (Ice Cream Sandwich)  Changes to the UI, Voice input, NFC Android Application Development Workshop 44 Ice cream Sandwich Android 4.0+ Honeycomb Android 3.0-3.2

45 Flavours of Android Android Application Development Workshop 45

46 Overview  Linux Kernel: memory management, process management, networking, and other operating system services.  Native Libraries: written in C or C++, including: Surface Manager, 2D and 3D graphics, Media codes, SQL database, Browser engine, etc. only to be called by higher level programs Android Application Development Workshop 46

47 Overview  Android Runtime: including the Dalvik virtual machine and the core Java libraries. (not J2SE/J2ME)  Application Framework: Activity manager, Content providers, Resource manager, Notification manager  Applications and Widgets: the real programs display information and interact with users. Android Application Development Workshop 47

48 Layers Relying on Linux Kernel 2.6 for core system services  Power Management  Memory Management  Driver Model  Security Linux Kernel provides an abstraction layer between the H/W and Android Application Development Workshop 48

49 Layers Surface Manager – manages access to the display subsystem Media framework: allows the recording and playback of different media formats SQLite: database engine used for data storage purposes OpenGL: Used to render 2D or 3D graphics content to the screen Android Application Development Workshop 49

50 Layers  FreeType – bitmap and vector font rendering  WebKit - It is the browser engine used to display HTML content.  SGL – the underlying 2D graphics engine.  SSL – stands for Secure Socket Layer which provides security for communications over networks.  libc – A standard C system library. Android Application Development Workshop 50

51 Android Application Development Workshop 51  Core Libraries Providing most of the functionality available in the core libraries of the Java language APIs  Data Structures  Utilities  File Access  Network Access  Graphics

52 Android Application Development Workshop 52 Activity Manager  Each activity has a default window  Most applications have several activities that start each other as needed  Each is implemented as a subclass of the base Activity class Window Manager  responsible for organizing the screen Content Providers Enabling applications to access data from other applications or to share their own data. View  The content of the window is a view or a group of views (derived from View or ViewGroup)  Example of views: buttons, text fields, scroll bars, menu items, check boxes, etc.  View(Group) made visible via Activity.setContentView() method.  manages UI elements Package Manager  manages application packages that are current installed on the device. Telephony Manager  handles making and receiving phone calls. Resource Manager  Providing access to non- code resources (localized strings, graphics, and layout files) Location Manager  obtains the device's position. Notification Manager  Enabling all applications to display customer alerts in the status bar.

53 Architecture Android Application Development Workshop 53 HAL CORE ANDROID + LIBRARIES HAL Multimedia / Graphics TCMD MBM / Boot loader CONNECTIVITY USB BLUETOOTH Wi-Fi CONNECTIVITY MODEM + RIL KERNEL+BSP GPS

54 Life Cycle Seven life cycle methods are defined in the android.app.Activity class: public class Activity { protected void onCreate(Bundle savedInstanceState); protected void onStart(); protected void onRestart(); protected void onResume(); protected void onPause(); protected void onStop(); protected void onDestroy(); } Android Application Development Workshop 54

55 DDMS ■ Task management ■ File management ■ Memory management ■ Emulator interaction ■ Logging ■ Screen captures Android Application Development Workshop 55

56 Exploring the Android Application Framework  The Android application framework is provided in the android.jar file. Android Application Development Workshop 56

57 Android Application Development Workshop 57

58 Android Manifest  It describes the application’s components (activities, services, broadcast receivers, and content providers) and their capabilities in terms of the intents that they can handle.  It declares which permissions are required in order to access protected parts of the Android runtime and also interact with other applications running on the system. Android Application Development Workshop 58

59 Android Manifest  The Android manifest file presents essential information about the application to the system  Check Android platform correctly run the application's code and to grant the necessary privileges during installation.  The Android manifest file provides the following information to the system:  It includes the name, package name, and the version number of the application.  It indicates the minimum version of the API required for running the application. Android Application Development Workshop 59

60 Android Application Development Workshop 60 Android Manifest  Its main purpose in life is to declare the components to the system:...

61 Packaging  The Android Package File (APK) file format is used to package and distribute Android applications.  APK files are actually archive files in ZIP file format. They partially follow the JAR file format, except for the way that the application class files are packaged. APK files contain the following:  META-INF/MANIFEST.MF: This is the JAR manifest file for the package file itself.  META-INF/CERT.SF: This contains SHA1 hashes for the files that are included in the package file. The file is signed by the application developer’s certificate.  META-INF/CERT.RSA: This is the public key of the certificate that is used to sign the CERT.SF file. Android Application Development Workshop 61

62 R file  The R file consists of a number of public static final constants, each one referring to an XML resource. The constants are grouped into interfaces according to XML resource type.  final declaration is one that cannot be changed. Classes, methods, fields, parameters,and local variables can all be final.  Static -only one instance of that object (the object that is declared static) in the class Android Application Development Workshop 62

63 Android Application Development Workshop 63

64 Android Application Development Workshop 64 Broadcast Receivers  Receive and react to broadcast announcements  Extend the class BroadcastReceiver  Examples of broadcasts: Low battery, power connected, shutdown, timezone changed, etc. Other applications can initiate broadcasts

65 Android Application Development Workshop 65 Intents  An intent is an Intent object with a message content.  Activities, services and broadcast receivers are started by intents. ContentProviders are started by ContentResolvers: An activity is started by Context.startActivity(Intent intent) or Activity.startActivityForResult(Intent intent, int RequestCode) A service is started by Context.startService(Intent service) An application can initiate a broadcast by using an Intent in any of Context.sendBroadcast(Intent intent), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast()

66 Android Application Development Workshop 66 Intent Filters  Declare Intents handled by the current application (in the AndroidManifest):... Shows in the Launcher and is the main activity to start Handles JPEG images in some way

67 Android Market http://www.android.com/market/  Has various categories, allows ratings  Have both free/paid apps  Featured apps on web and on phone  The Android Market (and iTunes/App Store) is great for developers Level playing field, allowing third-party apps Revenue sharing Android Application Development Workshop 67

68 Publishing to Android Market  Requires Google Developer Account $25 fee  Link to a Merchant Account Google Checkout Link to your checking account Google takes 30% of app purchase price Android Application Development Workshop 68

69 Android Application Development Workshop 69

70 Conclusion  Apps are written in Java  Bundled by Android Asset Packaging Tool  Every App runs its own Linux process  Each process has it’s own Java Virtual Machine  Each App is assigned a unique Linux user ID  Apps can share the same user ID to see each other’s files Android Application Development Workshop 70

71 Android Application Development Workshop 71


Download ppt "Android Application Development Workshop 1 JAVA Introduction Application Fundamentals."

Similar presentations


Ads by Google