android introduction

Upload: dineaminshoilo

Post on 14-Oct-2015

12 views

Category:

Documents


0 download

DESCRIPTION

it is very helpful.

TRANSCRIPT

  • Mobile Application Development in AndroidBangladesh-Korea Information Access Center (IAC)Department of Computer Science and Engineering (CSE)Bangladesh University of Engineering and Technology (BUET) E-mail: [email protected]

  • ObjectiveIAC/CSE/BUET - 2012To provide comprehensive knowledge on basic java programmingOOP concepts, classes, objects, data types, conditions, loops, etc.Inheritance, polymorphism, GUIs, threads, etc.To provide the basic building blocks of Android programmingTo create and deploy mobile applications for end users/clientsTo introduce students with the advanced topic of AndroidLocation based services (lbs), multimedia, web, etc.*

    IAC/CSE/BUET - 2012

  • Smart PhonesIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Jan 9,2007: iPhoneIts the ultimate digital device!Display: Multi-TouchWireless: HSPDA, GSM, Wi-fi, BluetoothLocation: A-GPS, Wi-fi, CellularSensors: Accelerometer, Proximity sensor, Ambient light sensor , Gyroscope Camera: 3-5 MegapixelIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Android, Nokia, Goggle-PhoneNokia-PhoneIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Market Size and Share

    Share of worldwide 2010 Q4 Smartphone sales to end users by operating system, according to CanalysRBC estimates there will be 449 million Smartphone users globally by calendar year 2011, up from estimates of 102 million by the end of calendar year 2007 IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Look Out, Apple: Android App Market About To Blow Through 100,000 Apps

    IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Why is mobile programming different?IAC/CSE/BUET - 2012So why cant we just put our desktop code on a mobile device verbatim?Easy answer: mobile devices have different types of resources (RAM, long term storage, etc) than your laptop.Slightly more difficult answer:The field is moving quickly, driven by industry, companies are driven by time to market.Consequence: APIs are specific to devices.Mobile devices have increased RAM daily, so why dont we wait until we have enough?Easy answer, we never willHarder answer:Other differences too.Device manufacturers or other big companies give you the tool chain.The API is restricted:Dont want a program to send data or text without your knowledge!User interaction is different on a mobile device (small screen?)*

    IAC/CSE/BUET - 2012

  • What is Android?A software platform and operating system for mobile devices

    Based on the Linux kernel

    Developed by Google and later the Open Handset Alliance (OHA)

    Allows writing managed code in the Java language

    Possibility to write applications in other languages and compiling it to ARM native code (support ofGoogle? No)

    Unveiling of the Android platform was announced on 5 November 2007 with the founding of OHA

    IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • What is Open Handset Alliance (OHA)?IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Android FeaturesApplication FrameworkDalvik virtual machineIntegrated (webkit) browser2D and 3D graphics APIs with HWSQLiteVideo and audio codecsBluetooth, EDGE, 3G, and WiFiCamera, GPS, compass, and accelerometer

    *IAC/CSE/BUET - 2012

    IAC/CSE/BUET - 2012

  • Android Architecture / Software Stack

  • Program StackLinux Kernel Core services (including hardware drivers, process and memory management, security, network, and power management) are handled by a Linux 2.6 kernel. The kernel also provides an abstraction layer between the hardware and the remainder of the stack.Libraries Running on top of the kernel, Android includes various C/C++ core libraries such as libc and SSL, as well as: multimedia, OpenGL, SQLiteIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Program StackAndroid Run Time What makes an Android phone an Android phone rather than a mobile Linux implementation is the Android run timeCore Libraries While Android development is done in Java, Dalvik is not a Java VM. The core Android libraries provide most of the functionality available in the core Java libraries as well as the Android-specifi c libraries.Dalvik Virtual Machine Dalvik is a register-based virtual machine thats been optimized o ensure that a device can run multiple instances efficiently. It relies on the Linux kernel for threading and low-level memory management.IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Program StackApplication Framework The application framework provides the classes used to create Android applications. It also provides a generic abstraction for hardware access and manages the user interface and application resources.Application Layer All applications, both native and third party, are built on the application layer using the same API libraries. The application layer runs within the Android run time using the classes and services made available from the application framework.IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • ChallengesCPU typically runs 500-600 MhzRAM available to an App may only be a few megabytesDisk (flash) access is very slowLifecycle - apps must pause/quit often, and restore to give the illusion that they are always runningUI designtypical screen may be HVGA (320x480)may be in portrait or landscapevery high DPI - small text may not be readabletouch resolution is very low (~25 pixel)Network access may be slow and (very)intermittent*IAC/CSE/BUET - 2012

    IAC/CSE/BUET - 2012

  • Opportunities100s of millions of mobile phone usersVery high growth, esp. in Asia"Wild wild west" of application developmentthink 1985 on the desktop (if you were born by then)no dominant 3rd party developers.... yetwhat will the killer app categories be?what does it mean to have any app + the internet in your pocket?You can develop for it today!*IAC/CSE/BUET - 2012

    IAC/CSE/BUET - 2012

  • PossibilitiesIAC/CSE/BUET - 2012Augmented Campus Realitycamera + maps + compass = heads up info

    Gamesmulti-player (bluetooth, wifi, cell)location-based (scavenger hunt)

    Custom overlays for mapsweather, traffic, friendsWeb scrapingnews filterssport scoresdook news eliminators

    Distributed computing?10,000 phones all donating their idle timetiny terminal (phone) + fast cpu/network (server)

    Shared whiteboard*

    IAC/CSE/BUET - 2012

  • Android Platform

    HardwareAndroid is not a single piece of hardware; it's a complete, end-to-end software platform that can be adapted to work on any number of hardware configurations. Everything is there, from the bootloader all the way up to the applications.

    IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • PlatformOperating System- Android uses Linux for its device drivers, memory management, process management, and networking.- The next level up contains the Android native libraries. They are all written in C/C++ internally, but youll be calling them through Java interfaces. In this layer you can find the Surface Manager, 2D and 3D graphics, Media codecs, the SQL database (SQLite), and a native web browser engine (WebKit).- Dalvik Virtual Machine. Dalvik runs dex files, which are coverted at compile time from standard class and jar files.

    IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • PlatformNetworking

    GSM mobile-phone technology

    3G

    Edge

    802.11 Wi-Fi networks

    IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • PlatformSecurityAndroid is a multi-process system, in which each application (and parts of the system) runs in its own process. Most security between applications and the system is enforced at the process level through standard Linux facilities, such as user and group IDs that are assigned to applications.

    Additional finer-grained security features are provided through a "permission" mechanism that enforces restrictions on the specific operations that a particular process can perform, and per-URI permissions for granting ad-hoc access to specific pieces of data.

    IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • PlatformPerformanceIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Supported OSWindows XP (32-bit), Vista (32- or 64-bit), or Windows 7 (32- or 64-bit)Mac OS X 10.5.8 or later (x86 only)Linux (tested on Ubuntu Linux, Lucid Lynx)Supported Development Environment (Eclipse IDE based)Eclipse 3.4 (Ganymede) or greater/Eclipse Classic (versions 3.5.1 and higher)JDK 1.5 or 1.6ADT PluginSetting up Development Env.*IAC/CSE/BUET - 2012

    IAC/CSE/BUET - 2012

  • Setting up Development Env.To set up the development environment, we need:Java Development Kit (JDK 1.5+, 1.6 is preferable)Eclipse IDEAndroid SDKADT pluginIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Setting up Development Env.Java Development Kit (JDK)IAC/CSE/BUET - 2012Download Link: http://www.java.com/en/download/index.jspDownload JDK from here and Install JDK*

    IAC/CSE/BUET - 2012

  • Eclipse IDEIAC/CSE/BUET - 2012Download Link: http://eclipse.org/downloads/ *

    IAC/CSE/BUET - 2012

  • Android SDKIAC/CSE/BUET - 2012Link: http://developer.android.com/sdk/index.html *

    IAC/CSE/BUET - 2012

  • Setting Dev. Env.Android SDK SetupIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Setting Dev. Env.Android SDK SetupIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • What is in the SDK?The Android APIs Development ToolsThe Android EmulatorFull DocumentationSample Code Online SupportIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • ADT Plugin: Android Development Tools (ADT) is a plugin for the Eclipse IDE for

    extending the capabilities of Eclipse to set up new Android projectscreating an application UI adding components based on the Android Framework API debugging your applications using the Android SDK tools exporting signed (or unsigned) APKs in order to distribute your applicationIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Installing the ADTIAC/CSE/BUET - 2012Start Eclipse, then select Help > Install New Software....Click Add, in the top-right corner.In the next window, you'll see a list of the tools to be downloaded. Click Next. Read and accept the license agreements, then click Finish.When the installation completes, restart Eclipse.

    *

    IAC/CSE/BUET - 2012

  • Set SDK pathIAC/CSE/BUET - 2012Start Eclipse, then select Window > PreferencesBrowse and show the path of Android SDK.Press Apply and OK.*

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*Hello World !!!Create a very simple applicationRun it on a real deviceRun it on the emulatorExamine its structure

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*Google TutorialWe will follow the tutorial at:http://developer.android.com/resources/tutorials/hello-world.htmlStart Eclipse (Start -> All Programs -> Eclipse)Create an Android Virtual Device (AVD)Create a New Android Project

    IAC/CSE/BUET - 2012

  • Creating an ApplicationStart Eclipse and Go to File>New>Android ProjectWe keep project name HelloAndroid Build Target Android2.1Application name: Hello AndroidPackage name: com.example.helloandroidInitial Activity Name: HelloAndroidMin SDK Version: 6Click Finish

    IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*Package ContentJava code for our activityAll source code hereGenerated Java codeHelps link resources to Java codeLayout of the activityStrings used in the programAll non-code resourcesAndroid ManifestImages

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*Android Manifest

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*ActivityAn Android activity is focused on a single thing a user can do.Most applications have multiple activities

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*Activities start each other

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*Revised HelloAndroid.java package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android by hand"); setContentView(tv); } } Set the view by hand from the programInherit from the Activity Class

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*Run it!

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*/res/layout/main.xml

    Further redirection to /res/values/strings.xml

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*/res/values/strings.xml

    Hello World, HelloAndroid by resources! Hello, Android

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*HelloAndroid.javapackage com.example.helloandroid;

    import android.app.Activity;import android.os.Bundle;public class HelloAndroid extends Activity {

    /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.main); }}Set the layout of the view as described in the main.xml layout

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*/gen/R.java package com.example.helloandroid; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int textview=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*Run it!

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*

  • Accessing UI ElementsTo get access to your UI elements in code, you add identifier attributes to them in the XML definition.You can then use the findViewById method to return a reference to each named item.

    How to get access to it in code:TextView myTextView = (TextView)findViewById(R.id.myTextView);

    IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*

    Alternatively (although its not considered good practice), if you need to, you can create your layout directly in code as shown below:

    public void onCreate(Bundle icicle) {super.onCreate(icicle);LinearLayout.LayoutParams lp;lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);LinearLayout.LayoutParams textViewLP;textViewLP = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);LinearLayout ll = new LinearLayout(this);ll.setOrientation(LinearLayout.VERTICAL);TextView myTextView = new TextView(this);myTextView.setText(Hello World, HelloWorld);ll.addView(myTextView, textViewLP);this.addContentView(ll, lp);}

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*Introduce a bug package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Object o = null; o.toString(); setContentView(R.layout.main); } }

    IAC/CSE/BUET - 2012

  • IAC/CSE/BUET - 2012*Run it!

    IAC/CSE/BUET - 2012

  • Development ToolsThe Android Emulator: An implementation of the Android virtual machine designed to run on your development computer. You can use the emulator to test and debug your android applications.Dalvik Debug Monitoring Service (DDMS): Use the DDMS perspective to monitor and control the Dalvik virtual machines on which youre debugging your applications.Android Asset Packaging Tool (AAPT): Constructs the distributable Android package files (.apk).Android Debug Bridge (ADB): The ADB is a client-server application that provides a link to a running emulator. It lets you copy files, install compiled application packages (.apk), and run shell commands.IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Android ComponentsAndroid applications consist of loosely coupled components, bound using a project manifest that describes each component and how they interact.There are six components that provide the building blocks for your applications:Activities ServicesContent Providers Intents Broadcast ReceiversNotificationsIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Android ComponentsActivities: Your applications presentation layer. Every screen in your application will be an extension of the Activity class. Activities use Views to form graphical user interfaces that display information and respond to user actions. In terms of desktop development, an Activity is equivalent to a Form. Services: The invisible workers of your application. Service components run invisibly, updating your data sources and visible Activities and triggering Notifications. Theyre used to perform regular processing that needs to continue even when your applications Activities arent active or visible. IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Android ComponentsContent Providers: A shareable data store. Content Providers are used to manage and share application databases. Content Providers are the preferred way of sharing data across application boundaries. Android devices include several native Content Providers that expose useful databases like contact information. Intents: A simple message-passing framework. Using Intents, you can broadcast messages system-wide or to a target Activity or Service, stating your intention to have an action performed.The system will then determine the target(s) that will perform any actions as appropriate.IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Android ComponentsBroadcast Receivers Intent broadcast consumers. By creating and registering a Broadcast Receiver, your application can listen for broadcast Intents that match specific filter criteria.Broadcast Receivers will automatically start your application to respond to an incoming Intent, making them ideal for event-driven applications.Notifications A user notification framework. Notifications let you signal users without stealing focus or interrupting their current Activities. Theyre the preferred technique for getting a users attention from within a Service or Broadcast Receiver. For example, when a device receives a text message or an incoming call, it alerts you by flashing lights, making sounds, displaying icons, or showing dialog messages. IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Android ComponentsActivityPresent a visual user interface for one focused endeavor the user can undertakeExample: a list of menu items users can choose fromServicesRun in the background for an indefinite period of timeExample: calculate and provide the result to activities that need itBroadcast ReceiversReceive and react to broadcast announcementsExample: announcements that the time zone has changedIAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

  • Content ProvidersStore and retrieve data and make it accessible to all applications Example: Android ships with a number of content providers for common data types (e.g., audio, video, images, personal contact information, etc.)IntentsHold the content of a messageExample: convey a request for an activity to present an image to the user or let the user edit some text

    IAC/CSE/BUET - 2012*

    IAC/CSE/BUET - 2012

    *****Application ComponentsA central feature of Android is that one application can make use of elements of other applications (provided those applications permit it). For example, if your application needs to display a scrolling list of images and another application has developed a suitable scroller and made it available to others, you can call upon that scroller to do the work, rather than develop your own. Your application doesn't incorporate the code of the other application or link to it. Rather, it simply starts up that piece of the other application when the need arises. For this to work, the system must be able to start an application process when any part of it is needed, and instantiate the Java objects for that part. Therefore, unlike applications on most other systems, Android applications don't have a single entry point for everything in the application (no main() function, for example). Rather, they have essential components that the system can instantiate and run as needed. There are four types of components:

    Activities An activity presents a visual user interface for one focused endeavor the user can undertake. For example, an activity might present a list of menu items users can choose from or it might display photographs along with their captions. A text messaging application might have one activity that shows a list of contacts to send messages to, a second activity to write the message to the chosen contact, and other activities to review old messages or change settings. Though they work together to form a cohesive user interface, each activity is independent of the others. Each one is implemented as a subclass of the Activity base class. An application might consist of just one activity or, like the text messaging application just mentioned, it may contain several. What the activities are, and how many there are depends, of course, on the application and its design. Typically, one of the activities is marked as the first one that should be presented to the user when the application is launched. Moving from one activity to another is accomplished by having the current activity start the next one.

    Services A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it. Each service extends the Service base class. A prime example is a media player playing songs from a play list. The player application would probably have one or more activities that allow the user to choose songs and start playing them.

    Broadcast receivers A broadcast receiver is a component that does nothing but receive and react to broadcast announcements. Many broadcasts originate in system code for example, announcements that the timezone has changed, that the battery is low, that a picture has been taken, or that the user changed a language preference.

    Content providers A content provider makes a specific set of the application's data available to other applications. Android ships with a number of content providers for common data types (audio, video, images, personal contact information, etc.)

    IntentsActivities, services, and broadcast receivers are activated by asynchronous messages called intents. An intent is an Intent object that holds the content of the message. For activities and services, it names the action being requested and specifies the URI of the data to act on, among other things. For example, it might convey a request for an activity to present an image to the user or let the user edit some text. For broadcast receivers, the Intent object names the action being announced. For example, it might announce to interested parties that the camera button has been pressed.

    *