Intro to Android

45
Hey, Robot! A casual introduc5on to Android development in Java Ron DuPlain [email protected] @rduplain h@p://go.nrao.edu/android/talk Android @ Richmond JUG ‐ July 15, 2009

description

 

Transcript of Intro to Android

Page 1: Intro to Android

Hey,Robot!

Acasualintroduc5ontoAndroiddevelopmentinJava

[email protected]

@rduplainh@p://go.nrao.edu/android/talk

Android@RichmondJUG‐July15,2009

Page 2: Intro to Android

DiscussionGoals

•  provideanAndroidoverview•  highlightthe(free)AndroidJavaSDK•  introduceAndroidapplica5onfundamentals

•  walkthroughrealexamples

*IamnotaffiliatedwithGoogleoranypartoftheAndroidproject.

Android@RichmondJUG‐July15,2009

Page 3: Intro to Android

AndroidOverview

Android@RichmondJUG‐July15,2009

Page 4: Intro to Android

one‐page•  “AndroidisasoYwarestackformobiledevicesthatincludesanopera5ngsystem,middlewareandkeyapplica5ons.”developer.android.com

•  developmentledbyGoogle•  developedbytheOpenHandsetAlliance•  carriedfirstbyT‐Mobile(G1)•  opensource(ApacheLicense)•  supportedbyhardwarevendors•  portedbyhardwareenthusiasts•  applica5onsonanAndroiddeviceare“equal”

Android@RichmondJUG‐July15,2009

Page 5: Intro to Android

Architecture

Android@RichmondJUG‐July15,2009h@p://developer.android.com/guide/basics/what‐is‐android.html

Page 6: Intro to Android

AndroidDevices

•  HTC/T‐MobileG1

•  HTCMagic

•  “AtLeast18AndroidPhonesComingThisYear”

•  h@p://www.businessweek.com/the_thread/techbeat/archives/2009/05/at_least_18_and.html

•  Portedtootherdevices•  h@p://groups.google.com/group/android‐por5ng

Android@RichmondJUG‐July15,2009

Page 7: Intro to Android

AndroidDevelopment

•  AndroidJavaSDK,withEclipseIDEsupport•  AndroidC/C++Na5veDevelopmentKit– addontoSDK,supportforna5vecode

•  AndroidScrip5ngEnvironment– Python,Lua,BeanShellnow– expectRubyandJavaScriptlater

•  PhoneGap(HTML&JavaScript)

Android@RichmondJUG‐July15,2009

Page 8: Intro to Android

AndroidDevelopment

•  AndroidJavaSDK,withEclipseIDEsupport•  AndroidC/C++Na5veDevelopmentKit– addontoSDK,supportforna5vecode

•  AndroidScrip5ngEnvironment– Python,Lua,BeanShellnow– expectRubyandJavaScriptlater

•  PhoneGap(HTML&JavaScript)

Android@RichmondJUG‐July15,2009

Page 9: Intro to Android

AndroidDevelopers

•  signupfor$25•  publishapplica5onstotheAndroidMarket

•  Market:freeorpaid

•  paidapps:70%todeveloper,~30%tocarriers•  developerscanpurchasedunlockedphones•  alterna5vely,get.apkzipbundlestousers

Android@RichmondJUG‐July15,2009

Page 10: Intro to Android

AndroidJavaSDK

Android@RichmondJUG‐July15,2009

Page 11: Intro to Android

SDKOverview

•  full‐featuredemulator•  AndroidDevelopmentTools(ADT)pluginforEclipseIDE

•  suiteoftools(mostlycommand‐line)– debugging,packaging,&installingapplica5ons– queryingdevices

•  targetemulatorordevice

Android@RichmondJUG‐July15,2009

Page 12: Intro to Android

SDKOverview

•  full‐featuredemulator(nodeviceneeded)•  AndroidDevelopmentTools(ADT)pluginforEclipseIDE

•  suiteoftools(mostlycommand‐line)– debugging,packaging,&installingapplica5ons– queryingdevices

•  targetemulatorordevice

