Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Write Your First Android App 9/3/2014 About Me ● EveryLabs ○ Co-founder & Android Lead ● Mustbin ○ Software Architect & Android Lead ● RunKeeper ○ Lead Android Developer ● Professional Android Sensor Development ○ co-author Agenda ● Go over some app basics ● Write a simple “Hello Android” app ● Walk through a more complicated app What is an Android “app” ● Package of Android components ● Resources used by those Android Components ● Usually written in Java ● Distributed in an APK ● Runs on Android Components of an Android App ● ● ● ● ● Activity ContentProvider Service BroadcastReceiver Most apps won’t use all four, but all apps will use some Activity ● Represents a “screen” in the app ● Well defined lifecycle ● UI can be specified in XML or code ContentProvider ● ● ● ● Abstracts database access Well supported in Android Cumbersome at times Highly recommended ○ A bit of a “holy war” Service ● Not associated with a screen ○ Background task ● (Usually) runs on the UI thread ● (Usually) runs in same process as rest of app BroadcastReceiver ● Handles broadcasts ○ System-wide ○ In-app ■ LocalBroadcastReceiver ● Asynchronous ● May not persist in memory through subsequent executions Intents ● The “glue” that ties Android components together ○ Starts Activities ○ Starts Services ○ Launches BroadcastReceivers ● System-wide messages ● Components can declare Intent filters ● Passes data between components Implicit vs Explicit Intents ● Explicit ○ Start component by name ○ “Start Activity MyCameraActivity.class” ● Implicit ○ Declare an action ○ “Start an Activity that can take a picture” Back Stack ● Apps usually contain a stack of Activities ○ “Back Stack” Source: https://developer.android.com/guide/components/tasks-and-back-stack.html Back Stack ● Apps can contain activities from other apps ○ This allows developers to include functionality from other apps directly into theirs Task ● A collection of Activities ● Capable of being moved into the background ○ Pressing home button ○ All activities in Task are stopped ■ Activities can be destroyed if they are not visible and the system needs resources Source: https://developer.android.com/guide/components/tasks-and-back-stack.html How apps “live” in Android ● Installed as a different user in Android ○ Android is a multi-user Linux OS ● Each app has it’s own home directory ○ Private from other apps (unless device is rooted) ● Apps run in their own process ● An app is uniquely identified by it’s “package name” App Execution ● By default, all app components run in the same process and thread. ○ Referred to as the “main” or “UI” thread ● Main thread dispatches events to UI widgets ● Android will NOT tolerate long operations of the main thread ○ The OS will kill an app if it blocks the main thread App Execution ● Worker threads can perform long tasks ● All UI manipulation MUST happen on the main thread ○ A runtime exception will be thrown otherwise ● The Android SDK provides support for long running tasks ○ AsyncTask ○ Loaders Activity Lifecycle Source: https://developer.android.com/training/basics/activity-lifecycle/starting.html Activity Lifecycle ● onCreate() ○ ○ ○ ○ Called once per instance Load layout Initialization Not visible ● onStart() ○ Becomes visible Activity Lifecycle ● onResume() ○ Activity in foreground ● onPause() ○ Activity no longer in foreground, but visible ■ Blocked by dialog ● onStop() ○ Activity not visible Activity Lifecycle ● onDestroy() ○ Activity is going away ○ onCreate() will be called again if Activity is needed later Defining the UI for an Activity ● Hierarchy of Views and ViewGroups ● Usually defined in XML ● Can also use Java code Source: https://developer.android.com/training/basics/firstapp/building-ui.html ViewGroup ● Organizes views on the screen ● Consists of views and other ViewGroups ● Layouts ○ ○ ○ ○ LinearLayout RelativeLayout FrameLayout GridView LinearLayout ● Positions children in a single direction ○ Vertical ○ Horizontal Source: https://developer.android.com/guide/topics/ui/layout/linear.html RelativeLayout ● Positions children relative to each other ○ ○ ○ ○ Source: https://developer.android.com/guide/topics/ui/layout/relative.html “Left Of” “Right Of” “Below” “Above” GridView ● Arranges children in a grid Source: https://developer.android.com/guide/topics/ui/layout/gridview.html Views ● ● ● ● ● EditText TextView Button RadioButton CheckBox XML Layout Example <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> Associating XML layout in Activity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello_world); } “R” class ● auto-generated by the Android tools ● Provides access to app resources ○ ○ ○ ○ Strings Layouts Misc. values Drawables (images) Android Project Structure (Gradle) ● app ○ main app ● app/src/main/java ○ Java code ● app/src/main/res ○ app resources Resource Qualifiers ● Provides different variations of same resource ● Android will select proper resource at runtime Resource Qualifier Examples ● ● ● ● Screen size Screen density Android version Language Android Manifest ● Contains meta-data about app ○ Components ○ Permissions ○ Features ■ camera ■ GPS ○ Minimum Android version ○ Launcher Activity ○ Package name Manifest Example <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.adstrosoftware.tsw"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS_SYNC_SETTINGS" /> <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" /> <application android:name=".Application" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".NotesActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> Manifest Example <activity android:name="com.evernote.client.android.EvernoteOAuthActivity" android:configChanges="orientation|keyboardHidden" /> <provider android:name=".provider.TswProvider" android:authorities="com.adstrosoftware.tsw.provider" android:exported="false" /> <service android:name="com.adstrosoftware.tsw.sync.AuthenticatorService"> <intent-filter> <action android:name="android.accounts.AccountAuthenticator"/> </intent-filter> <meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator" /> </service> </application> </manifest> Hello Android App ● ● ● ● Android Studio Gradle JDK (6 or 7) Genymotion ○ Emulation replacement ○ Much faster Where to go from here ● Android developer docs ○ https://developer.android.com/index.html ● Android design ○ https://developer.android.com/design/index.html ○ https://developer.android. com/preview/material/index.html Code and Contact Info Email: [email protected] Twitter: @adstro Slides: http://goo.gl/A5WyRH https://github.com/adstro/hello-android https://github.com/adstro/reading-list Attribution Portions of this presentation are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License. Questions?