Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Android Development Mikko Ahonen [email protected] Skype: mikko.emicode TIES425 Programming for Mobile Terminals Class rooms ● ● 25.09 and 27.09 ● Agora's Beeta hall ● Basic stuff From 02.10 forward (at least to 18.10) ● B213.1 ● Code examples and hands on lessons ● Final exercise Tools & Links Java JDK 6: ●http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse(Eclipse 3.6.2 (Helios) or greater): ●http://www.eclipse.org/downloads/ Android sdk: ● http://developer.android.com/sdk/index.html GitHub: ●https://github.com/ Stack Overflow: ● http://stackoverflow.com/questions/tagged/android Google: ●https://www.google.com Android developer site: ● http://developer.android.com/index.html Final exercise ● To get this course finished you need to do final exercise ● Work in pairs or alone ● Use git (contact me if you don't want to use git for some reason) ● Send me email when you are done: [email protected] ● ● ● – Add name(s) – Add github address This final work should be complete at 30.11.2012 That's it and if you need help, contact me and/or read this great tutorial site: http://www.vogella.com/android.html You don't have to participate for lessons if you don't want to. Final exercise ● Project name: GeoTrack ● Description: “App keeps list of locations where user has been moving while app is on. Software shows trail in map. List holds points and time of locations. When list item is clicked it shows location from map.” ● Activities: ● Main page with some, text, buttons and functionality ● List view that shows locations where user have been – Use custom list items One item contains geo tag as a text and time when geo tag was obtained. Map view that shows locations ● ● – No need to draw path, points are enough – Use Google map (recommended) Final exercise ● ● Layouts: ● Use fragments in splash layout. Map doesn't support fragments. ● List Item layout: Add application icon/picture in left of item – Add Geo tag in top of item and right side of application picture – Add Geo tags time under geo tag and right side of application picture Menu ● ● – Add menu with some functionality Minimum apilevel 8 (Android 2.2) Android OS ● Android is based on Linux with Java programming interface ● ● Virtual machine ● ● Each process has it own VM. Applications are unique, but can “talk” with each other ● ● Multiuser system. Each application is different user Example Android has 3rd party app for managing accounts. Other apps can use this 3rd party app to store and sync user account details Android's own virtual machine : Dalvik Virtual Machine Getting Started 1. Download and install Java JDK6 ●JDK6 is recommended by google ●JDK7 may cause unstable developing environment 1. Download and install Eclipse (if you want to use eclipse) ●Choose “Eclipse Classic” version ●Eclipse 3.6.2 (Helios) or greater 1. Download and install Android SDK ●Latest SDK is found from Android developer site Eclipse ● Android offers plugin for eclipse (Android Development Tools (ADT)) ● Read instructions and install adt (and prey that everything goes well): – ● http://developer.android.com/sdk/installing/installing-adt.html Eclipse and ADT is used in this course Android SDK Manager ● ● In Eclipse go Window → Android SDK Manager Select tools and apis you need ● ● You can install all available packages if you want (this takes time) From api level 8 to up covers 96 % of distributed android devices AVD Manager ● In Eclipse Window → AVD Manager ● ● In AVD Manager you can manage your virtual devices To install new Virtual device – emulator choose new button – Add name – Choose target platform ● If you use goole 3rd party api (ex. Maps) choose target platform with google apis! AVD Manager ● In Eclipse Window → AVD Manager ● To install new Virtual device – emulator choose new button (Continues) – If you want to emulate SD Card, choose size – Choose hardware properties – When all is ready push create AVD button Creating project ● To create new android project: ● File → new → other ● Under Android folder choose “Android Application Project” – Give application name and project name – Choose package name – Usually package names are reverse of your domain name and ends to your project name ● Must be at least to identifiers ● Example: com.example.applicationName, also example.applicationName is valid Select Build SDK ● ● This is your target api Creating project ● To create new android project: – Select Minimum Required SDK – This is minimum api you want for your application Next steps – Choose Icons ● Choose Activity ● Name your activity Press finnish – To test is everything ok, run your project. – Choose Run → Run and select “Run as android application” ● ● GitHub ● Use github to store and share your exercise ● Needs registering – Normal account is free (Everyone can see your projects) – Private account costs some amount of money ● Git is used widely in projects and companies ● To get started go to https://github.com/ – Read Set Up Git – Create A Repository – Start using Git GitHub ● Git files ● ● ● README.md – Describe here your project and instructions etc. – It works like “first page” of your project in github .gitignore – Add folders and files here you don't want to be sent to git. – When you create repository you can add android ignore file there automatically. Other files comes from your machine GitHub ● Commands (command line) ● “git add ./” adds all new files and folders to be commited to git ● “git pull” get all files from git repository to your computer ● ● ● “git commit -a -m “Your message here”” adds files to commit stack and message what you have done “git push origin master” send files from commit stack to git repository master branch If files from your local computer and git repository are changed you need to merge before pushing – “git merge tool” or “git diff tool” ● Great tool for merging files: Meld (for Linux, Windows, OS X) Eclipse interface DDMS DDMS ● File explorer ● ● Can be used for moving files to phone or computer Emulator control ● Emulate different phone actions ● Relevant for this course: – Location Controls ● Emulator don't have gps – Give longitude and latitude and push send – Emulator gets given location from phone gps Application Architecture ● Project ● src(folder) – ● Packagename(package) ● Java files ● R.java libs(folder) – Jars ● assets(folder) ● res(folder) ● – Drawable folders – layout(folder) – values(folder) AndroidManifest.xml (file) Src/Gen Folder ● Src folder contains java file packages of your project ● All java files go under package ● Auto generated R.java file can be found from Src or Gen folders ● R.java contains id's to your resource files ● Automatically created and updated by ADT tools ● Manual changes will be overridden ● R.java is accessed in code example: getString(R.string.stringName); This returns string you that you have named stringName in resources. Libs folder ● Libs folder contains 3rd party libraries ● Libraries are in .jar format ● One of the most important library is Android-support library – Allows low api level phones to use newer api level properties ● Example Fragments Assets folder ● ● Assets folder contains all other data that doesn't belong under other folders To access data under assets use AssetsManager ● AssetsManager allows to read an assets as InputStream ● Example: AssetManager manager = getAssets(); try { InputStream open = manager.open("logo.png"); Bitmap bitmap = BitmapFactory.decodeStream(open); ImageView view = (ImageView) findViewById(R.id.imageView1); view.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); } Res (folder) ● Res folder contains resources, like images and xml configuration files ● Drawable folders holds images ● Layout folder contains Activity layouts definitions ● ● ● Values folder contains definition files for strings, colors, styles, static arrays, integers etc. Menu folder keeps definition for menu Some folders for res directory you need to create by yourself ● Example AccountManager needs some definitions to be stored in xml folder in file authenticator.xml Res folder ● Reference to resources in code ● Context class' getResources() method String hello = this.getResources().getString(R.string.hello_world); ● Reference to resources in XML ● @color/name or @string/name <TextView android:layout_width="wrap_content"... … android:text="@string/hello_world"/> Basic components (UI) ● ● Activity ● visual representation of application ● Activities use Views and Fragments to create userinterface Fragments ● ● components which run in the context of an Activity. Views and ViewGroups ● Views are user interface widgets ● ViewGroups arrange views Activity ● Android applications most important component ● ● Application can have multiple activities Must be declared in Manifest.xml <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> Activity ● In code, class you use as activity should extend activity public class MainActivity extends Activity ● ● Activities can't call each others methods as normal classes in java programming Activities can start other activities in order to perform different actions ● ● Previous activity is paused when new activity starts Paused activities are kept in stack Activity lifecycle/states Activity states ● When to use different states? Examples ● Layout orientation ● Need to store something Activity lifecycle public class MainActivity extends Activity { //called when activity is created @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } Fragments Fragments ● Represents a behavior or a portion of user interface in a Activity ● One activity can have multiple fragments ● One fragment can be used in multiple activities ● Fragments are embedded in activity ● Fragments have a own states like activities ● Activity states affects in fragment states ● – When activity is destroyed fragment is destroyed too – When activity is paused fragment is paused too Views and ViewGroups Views and ViewGroups ● ● ● ViewGroup objects are invisible and defines how views are laid out for user ● LinearLayout ● FrameLayout ● RelativeLayout ● GridLayout View objects are usually UI Widgets like buttons or text fields Layouts using view- and viewgroup objects can be declared in code or in layout xml files. More Android components ● Intents ● Services ● ContentProvider ● BroadcastReceiver ● Widgets Intents ● Intents are asynchronous messages ● ● Can request functionality from other components Components can be registered to Intent via an intent filter ● Manifest.xml : <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> ● Intents are used to start new activities Intents ● Intents carry description what need to be done ● You can add extras to intent ● Extras are used to send data from activity to another ● Broadcasts contain extras and events – GPS is updated and intent is sent to notify changes in GPS Services ● Services are used to perform tasks without user interface ● Service class must extend as a service ● Services will run in backround even if application is not used ContentProvider ● ● ContentProvider provides a structured interface to application data Via a ContentProvider your application can share data with other applications ● Example: Android contains an SQLite database which is used via ContentProvider BroadcastReceiver ● Are used to receive system messages and intents ● BroadcastReceiver is notified by Android system if something specified happens. Examples: – Phone call is received – System boot process is completed Widgets (HomeScreen) ● Interactive components which are used in Android homescreen ● Typically display some data ● Examples: ● Clock widget shows time ● Play store widget shows some new applications from google play ● Calendar widget shows scheduled events Logcat ● Used for android development logging ● Uses android.util.Log class ● Methods ● Log.i() – ● Log.d() – ● Send a WARN log message. Log.e() – ● Send a DEBUG log message. Log.w() – ● Send an INFO log message. Send an ERROR log message. Log.wtf() – What a Terrible Failure: Report a condition that should never happen.