Android Studio
Android Studio
Android Development
Adam C. Champion
CSE 5236: Mobile Application Development
Autumn 2014
Based on material from 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
Getting Started
Android Programming
Linux:
Type sudo
aptget
install
defaultjdk at command line
(Debian, Ubuntu)
Other distributions: consult distributions documentation
3
Install!
Install!
Settings
Outline
Getting Started
Android Programming
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]
10
11
(not
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
14
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
15
Source: [12]
19
<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>
RelativeLayouts are quite complicated. See [13] for details
21
22
23
//
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...");
24
@Override
protected
void
onResume()
{
super.onResume();
registerReceiver(mReceiver,
mIntentFilter);
}
@Override
protected
void
onPause()
{
super.onPause();
unregisterReceiver(mReceiver);
}
25
26
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="http://schemas.android.com/apk/
res/android"
xmlns:tools="http://schemas.android.com/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"
android:id="@+id/button"
android:text="@string/button_text"/>
<TextView
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ap_list"
tools:context=".WiFiActivity"
android:textStyle="bold"
android:gravity="center">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".WiFiActivity"
android:id="@+id/textview"
android:scrollbars="vertical">
</TextView>
</LinearLayout>
27
Example:
29
Concurrency: AsyncTasks
AsyncTask encapsulates asynchronous task that interacts with UI thread
in Activity:
public
class
AsyncTask<Params,
Progress,
Result>
{
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
}
}
31
Thank You
Any questions?
32
References (1)
1.
2.
3.
4.
5.
6.
7.
8.
9.
References (2)
10.
11.
12.
13.
14.
15.
http://bcs.wiley.com/he-bcs/Books?action=index&itemId=1118087887&bcsId=7006
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
https://developer.android.com/guide/components/activities.html
https://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts
https://cloud.genymotion.com/page/doc/#collapse4
http://blog.zeezonline.com/2013/11/install-google-play-on-genymotion-2-0/
34