Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Android Tutorial


Published on

Android Tutorial by Mr. Manjeet Singh, Module Lead, Spice Labs. He is presenting at Verious presents OpenClass - Android Developers Day.

Published in: Technology
  • Thanks for the nice tutorials.. Even this might help... Have a look..
    Are you sure you want to  Yes  No
    Your message goes here

Android Tutorial

  1. 1. Verious Presents OpenClass – Android Developers Day Event Organized on September 24, 2011
  2. 2. Agenda <ul><li>Android System Architecture </li></ul><ul><li>GUI Design </li></ul><ul><li>Services and Broadcast Receivers </li></ul>Android
  3. 3. Android System Architecture
  4. 4. Linux Kernel Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. <ul><li>New Android specific components have been added </li></ul><ul><li>Alarm, Android Shared Memory </li></ul><ul><li>Kernel Memory Killer, Kernel Debugger, Loger </li></ul>
  5. 5. Libraries Android Libraries implementation Android includes a set of C/C++ libraries used by various components of the Android system. These capabilities are exposed to developers through the Android application framework.
  6. 6. Core Libraries <ul><li>System C library - a BSD-derived implementation of the standard C system library (libc), tuned for embedded Linux-based devices </li></ul><ul><li>Surface Manager - Provides a system-wide surface “composer” to render all the surfaces in a frame buffer </li></ul><ul><ul><li>Can combined 2D and 3D surfaces </li></ul></ul><ul><ul><li>Can use OpenGL ES and 2D hardware accelerator for its compositions </li></ul></ul><ul><li>Storage (SQLite) – a powerful and lightweight relational database engine available to all applications </li></ul><ul><li>WebKit – an Application framework that provides foundation for building a web browser. </li></ul>
  7. 7. Core Libraries (Cont..) <ul><li>Media Libraries - based on PacketVideo's OpenCORE; the libraries support playback and recording of many popular audio and video formats, as well as static image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG </li></ul>
  8. 8. Android Runtime <ul><li>Android includes a set of core libraries that provides most of the functionality available in the core libraries of the Java programming language. </li></ul><ul><li>Every Android application runs in its own process, with its own instance of the Dalvik virtual machine . Dalvik has been written so that a device can run multiple VMs efficiently. </li></ul><ul><li>The Dalvik VM executes files in the Dalvik Executable (.dex) format which is optimized for minimal memory footprint </li></ul><ul><li>The Dalvik VM relies on the Linux kernel for underlying functionality such as threading and low-level memory management. </li></ul>
  9. 9. Application Framework <ul><li>Android offers developers the ability to build extremely rich and innovative applications. Developers are free to take advantage of the device hardware, access location information, run background services, set alarms, add notifications to the status bar, and much, much more. </li></ul><ul><li>Application Framework Layer </li></ul><ul><li>Activity Manager handles application lifecycle </li></ul><ul><li>Package Manager holds information about applications loaded in the system </li></ul><ul><li>Windows Manager handles all the application related windows </li></ul><ul><li>View system provides rich and extensible set of Views that can be used to build an application, including lists, grids, text boxes, buttons, and even an embeddable web browser </li></ul>
  10. 10. Application Framework (Cont..) <ul><li>Resource Manager , providing access to non-code resources such as localized strings, graphics, and layout files </li></ul><ul><li>Content Providers that enable applications to access data from other applications (such as Contacts), or to share their own data </li></ul><ul><li>Notification Manager that enables all applications to display custom alerts in the status bar </li></ul><ul><li>Location Manager that provides location information. </li></ul><ul><li>Telephony Manager that provides telephone related events such as incoming/outgoing call </li></ul>
  11. 11. Application <ul><li>Android provides set of core applications including an email client, SMS program, calendar, maps, browser, contacts, and others. All applications are written using the Java programming language. </li></ul>
  12. 12. <ul><li>GUI (Layout) Design </li></ul>
  13. 13. Android UI <ul><li>In an Android application, the user interface is built using View and ViewGroup objects. There are many types of views and view groups, each of which is a descendant of the View class. </li></ul><ul><li>View objects are the basic units of user interface expression on the Android platform. The View class serves as the base for subclasses called &quot;widgets,&quot; which offer fully implemented UI objects, like text fields and buttons. The ViewGroup class serves as the base for subclasses called &quot;layouts,&quot; which offer different kinds of layout architecture, like linear, tabular and relative. </li></ul>
  14. 14. View Hierarchy Layouts (Linear/Relative) It can be TextView, Button or any other Widget
  15. 15. Creating a UI <ul><li>Android provides us Facility of creating UI in two ways: </li></ul><ul><li>1. Creating XML under Layout directory </li></ul><ul><li>2. Create widgets with in Java application </li></ul>
  16. 16. Creating XML under Layout directory <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul><ul><li><LinearLayout xmlns:android=&quot;; </li></ul><ul><li>android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; </li></ul><ul><li>android:orientation=&quot;vertical&quot; > </li></ul><ul><li><TextView android:id=&quot;@+id/text&quot; </li></ul><ul><li>android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; </li></ul><ul><li>android:text=&quot;Hello, I am a TextView&quot; /> </li></ul><ul><li><Button android:id=&quot;@+id/button&quot; </li></ul><ul><li>android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; </li></ul><ul><li>android:text=&quot;Hello, I am a Button&quot; /> </li></ul><ul><li></LinearLayout> </li></ul>
  17. 17. Create widgets with Java application <ul><li>TextView valueTV = new TextView(this); </li></ul><ul><li>valueTV.setText(&quot;hallo hallo&quot;); </li></ul><ul><li>valueTV.setId(5); </li></ul><ul><li>valueTV.setLayoutParams(new LayoutParams( </li></ul><ul><li>LayoutParams.FILL_PARENT, </li></ul><ul><li>LayoutParams.WRAP_CONTENT)); </li></ul>
  18. 18. Difference XML Based UI UI created within Application 1. It is easy to create as one can create UI using design tool. No development skill required for this. 2. This helps developer to create consistant UI. If property of any component is modified in XML then same changes will be reflected everywhere, where component is used. 1. This require testing every time user add any component if developer makes any changes. 2. Developer need to dig into the code and need to find all usage and replace and test every case as he/she may forget to keep update to same properties everywhere. So its time consming process for developers.
  19. 19. Layout resources in Android <ul><li>One thing that often confuses developers new to the Android platform is the handling of layout resources. The xml files describing the layouts are magically transfered into a more efficient binary format behind the scenes and hidden away - leaving the developer with a static reference to the resources via the file. </li></ul><ul><li>By using the setContentView(int layoutResId) method of the Activity class your layout will be displayed on the screen. Behind the scenes the Android platform is creating all the view objects contained in your layout xml file provided to the setContentView(int layoutResId) method. This process of creating view objects out of layout resources is referred to as layout inflation. </li></ul>
  20. 20. LayoutInflater <ul><li>To avoid these errors it's a good habit to place the setContentView(int layoutResId) method call at the very top of the onCreate() method. </li></ul><ul><li>In some cases you will have to do the layout inflation by yourself, i.e. when you want to set a custom view to a Dialog or a Toast. To inflate a view you use the LayoutInflater class. There's a number of different ways to get a handle to a LayoutInflater: </li></ul><ul><li>LayoutInflater inflater = (LayoutInflater)context.getSystemService( </li></ul><ul><li>Context.LAYOUT_INFLATER_SERVICE); OR </li></ul><ul><li>LayoutInflater inflater = LayoutInflater.from(context); </li></ul><ul><li>View view = linflater.inflate(R.layout.inflate_xml, null); </li></ul><ul><li>TextView tv = (TextView) view.findViewById(; </li></ul><ul><li>mainLayout.addView(view); </li></ul>
  21. 21. Android Services and Broadcast Receiver
  22. 22. Services <ul><li>A service is a component which runs in the background, without interacting with the user. </li></ul><ul><li>The Android platform provides a lot of pre-defined services, usually exposed via a Manager class. In your activity you access services via the method getSystemService(). </li></ul><ul><li>Own Services must be declared via the &quot;AndroidManifest.xml&quot;. They run the main thread of their hosting process. Therefore you should run performance intensive tasks in the background . </li></ul>
  23. 23. Own Services <ul><li>You can declare your own service to perform long running operations without user interaction or to supply functionality to other applications. A activity can start a service via the startService() method and stop the service via the stopService() method. If the activity want to interact with the service it can use the bindService() method of the service. This requires an &quot;ServiceConnection&quot; object which allows to connect to the service and which return a IBinder object. This IBinder object can be used by the activity to communicate with the service. </li></ul><ul><li>Once a service is started its onCreate() method is called. Afterwards the onStartCommand() is called with the Intent data provided by the activity. startService also allows to provide a flag with determines the lifecycle behavior of the services </li></ul><ul><li>A Services needs to be declared in the &quot;AndroidManifest.xml&quot; via a <service android:name=&quot;yourclasss&quot;> </service> and the implementing class must extend the class &quot;Service&quot; or one of its subclasses. </li></ul>
  24. 24. Broadcast Receiver A broadcast receiver is a class which extends &quot;BroadcastReceiver&quot; and which is registered as a receiver in an Android Application via the AndroidManifest.xml (or via code). This class will be able to receive intents via the sendBroadcast() method. &quot;BroadCastReceiver&quot; defines the method &quot;onReceive()&quot;. Only during this method your broadcast receiver object will be valid, afterwards the Android system will consider your object as no longer active. Therefore you cannot perform any asynchronous operation. AndroidManifest.XML <receiver android:name=&quot;MyPhoneReceiver&quot;> <intent-filter> <action android:name=&quot;android.intent.action.PHONE_STATE&quot;> </action> </intent-filter> </receiver>
  25. 25. Broadcast Receiver Example <ul><li>public class MyPhoneReceiver extends BroadcastReceiver { </li></ul><ul><li>@Override </li></ul><ul><li>public void onReceive(Context context, Intent intent) { </li></ul><ul><li>Bundle extras = intent.getExtras(); </li></ul><ul><li>if (extras != null) { </li></ul><ul><li>String state = extras.getString(TelephonyManager.EXTRA_STATE); </li></ul><ul><li>if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { </li></ul><ul><li>String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER; </li></ul><ul><li>Log.w(&quot;DEBUG&quot;, phoneNumber); </li></ul><ul><li>} } } } </li></ul>