Android Programming
Android Programming
Android Programming
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.
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
http://java.oracle.com
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 aptget install defaultjdk at command line
(Debian, Ubuntu)
Other distributions: consult distributions documentation 7
Install!
8
Getting Started (2)
After installing JDK, download Android SDK
from http://developer.android.com
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)
Well 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 Settings
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, 2.3.3 APIs and
4.x API
Do not worry about Intel x86 Now youre ready for Android development!
Atom, MIPS system images 12
Outline
Introduction to Android
Getting Started
Android Programming
13
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 Oracles 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 weve learned still holds
Apps use four main components:
Activity: A single screen thats 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:
Apps 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 Threads,
AsyncTasks
18
Source: [12]
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 SettingsAbout phone, press Build number 7
times to enable developer options
For Android Studio:
Under FileSettingsAppearance, enable Show tool
window bars; the Android view shows LogCat, devices
Programs should log states via android.util.Logs
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 FileNew 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
Well 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
http://www.genymotion.com/
Run the app: press Run
button in toolbar
22
Underlying Source Code
src//MainActivity.java
package edu.osu.helloandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
@Override
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;
}
} 23
Underlying GUI Code
res/layout/activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="edu.osu.helloandroid.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> 25
A More Interesting App
Well 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 (1)
@Override
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();
28
The Broadcast Receiver
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
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<ScanResult> 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 UI Layout (XML)
<LinearLayout
private void setTextView(String str) xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
{ android:layout_width="fill_parent"
android:layout_height="fill_parent"
TextView tv = (TextView) android:orientation="vertical">
findViewById(R.id.textview); <Button
android:layout_width="fill_parent"
tv.setMovementMethod(new android:layout_height="wrap_content"
ScrollingMovementMethod()); android:id="@+id/button"
android:text="@string/button_text"/>
tv.setText(str);
<TextView
} android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ap_list"
Any questions?
35
References (1)
1. C. Horstmann, Big Java Late Objects, Wiley, 2012. Online: http://proquest.safaribooksonline.
com.proxy.lib.ohiostate.edu/book//9781118087886
2. J. Bloch, Effective Java, 2nd ed., AddisonWesley, 2008. Online: http://proquest.
safaribooksonline.com.proxy.lib.ohiostate.edu/book/programming/java/9780137150021
3. S.B. Zakhour, S. Kannan, and R. Gallardo, The Java Tutorial: A Short Course on the Basics,
5th ed., AddisonWesley, 2013. Online: http://proquest.safaribooksonline.com.proxy.lib.
ohiostate.edu/book/programming/java/9780132761987
4. C. Collins, M. Galpin, and M. Kaeppler, Android in Practice, Manning, 2011. Online:
http://proquest.safaribooksonline.com.proxy.lib.ohio
state.edu/book/programming/android/9781935182924
5. M.L. Sichitiu, 2011, http://www.ece.ncsu.edu/wireless/MadeInWALAN/AndroidTutorial/PPTs/
javaReview.ppt
6. Oracle, http://docs.oracle.com/javase/1.5.0/docs/api/index.html
7. Wikipedia, https://en.wikipedia.org/wiki/Vehicle_Identification_Number
8. Nielsen Co., Smartphone Milestone: Half of Mobile Subscribers Ages 55+ Own
Smartphones, 22 Apr. 2014, http://www.nielsen.com/us/en/insights/news/2014/
smartphone-milestone-half-of-americans-ages-55-own-smartphones.html
9. Android Open Source Project, http://www.android.com
36
References (2)
10. http://bcs.wiley.com/he-bcs/Books?action=index&itemId=1118087887&bcsId=7006
11. B. Goetz, T. Peierls, J. Bloch, J. Bowbeer, D. Holmes, and D. Lea, Java Concurrency in
Practice, Addison-Wesley, 2006, online at
http://proquest.safaribooksonline.com/book/programming/java/0321349601
12. https://developer.android.com/guide/components/activities.html
13. https://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts
14. https://cloud.genymotion.com/page/doc/#collapse4
15. http://blog.zeezonline.com/2013/11/install-google-play-on-genymotion-2-0/
37