Android@RichmondJUG‐July15,2009

Page 13: Intro to Android

SDKInstalla5on

•  h@p://developer.android.com/sdk•  Windows,Mac,GNU/Linux,x86plaqormsonly

•  grabzipforyourplaqorm,unpack

•  addSDKtoolsto$PATH/%PATH%•  configureIDE– EclipseIDEofficiallysupported– AndroidDevelopmentTools(ADT)pluginforEclipseIDE

Android@RichmondJUG‐July15,2009

Page 14: Intro to Android

NotusingEclipse?

•  h@p://developer.android.com/guide/developing/other‐ide.html

•  walksthroughtoolsusagetoenableyourIDEto– maintainprojects

–  integratewithemulatoranddebugger

Android@RichmondJUG‐July15,2009

Page 15: Intro to Android

EclipseIntegra5on

Android@RichmondJUG‐July15,2009

Page 16: Intro to Android

Emulator

Android@RichmondJUG‐July15,2009

Page 17: Intro to Android

GrabScreenshots

•  emulatorordevice

Android@RichmondJUG‐July15,2009

Page 18: Intro to Android

Applica5onFundamentals

Android@RichmondJUG‐July15,2009

Page 19: Intro to Android

Applica5onComponents

•  Ac?vity–foreground,withUI•  Service–background,noUI•  BroadcastReceiver–forspecifiedIntents•  ContentProvider–supportfunc5onality

Android@RichmondJUG‐July15,2009

Page 20: Intro to Android

Ac5vityLifecycle

Android@RichmondJUG‐July15,2009

Page 21: Intro to Android

ServiceLifecycle

Android@RichmondJUG‐July15,2009

Page 22: Intro to Android

Resources,Layout,UI•  storestrings,colors,constants,drawables,etcinproject/res/

•  Androidauto‐generatesR.java,accessresources•  buildUIlayoutsinJava,or…•  declareUIelementsinXML,manipulateinJava

•  build/assignlistenerstoUIelements(inJava)

Android@RichmondJUG‐July15,2009

Page 23: Intro to Android

Manifest•  defineapplica5oncomponents,requestpermissionsinAndroidManifest.xml

•  (examplescoming)

Android@RichmondJUG‐July15,2009

Page 24: Intro to Android

Intents

•  callouttoanac5vity,aservice,orreceivers•  cancallwithin,ortootherapplica5ons•  bundle“extras”–key/valuepairs•  “backstack”

Android@RichmondJUG‐July15,2009

Page 25: Intro to Android

Ammuni5on

•  Intents•  ContentProviders•  sqlite3•  drawables/layouts(borrowfromfreeprojects)

Android@RichmondJUG‐July15,2009

Page 26: Intro to Android

Status/No5fica5onBar

Android@RichmondJUG‐July15,2009

Page 27: Intro to Android

NotableFeatures

•  canruninbackgroundasservice•  intents•  broadcastintent‐>applica5on•  status/no5fica5onbar

Android@RichmondJUG‐July15,2009

Page 28: Intro to Android

AndroidExamples

fromopensourceapplica5ons

Android@RichmondJUG‐July15,2009

Page 29: Intro to Android

OutlineofExamples

•  BroadcastReceiver&AndroidManifest.xml– SMSPopup’sSMSReceiver

– h@p://code.google.com/p/android‐smspopup/

•  Service– OICountdown’sAlarmService– h@p://code.google.com/p/openintents/

•  Ac5vity,layout,andR.java– OICountdown’sCountdownEditorAc5vity

*Iamnotaffiliatedwiththeseprojects.Seewebsitesforauthors.

Android@RichmondJUG‐July15,2009

Page 30: Intro to Android

BroadcastReceiver

Android@RichmondJUG‐July15,2009

import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;

public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.v("SMSReceiver: onReceive()"); intent.setClass(context, SMSReceiverService.class); intent.putExtra("result", getResultCode()); SMSReceiverService.beginStartingService(context, intent); }

}

Page 31: Intro to Android

AndroidManifest.xml

