Android Programming: Setup for Android Development Based on material from Adam Champion, Xinfeng Li, C. Horstmann [1], J. Bloch [2], C. Collins et al.

Slides:



Advertisements
Similar presentations
Android Application Development A Tutorial Driven Course.
Advertisements

Google Android Introduction to Mobile Computing. Android is part of the build a better phone process Open Handset Alliance produces Android Comprises.
Android architecture overview
Chapter 1: Voilà! Meet the Android
Programming Mobile Applications with Android
Joemarie Comeros Amparo Android Development Orientation for Starters.
A N I NTRODUCTION TO A NDROID P ROGRAMMING Xinfeng Li CSE OSU.
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Filip Debelić What is it? Android is a mobile operating system (OS) based on the Linux kernel and currently developed by Google Android,
Basic, Basic, Basic Android. What are Packages? Page 346 in text Package statement goes before any import statements Indicates that the class declared.
Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department.
Android Application Development with Java UPenn CS4HS 2011 Chris Murphy
ANDROID PROGRAMMING MODULE 1 – GETTING STARTED
Android Application Development CSE 5520/4520 Wireless Networks.
Emerging Platform#4: Android Bina Ramamurthy.  Android is an Operating system.  Android is an emerging platform for mobile devices.  Initially developed.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Mobile Programming Lecture 1 Getting Started. Today's Agenda About the Eclipse IDE Hello, World! Project Android Project Structure Intro to Activities,
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
Intro to Android Programming George Nychis Srinivasan Seshan.
About me Yichuan Wang Android Basics Credit goes to Google and UMBC.
Cosc 5/4730 Information, Resources, and basic GUI concepts.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
@2011 Mihail L. Sichitiu1 Android Introduction Platform Overview.
Introduction to Android Programming (CS5248 Fall 2015) Aditya Kulkarni August 26, 2015 *Based on slides from Paresh Mayami.
Presentation Seminar on “IMAGE SLIDER –AN ANDROID APPLICATION”
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Social Media Apps Programming Min-Yuh Day, Ph.D. Assistant Professor Department of Information Management Tamkang University
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Android for Java Developers Denver Java Users Group Jan 11, Mike
Android - Broadcast Receivers
ANDROID L. Grewe Components  Java Standard Development Kit (JDK) (download) (latest version)  AndroidStudio.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Recap of Part 1 Terminology Windows FormsAndroidMVP FormActivityView? ControlViewView? ?ServiceModel? Activities Views / ViewGroups Intents Services.
TCS Internal Maps. 2 TCS Internal Objective Objective :  MAPS o Integration of Maps.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
Android Programming: Setup for Android Development Based on material from Adam Champion, Xinfeng Li, C. Horstmann [1], J. Bloch [2], C. Collins et al.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Introduction to Android Programming
COM594: Mobile Technology Practical – Week 1 Android and Android Studio.
Android Programming.
Workshop by T.Naveen sai kumar.
Lab7 – Appendix.
Introduction to android
Android Application -Architecture.
Android Programming - Features
Android Application Development 1 6 May 2018
GUI Programming Fundamentals
Mobile Computing With Android ACST 4550 Intro to Android, Part 1
Android Studio, Android System Basics and Git
Android.
Development-Introduction
CMPE419 Mobile Application Development
Mobile Device Development
CIS 470 Mobile App Development
Application Development A Tutorial Driven Course
CMPE419 Mobile Application Development
HNDIT2417 Mobile Application Development
Android Programming Tutorial
CIS 470 Mobile App Development
Emerging Platform#3 Android & Programming an App
Adding Components to Activity
Objects First with Java
CMPE419 Mobile Application Development
CMPE419 Mobile Application Development
CMPE419 Mobile Application Development
Mobile Programming Broadcast Receivers.
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Android Programming: Setup for Android Development Based on material from Adam Champion, Xinfeng Li, C. Horstmann [1], J. Bloch [2], C. Collins et al. [4], M.L. Sichitiu (NCSU), V. Janjic (Imperial College London), CSE 2221 (OSU), and other sources 1

Outline Introduction to Android Getting Started Android Programming 2

Introduction to Android Popular mobile device OS: 52% of U.S. smartphone market [8] Developed by Open Handset Alliance, led by Google Google claims 900,000 Android device activations [9] Source: [8] 3

What is Android Android is an operating system for mobile devices such as smartphones and tablet computers. It is developed by the Open Handset Alliance led by Google. Android has beaten Apple iOS, being the leading mobile operating system from first quarter of 2011 Version: Android 1.0, 1.1 to 1.5 (Cupcake), 1.6 (Donut), 2.0/2.1 (Eclair), 2.2 (Froyo), 2.3 (Gingerbread), to 3.0 (Honeycomb), 4.0 (Ice Cream Sandwich), 5.0 (Lollipop)

Android Architecture

Outline Introduction to Android Getting Started Android Programming 6

