Intro to Android
description
Transcript of Intro to Android
Hey,Robot!
Acasualintroduc5ontoAndroiddevelopmentinJava
@rduplainh@p://go.nrao.edu/android/talk
Android@RichmondJUG‐July15,2009
DiscussionGoals
• provideanAndroidoverview• highlightthe(free)AndroidJavaSDK• introduceAndroidapplica5onfundamentals
• walkthroughrealexamples
*IamnotaffiliatedwithGoogleoranypartoftheAndroidproject.
Android@RichmondJUG‐July15,2009
AndroidOverview
Android@RichmondJUG‐July15,2009
one‐page• “AndroidisasoYwarestackformobiledevicesthatincludesanopera5ngsystem,middlewareandkeyapplica5ons.”developer.android.com
• developmentledbyGoogle• developedbytheOpenHandsetAlliance• carriedfirstbyT‐Mobile(G1)• opensource(ApacheLicense)• supportedbyhardwarevendors• portedbyhardwareenthusiasts• applica5onsonanAndroiddeviceare“equal”
Android@RichmondJUG‐July15,2009
Architecture
Android@RichmondJUG‐July15,2009h@p://developer.android.com/guide/basics/what‐is‐android.html
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
AndroidDevelopment
• AndroidJavaSDK,withEclipseIDEsupport• AndroidC/C++Na5veDevelopmentKit– addontoSDK,supportforna5vecode
• AndroidScrip5ngEnvironment– Python,Lua,BeanShellnow– expectRubyandJavaScriptlater
• PhoneGap(HTML&JavaScript)
Android@RichmondJUG‐July15,2009
AndroidDevelopment
• AndroidJavaSDK,withEclipseIDEsupport• AndroidC/C++Na5veDevelopmentKit– addontoSDK,supportforna5vecode
• AndroidScrip5ngEnvironment– Python,Lua,BeanShellnow– expectRubyandJavaScriptlater
• PhoneGap(HTML&JavaScript)
Android@RichmondJUG‐July15,2009
AndroidDevelopers
• signupfor$25• publishapplica5onstotheAndroidMarket
• Market:freeorpaid
• paidapps:70%todeveloper,~30%tocarriers• developerscanpurchasedunlockedphones• alterna5vely,get.apkzipbundlestousers
Android@RichmondJUG‐July15,2009
AndroidJavaSDK
Android@RichmondJUG‐July15,2009
SDKOverview
• full‐featuredemulator• AndroidDevelopmentTools(ADT)pluginforEclipseIDE
• suiteoftools(mostlycommand‐line)– debugging,packaging,&installingapplica5ons– queryingdevices
• targetemulatorordevice
Android@RichmondJUG‐July15,2009
SDKOverview
• full‐featuredemulator(nodeviceneeded)• AndroidDevelopmentTools(ADT)pluginforEclipseIDE
• suiteoftools(mostlycommand‐line)– debugging,packaging,&installingapplica5ons– queryingdevices
• targetemulatorordevice
Android@RichmondJUG‐July15,2009
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
NotusingEclipse?
• h@p://developer.android.com/guide/developing/other‐ide.html
• walksthroughtoolsusagetoenableyourIDEto– maintainprojects
– integratewithemulatoranddebugger
Android@RichmondJUG‐July15,2009
EclipseIntegra5on
Android@RichmondJUG‐July15,2009
Emulator
Android@RichmondJUG‐July15,2009
GrabScreenshots
• emulatorordevice
Android@RichmondJUG‐July15,2009
Applica5onFundamentals
Android@RichmondJUG‐July15,2009
Applica5onComponents
• Ac?vity–foreground,withUI• Service–background,noUI• BroadcastReceiver–forspecifiedIntents• ContentProvider–supportfunc5onality
Android@RichmondJUG‐July15,2009
Ac5vityLifecycle
Android@RichmondJUG‐July15,2009
ServiceLifecycle
Android@RichmondJUG‐July15,2009
Resources,Layout,UI• storestrings,colors,constants,drawables,etcinproject/res/
• Androidauto‐generatesR.java,accessresources• buildUIlayoutsinJava,or…• declareUIelementsinXML,manipulateinJava
• build/assignlistenerstoUIelements(inJava)
Android@RichmondJUG‐July15,2009
Manifest• defineapplica5oncomponents,requestpermissionsinAndroidManifest.xml
• (examplescoming)
Android@RichmondJUG‐July15,2009
Intents
• callouttoanac5vity,aservice,orreceivers• cancallwithin,ortootherapplica5ons• bundle“extras”–key/valuepairs• “backstack”
Android@RichmondJUG‐July15,2009
Ammuni5on
• Intents• ContentProviders• sqlite3• drawables/layouts(borrowfromfreeprojects)
Android@RichmondJUG‐July15,2009
Status/No5fica5onBar
Android@RichmondJUG‐July15,2009
NotableFeatures
• canruninbackgroundasservice• intents• broadcastintent‐>applica5on• status/no5fica5onbar
Android@RichmondJUG‐July15,2009
AndroidExamples
fromopensourceapplica5ons
Android@RichmondJUG‐July15,2009
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
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); }
}
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>
Result
Android@RichmondJUG‐July15,2009
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(); } ... }
Result
Android@RichmondJUG‐July15,2009
Ac5vity,whenloaded
Android@RichmondJUG‐July15,2009
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>
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>
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; ... } ... }
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); ... } ... }
Result
Android@RichmondJUG‐July15,2009
Result
Android@RichmondJUG‐July15,2009
Conclusions
Android@RichmondJUG‐July15,2009
KeyTakeaways
• appintegra5onthroughintents• appscanruninmul5pleways:– intheforeground,withaUI(Ac?vity)– inthebackground,withoutaUI(Service)– triggeredbyintent,eitherdirectorbroadcast(Ac?vityorBroadcastReceiver)
• third‐partyappscanreplacecoreapps(“equal”)
• .apkzipbundles,fordirectinstallormarket
Android@RichmondJUG‐July15,2009
AndroidDeveloperChallenge
• h@p://code.google.com/android/adc/• opensubmissionAugust1‐31,2009
• winupto$250,000USD• winnersannounced~November2009
Android@RichmondJUG‐July15,2009
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