Android@RichmondJUG‐July15,2009

<receiver android:name=".SMSReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED”/> </intent-filter>

<intent-filter> <action android:name="android.provider.Telephony.WAP_PUSH_RECEIVED”/> <data android:mimeType="application/vnd.wap.mms-message" /> </intent-filter> </receiver>

Page 32: Intro to Android

Result

Android@RichmondJUG‐July15,2009

Page 33: Intro to Android

Service

Android@RichmondJUG‐July15,2009

public class AlarmService extends Service { private static final String TAG = LogConstants.TAG; private static final boolean debug = LogConstants.debug;

Thread mThread; long mEndTime;

@Override public void onCreate() { // Start up the thread running the service. Note that we create a // separate thread because the service normally runs in the process's // main thread, which we don't want to block. mThread = new Thread(null, mTask, "AlarmService"); mThread.start(); } ... }

Page 34: Intro to Android

Result

Android@RichmondJUG‐July15,2009

Page 35: Intro to Android

Ac5vity,whenloaded

Android@RichmondJUG‐July15,2009

Page 36: Intro to Android

Layout

Android@RichmondJUG‐July15,2009

res/layout/countdown_editor.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"

> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginTop="10dip" android:layout_marginBottom="10dip" android:hint="@string/hint_title" /> ... </LinearLayout> </ScrollView>

Page 37: Intro to Android

Strings

Android@RichmondJUG‐July15,2009

res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?> <resources> ... <string name="hint_title">e.g. pizza</string> ... </resources>

Page 38: Intro to Android

R.java

•  Justknowthatit’sauto‐generated.

Android@RichmondJUG‐July15,2009

public final class R { ... public static final class id { ... public static final int countdown=0x7f08000a; ... public static final int title=0x7f080008; ... } ... }

Page 39: Intro to Android

Ac5vity

Android@RichmondJUG‐July15,2009

public class CountdownEditorActivity extends Activity { ... private TextView mCountdownView; ... protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... mCountdownView = (TextView) findViewById(R.id.countdown); ... } ...

private void updateCountdown() { ... mCountdownView.setVisibility(View.VISIBLE); mCountdownView.setText("" + CountdownUtils.getDurationString(delta)); mCountdownView.setTextAppearance(this, android.R.style.TextAppearance_Large); mCountdownView.setTextSize(64); ... } ... }

Page 40: Intro to Android

Result

Android@RichmondJUG‐July15,2009

Page 41: Intro to Android

Result

Android@RichmondJUG‐July15,2009

Page 42: Intro to Android

Conclusions

Android@RichmondJUG‐July15,2009

Page 43: Intro to Android

KeyTakeaways

•  appintegra5onthroughintents•  appscanruninmul5pleways:–  intheforeground,withaUI(Ac?vity)–  inthebackground,withoutaUI(Service)–  triggeredbyintent,eitherdirectorbroadcast(Ac?vityorBroadcastReceiver)

•  third‐partyappscanreplacecoreapps(“equal”)

•  .apkzipbundles,fordirectinstallormarket

Android@RichmondJUG‐July15,2009

Page 44: Intro to Android

AndroidDeveloperChallenge

•  h@p://code.google.com/android/adc/•  opensubmissionAugust1‐31,2009

•  winupto$250,000USD•  winnersannounced~November2009

Android@RichmondJUG‐July15,2009

Page 45: Intro to Android

GetStartedStarthere:•  h@p://developer.android.com/‐overview•  h@p://source.android.com/‐sourcecodeLookforexamplesinAndroidsource.Seealso:•  h@p://code.google.com/p/openintents/‐largecollec5onofsamplesandfreeapplica5ons

•  h@p://code.google.com/p/bites‐android/‐cookbook•  h@p://code.google.com/p/trolly/‐shoppinglist•  h@p://www.swiYp.org/‐FTPserverforthedevice•  h@p://code.google.com/p/android‐smspopup/‐SMS•  (Thanksto@ben_caldwelland@callahadfor5ps.)

Android@RichmondJUG‐July15,2009