Getting Started (1) Need to install Java Development Kit (JDK) to write Java (and Android) programs – Do not install Java Runtime Environment (JRE); JDK and JRE are different! Can download the JDK for your OS at Alternatively, for OS X, Linux: – OS X: Open /Applications/Utilities/Terminal.app Type javac at command line Install Java when prompt appears – Linux: Type sudo apt–get install default–jdk at command line (Debian, Ubuntu) Other distributions: consult distribution’s documentation 7

Install! 8

Getting Started (2) After installing JDK, download Android SDK from Simplest: download and install Android Studio bundle (including Android SDK) for your OS Alternatives: – Download/install Android Developer Tools from this site (based on Eclipse) – Install Android SDK tools by themselves, then install ADT for Eclipse separately (from this site) We’ll use Android Studio with SDK included (easy) 9

Install! 10

Getting Started (3) Install Android Studio directly (Windows, Mac); unzip to directory android-studio, then run./android-studio/bin/studio.sh (Linux) You should see this: 11

Getting Started (4) Strongly recommend testing with real Android device – Android emulator: very slow – Faster emulator: Genymotion [14], [15] – Install USB drivers for your Android device! Bring up the Android SDK Manager – Recommended: Install Android 2.2, APIs and 4.x API – Do not worry about Intel x86 Atom, MIPS system images Settings Now you’re ready for Android development! 12

Outline Introduction to Android Getting Started Android Programming 13

14

Android Highlights (1) Android apps execute on Dalvik VM, a “clean-room” implementation of JVM – Dalvik optimized for efficient execution – Dalvik: register-based VM, unlike Oracle’s stack-based JVM – Java.class bytecode translated to Dalvik EXecutable (DEX) bytecode, which Dalvik interprets 15

Android Highlights (2) Android apps written in Java 5 – Actually, a Java dialect (Apache Harmony) – Everything we’ve learned still holds Apps use four main components: – Activity : A “single screen” that’s visible to user – Service : Long-running background “part” of app (not separate process or thread) – ContentProvider : Manages app data (usually stored in database) and data access for queries – BroadcastReceiver : Component that listens for particular Android system “events”, e.g., “found wireless device”, and responds accordingly 16

App Manifest Every Android app must include an AndroidManifest.xml file describing functionality The manifest specifies: – App’s Activities, Services, etc. – Permissions requested by app – Minimum API required – Hardware features required, e.g., camera with autofocus – External libraries to which app is linked, e.g., Google Maps library 17

Activity Lifecycle Activity : key building block of Android apps Extend Activity class, override onCreate(), onPause(), onResume() methods Dalvik VM can stop any Activity without warning, so saving state is important! Activities need to be “responsive”, otherwise Android shows user “App Not Responsive” warning: – Place lengthy operations in Runnable Thread s, AsyncTask s Source: [12] 18

App Creation Checklist If you own an Android device: – Ensure drivers are installed – Enable developer options on device under Settings, specifically USB Debugging Android 4.2+: Go to Settings→About phone, press Build number 7 times to enable developer options For Android Studio: – Under File→Settings→Appearance, enable “Show tool window bars”; the Android view shows LogCat, devices – Programs should log states via android.util.Log ’s Log.d(APP_TAG_STR, “debug”), where APP_TAG_STR is a final String tag denoting your app – Other commands: Log.e() (error); Log.i() (info); Log.w() (warning); Log.v() (verbose) – same parameters 19

Creating Android App (1) Creating Android app project in Android Studio: – Go to File→New Project – Enter app, project name – Choose package name using “reverse URL” notation, e.g., edu.osu.myapp – Select APIs for app, then click Next 20

Creating Android App (2) Determine what kind of Activity to create; then click Next – We’ll choose a Blank Activity for simplicity Enter information about your Activity, then click Finish This creates a “Hello World” app 21

Deploying the App Two choices for deployment: – Real Android device – Android virtual device Plug in your real device; otherwise, create an Android virtual device Emulator is slow. Try Intel accelerated version, or perhaps Run the app: press “Run” button in toolbar 22

Underlying Source Code package edu.osu.helloandroid; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } src/…/MainActivity.java 23

Underlying GUI Code <RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> res/layout/activity_main.xml – RelativeLayout s are quite complicated. See [13] for details 24

The App Manifest <manifest xmlns:android=" package="edu.osu.helloandroid" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" > <activity android:name="edu.osu.helloandroid.MainActivity" > AndroidManifest.xml 25

A More Interesting App We’ll now examine an app with more features: WiFi Tester (code on class website) Press a button, scan for WiFi access points (APs), display them 26

Underlying Source Code public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wi_fi); // Set up WifiManager. mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); // Create listener object for Button. When Button is pressed, scan for // APs nearby. Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { boolean scanStarted = mWifiManager.startScan(); // If the scan failed, log it. if (!scanStarted) Log.e(TAG, "WiFi scan failed..."); } }); // Set up IntentFilter for "WiFi scan results available" Intent. mIntentFilter = new IntentFilter(); mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); } 27

