Android Jump Start<br />By Pune Google Technology User Group<br />(Pune-GTUG)<br />
Topics<br />Android Basics<br />Android Building Blocks<br />Building Application<br />
Android Basics<br />Android is a software stack for mobile devices that includes an operating system, middleware and key a...
Android Basics<br />Features<br />Application framework enabling reuse and replacement of components<br />Dalvik virtual m...
Android Basics<br />
Topics<br />Android Basics<br />Android Building Blocks<br />Building Application<br />
Building Blocks<br />Phone Call Comes<br />Data Store(Playlist)<br />Background<br />Process<br />♫♪♫♪♫<br />Activity<br /...
Topics<br />Android Basics<br />Android Building Blocks<br />Building Application<br />
Building Application    .<br />Lets build a new Music Player<br />
Building Application    .<br />Step 1 – You need a Screen (Activity)<br />Views could be<br /><ul><li> Buttons
 Text Views
 etc ….</li></ul>Layouts could be<br /><ul><li> Linear Layout
 Relative Layout
 Table Layout
 etc …..</li></li></ul><li>Building Application    .<br />Activity Life Cycle<br />
Complete LifeCycle<br />Visible LifeCycle<br />Building Application    .<br />Activity Life Cycle made easier<br />onCreat...
Building Application    .<br />What to do in each life cycle methods?<br />onCreate<br />onStart<br />onResume<br />onPaus...
Building Application    .<br />Lets Draw the Screen using Linear Layouts<br />Current Playlist<br />Player<br />
Building Application    .<br />Lets Draw the Screen using Linear Layouts<br />Music file 1<br />Music file 2<br />Music fi...
Building Application    .<br />Lets Draw the Screen using Linear Layouts<br />Music file 1<br />Music file 2<br />Music fi...
Building Application    .<br />Lets Draw the Screen using Relative Layouts<br />Title is aligned to the top and right of t...
Building Application    .<br />Use Layouts<br />Layouts can be defined in different XML files<br />Code can refer to these...
Building Application    .<br />Use Layouts<br />
Building Application    .<br />Use Layouts<br />
Building Application    .<br />Use Layouts<br />
Building Application    .<br />Lets write code to play an mp3 file<br />MediaPlayer mp = new MediaPlayer(); <br />	public ...
Building Application    .<br />Lets attach this code to the button<br />	public void onCreate(final Bundle savedInstanceSt...
Building Application    .<br />Refer following for detailed instructions http://www.helloandroid.com/node/134<br />
Problem with Playing     .in Activity    <br />Phone Calls<br />Pressing Back Button<br />   Will kill you Activity and yo...
Building Application    .<br />Step 2 – Delegate Playing to a Service<br />Define an MusicPlayer.aidl with methods<br />Au...
Building Application    .<br />Service<br />LifeCycle<br />
Building Application    .<br />Service lifecycle (Note)<br />A service can be used in two ways:<br />It can be started and...
Building Application    .<br />Create an aidl Interface<br />
Building Application    .<br />Extend Service, Implement Stub<br />
Building Application    .<br />Declare in Manifest.xml<br />In order to use a remote service first we must add a line to o...
Building Application    .<br />Refer following for detailed instructions http://www.helloandroid.com/node/140<br />
Building Application    .<br />Step 3 – Reuse using Content Provider<br />Old Music Player<br />New Music Player<br />Exis...
Building Application    .<br />Step 3 – Reuse using Content Provider<br />Old Music Player<br />New Music Player<br />?<br...
Building Application    .<br />Content Resolver & Content Providers<br />Content<br />Resolver<br />URI matches<br />URI o...
Building Application    .<br />Content Resolver & Content Providers<br /><ul><li> Extends ContentProvider
 More like a restful Service
 Has a Unique URI
 CRUD Methods</li></ul>Content<br />Provider<br />
Building Application    .<br />Content Resolver & Content Providers<br />String[] projection = new String[] { People._ID, ...
Building Application    .<br />Content Resolver & Content Providers<br />private void getColumnData(Cursor cur){ <br />	if...
http://developer.android.com/guide/topics/providers/content-providers.html<br />
Upcoming SlideShare
Loading in...5
×

Android jumpstart

2,164

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,164
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
47
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Android jumpstart

  1. 1. Android Jump Start<br />By Pune Google Technology User Group<br />(Pune-GTUG)<br />
  2. 2. Topics<br />Android Basics<br />Android Building Blocks<br />Building Application<br />
  3. 3. Android Basics<br />Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.<br />
  4. 4. Android Basics<br />Features<br />Application framework enabling reuse and replacement of components<br />Dalvik virtual machine optimized for mobile devices<br />Integrated browser based on the open source WebKit engine<br />Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional)<br />SQLite for structured data storage<br />Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF)<br />GSM Telephony (hardware dependent)<br />Bluetooth, EDGE, 3G, and WiFi (hardware dependent)<br />Camera, GPS, compass, and accelerometer (hardware dependent)<br />Rich development environment including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Eclipse IDE<br />
  5. 5. Android Basics<br />
  6. 6. Topics<br />Android Basics<br />Android Building Blocks<br />Building Application<br />
  7. 7. Building Blocks<br />Phone Call Comes<br />Data Store(Playlist)<br />Background<br />Process<br />♫♪♫♪♫<br />Activity<br />Service<br />Broadcast<br />Receiver<br />Content Provider<br />Communication is using Intents<br />
  8. 8. Topics<br />Android Basics<br />Android Building Blocks<br />Building Application<br />
  9. 9. Building Application .<br />Lets build a new Music Player<br />
  10. 10. Building Application .<br />Step 1 – You need a Screen (Activity)<br />Views could be<br /><ul><li> Buttons
  11. 11. Text Views
  12. 12. etc ….</li></ul>Layouts could be<br /><ul><li> Linear Layout
  13. 13. Relative Layout
  14. 14. Table Layout
  15. 15. etc …..</li></li></ul><li>Building Application .<br />Activity Life Cycle<br />
  16. 16. Complete LifeCycle<br />Visible LifeCycle<br />Building Application .<br />Activity Life Cycle made easier<br />onCreate<br />onStart<br />Foreground<br />LifeCycle<br />onResume<br />onPause<br />onStop<br />onDestroy<br />
  17. 17. Building Application .<br />What to do in each life cycle methods?<br />onCreate<br />onStart<br />onResume<br />onPause<br />onStop<br />onDestroy<br />
  18. 18. Building Application .<br />Lets Draw the Screen using Linear Layouts<br />Current Playlist<br />Player<br />
  19. 19. Building Application .<br />Lets Draw the Screen using Linear Layouts<br />Music file 1<br />Music file 2<br />Music file 3<br />Music file 4<br />Current Music Info<br />Progress Bar<br />Buttons<br />
  20. 20. Building Application .<br />Lets Draw the Screen using Linear Layouts<br />Music file 1<br />Music file 2<br />Music file 3<br />Music file 4<br />Current Music Info<br />Progress Bar<br />Prev<br />Next<br />Play<br />
  21. 21. Building Application .<br />Lets Draw the Screen using Relative Layouts<br />Title is aligned to the top and right of the image<br />Beautiful World<br />Take That<br />Author is aligned to the bottom and right of the image<br />
  22. 22. Building Application .<br />Use Layouts<br />Layouts can be defined in different XML files<br />Code can refer to these layout xml files<br />
  23. 23. Building Application .<br />Use Layouts<br />
  24. 24. Building Application .<br />Use Layouts<br />
  25. 25. Building Application .<br />Use Layouts<br />
  26. 26. Building Application .<br />Lets write code to play an mp3 file<br />MediaPlayer mp = new MediaPlayer(); <br /> public void play(){<br />mp.setDataSource(PATH_TO_FILE); <br />mp.prepare(); <br />mp.start();<br /> }<br />
  27. 27. Building Application .<br />Lets attach this code to the button<br /> public void onCreate(final Bundle savedInstanceState)<br /> {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);<br /> final Button playButton = (Button)findViewById(R.id.button_play);<br />playButton.setOnClickListener(new OnClickListener() {<br /> @Override<br /> public void onClick(final View arg0)<br /> {<br /> play();<br /> }<br /> });<br /> }<br />
  28. 28. Building Application .<br />Refer following for detailed instructions http://www.helloandroid.com/node/134<br />
  29. 29. Problem with Playing .in Activity <br />Phone Calls<br />Pressing Back Button<br /> Will kill you Activity and your Music will stop playing<br />
  30. 30. Building Application .<br />Step 2 – Delegate Playing to a Service<br />Define an MusicPlayer.aidl with methods<br />AutogeneratesMusicPlayer Interface<br />Extend Service Implement MusicPlayer Stub<br />Declare Service in Manifest.xml<br />Create a ServiceConnection Object<br />Bind to Service<br />Access the service methods<br />
  31. 31. Building Application .<br />Service<br />LifeCycle<br />
  32. 32. Building Application .<br />Service lifecycle (Note)<br />A service can be used in two ways:<br />It can be started and allowed to run until someone stops it or it stops itself. In this mode, it's started by calling Context.startService() and stopped by callingContext.stopService(). It can stop itself by calling Service.stopSelf() or Service.stopSelfResult(). Only one stopService() call is needed to stop the service, no matter how many times startService() was called.<br />It can be operated programmatically using an interface that it defines and exports. Clients establish a connection to the Service object and use that connection to call into the service. The connection is established by calling Context.bindService(), and is closed by calling Context.unbindService(). Multiple clients can bind to the same service. If the service has not already been launched, bindService() can optionally launch it.<br />The two modes are not entirely separate. You can bind to a service that was started with startService(). For example, a background music service could be started by calling startService() with an Intent object that identifies the music to play. Only later, possibly when the user wants to exercise some control over the player or get information about the current song, would an activity establish a connection to the service by calling bindService(). In cases like this, stopService() will not actually stop the service until the last binding is closed.<br />Like an activity, a service has lifecycle methods that you can implement to monitor changes in its state. But they are fewer than the activity methods — only three — and they are public, not protected:<br />void onCreate() <br />void onStart(Intent intent)<br />void onDestroy()<br />
  33. 33. Building Application .<br />Create an aidl Interface<br />
  34. 34. Building Application .<br />Extend Service, Implement Stub<br />
  35. 35. Building Application .<br />Declare in Manifest.xml<br />In order to use a remote service first we must add a line to our AndroidManifest.xml file inside our application tag to define our service, here is that line:<br /><service class=".MusicService" android:process=":remote" /><br />
  36. 36.
  37. 37. Building Application .<br />Refer following for detailed instructions http://www.helloandroid.com/node/140<br />
  38. 38. Building Application .<br />Step 3 – Reuse using Content Provider<br />Old Music Player<br />New Music Player<br />Existing<br />Playlist<br />On Existing<br />Music Player<br />
  39. 39. Building Application .<br />Step 3 – Reuse using Content Provider<br />Old Music Player<br />New Music Player<br />?<br />Existing<br />Playlist<br />On Existing<br />Music Player<br />
  40. 40. Building Application .<br />Content Resolver & Content Providers<br />Content<br />Resolver<br />URI matches<br />URI of Provider<br />Content<br />Provider<br />Cursor<br />Application<br />Application<br />Application<br />Application<br />Content<br />Provider<br />Content<br />Provider<br />Content<br />Provider<br />Content<br />Provider<br />
  41. 41. Building Application .<br />Content Resolver & Content Providers<br /><ul><li> Extends ContentProvider
  42. 42. More like a restful Service
  43. 43. Has a Unique URI
  44. 44. CRUD Methods</li></ul>Content<br />Provider<br />
  45. 45. Building Application .<br />Content Resolver & Content Providers<br />String[] projection = new String[] { People._ID, People._COUNT, People.NAME, People.NUMBER }; <br />Uri contacts = People.CONTENT_URI; <br />Cursor managedCursor = managedQuery(contacts, projection, <br /> // Which columns to return <br /> null, <br /> // Which rows to return (all rows) <br /> null, <br /> // Selection arguments (none) <br /> // Put the results in ascending order by name<br /> People.NAME + " ASC");<br />
  46. 46. Building Application .<br />Content Resolver & Content Providers<br />private void getColumnData(Cursor cur){ <br /> if (cur.moveToFirst()) { <br /> String name; <br /> String phoneNumber; <br />intnameColumn = cur.getColumnIndex(People.NAME); <br />intphoneColumn = cur.getColumnIndex(People.NUMBER); <br /> String imagePath; <br /> do { <br /> // Get the field values <br /> name = cur.getString(nameColumn); <br />phoneNumber = cur.getString(phoneColumn); <br /> // Do something with the values. ... <br /> } while (cur.moveToNext()); <br /> } <br />}<br />
  47. 47. http://developer.android.com/guide/topics/providers/content-providers.html<br />
  48. 48. Building Application .<br />Phone Call Comes<br />Step 4 – Keeping informed - Broadcast Receivers<br />Phone <br />rings<br />Conversation<br />ends<br />Pause the Music Player<br />
  49. 49. Building Application .<br />Understanding Intents<br />Need<br /><ul><li> Class Name</li></ul>Need <br /><ul><li>ACTION
  50. 50. CATEGORY
  51. 51. DATA</li></li></ul><li>Building Application .<br />Understanding Intents<br /><?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.sample“ android:versionCode="1“ android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".HelloWorld"<br />android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /> </application><br /> <uses-sdkandroid:minSdkVersion="4" /><br /></manifest><br />Program launcher shows all the activities which have MAIN Action and LAUNCHER category<br />
  52. 52. Building Application .<br />Understanding Intents<br />So what happens two activities have the exact same intent filter and an intent is fired.<br />Simple you choose one application, and you have an option to tell to se that application as the default application hence forth<br />
  53. 53. Building Application .<br />Understanding Intents<br />Intent to launch an Activity<br /><ul><li>Context.startActivity(intent)
  54. 54. ContextstartActivityForResult(intent)</li></ul>Intent to launch an Service<br /><ul><li>Context.startService(intent)</li></ul>Intent to send a broadcast<br /><ul><li>Context.sendBroadCast(intent)</li></li></ul><li>Building Application .<br />Step 4 – Keeping informed - Broadcast Receivers<br />Add BroadCast Receiver<br />Check Phone status and pause or play the music using service<br /><receiver android:name=".PhoneReceiver" android:enabled="true">      <intent-filter>        <action android:name=          "android.intent.action.PHONE_STATE" />      </intent-filter>    </receiver><br />
  55. 55. Building Application .<br />Step 4 – Keeping informed - Broadcast Receivers<br />public class Receiver extends BroadcastReceiver {<br /> public void onReceive(Context context, Intent intent) {<br /> if (intentAction.equals(ACTION_PHONE_STATE_CHANGED)) {                        pstn_state = intent.getStringExtra("state");                       //Depending on the pstn state pause or play music using the //MusicService<br />                }<br /> }<br />}<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×