Underlying Source Code (2) Code much more complex First get system WifiManager Create listener Object for button that performs scans We register Broadcast Receiver, mReceiver, to listen for WifiManager ’s “finished scan” system event (expressed as Intent WifiManager.SCAN_RESULTS_ AVAILABLE_ACTION ) Unregister Broadcast Receiver when leaving protected void onResume() { super.onResume(); registerReceiver(mReceiver, mIntentFilter); protected void onPause() { super.onPause(); unregisterReceiver(mReceiver); } 28

The Broadcast Receiver private final BroadcastReceiver mReceiver = new BroadcastReceiver() public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) { Log.e(TAG, "Scan results available"); List scanResults = mWifiManager.getScanResults(); mApStr = ""; for (ScanResult result : scanResults) { mApStr = mApStr + result.SSID + "; "; mApStr = mApStr + result.BSSID + "; "; mApStr = mApStr + result.capabilities + "; "; mApStr = mApStr + result.frequency + " MHz;"; mApStr = mApStr + result.level + " dBm\n\n"; } // Update UI to show all this information. setTextView(mApStr); } }; 29

User Interface Updating UI in code private void setTextView(String str) { TextView tv = (TextView) findViewById(R.id.textview); tv.setMovementMethod(new ScrollingMovementMethod()); tv.setText(str); } This code simply has the UI display all collected WiFi APs, makes the text information scrollable UI Layout (XML) <LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" tools:context=".WiFiActivity" android:textStyle="bold" android:gravity="center"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".WiFiActivity" android:scrollbars="vertical"> 30

Android Programming Notes Android apps have multiple points of entry: no main() method – Cannot “sleep” in Android – During each entrance, certain Object s may be null – Defensive programming is very useful to avoid crashes, e.g., if (!(myObj == null)) { // do something } Java concurrency techniques are required – Don’t block the “main” thread in Activities – Implement long-running tasks such as network connections asynchronously, e.g., as AsyncTask s – Recommendation: read [4]; chapter 20 [10]; [11] Logging state via android.util.Log throughout app is essential when debugging (finding root causes) Better to have “too many” permissions than too few – Otherwise, app crashes due to security exceptions! – Remove “unnecessary” permissions before releasing app to public Event handling in Android GUIs entails many listener Object s 31

Concurrency: Thread s (1) Thread : program unit (within process) executing independently Basic idea: create class that implements Runnable interface – Runnable has one method, run(), that contains code to be executed – Example: public class OurRunnable implements Runnable { public void run() { // run code } } Create a Thread object from Runnable and start() Thread, e.g., Runnable r = new OurRunnable(); Thread t = new Thread(r); t.start(); Problem: this is cumbersome unless Thread code is reused 32

Concurrency: Thread s (2) Easier approach: anonymous inner classes, e.g., Thread t = new Thread(new Runnable( { public void run() { // code to run } }); t.start(); Idiom essential for one-time network connections in Activities However, Threads can be difficult to synchronize, especially with UI thread in Activity. AsyncTasks are better suited for this 33

Concurrency: AsyncTasks AsyncTask encapsulates asynchronous task that interacts with UI thread in Activity : public class AsyncTask { protected Result doInBackground(ParamType param) { // code to run in background publishProgress(ProgressType progress); // UI … return Result; } protected void onProgressUpdate(ProgressType progress) { // invoke method in Activity to update UI } } Extend AsyncTask with your own class Documentation at 34

Thank You Any questions? 35

References (1) 1.C. Horstmann, Big Java Late Objects, Wiley, Online: com.proxy.lib.ohio–state.edu/book/–/ http://proquest.safaribooksonline. com.proxy.lib.ohio–state.edu/book/–/ J. Bloch, Effective Java, 2nd ed., Addison–Wesley, Online: safaribooksonline.com.proxy.lib.ohio–state.edu/book/programming/java/ http://proquest. safaribooksonline.com.proxy.lib.ohio–state.edu/book/programming/java/ S.B. Zakhour, S. Kannan, and R. Gallardo, The Java® Tutorial: A Short Course on the Basics, 5th ed., Addison–Wesley, Online: ohio–state.edu/book/programming/java/ http://proquest.safaribooksonline.com.proxy.lib. ohio–state.edu/book/programming/java/ C. Collins, M. Galpin, and M. Kaeppler, Android in Practice, Manning, Online: state.edu/book/programming/android/ state.edu/book/programming/android/ M.L. Sichitiu, 2011, javaReview.ppthttp:// javaReview.ppt 6.Oracle, 7.Wikipedia, 8.Nielsen Co., “Smartphone Milestone: Half of Mobile Subscribers Ages 55+ Own Smartphones”, 22 Apr. 2014, smartphone-milestone-half-of-americans-ages-55-own-smartphones.htmlhttp:// smartphone-milestone-half-of-americans-ages-55-own-smartphones.html 9.Android Open Source Project, 36

References (2) B. Goetz, T. Peierls, J. Bloch, J. Bowbeer, D. Holmes, and D. Lea, Java Concurrency in Practice, Addison-Wesley, 2006, online at