Download App Development for Android

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
App Development for Android
Prabhaker Matet
Development Tools
• (Android) Java
– Java is the same. But, not all libs are included.
– Unused: Swing, AWT, SWT, lcdui
•
•
•
•
Android Studio (includes Intellij IDEA)
Android SDK developer.android.com/
Android Device Emulator
Development Platforms: Linux, Mac OSX, or
Windows
Mateti/Android
2
Applicaton Runtme
• Each applicaton is a different “user”.
• Each applicaton gets a unique Linux user ID.
The system sets permissions for all the files in
an applicaton so that only the user ID
assigned to that applicaton can access them.
• Each process has its own Java VM (Dalvik/Art).
• Every applicaton runs in its own Linux
process. A process can have multple threads.
Mateti/Android
3
Applicaton Framework
• Views lists, grids, text boxes, buttons,
embeddable web browser
• Content Providers to access data from other
applicatons, or to share their own data
• Resource Manager access non-code resources;
e.g., strings, graphics, and layout files
• Notficaton Manager alerts in the status bar
• Actvity Manager lifecycle of applicatons and
navigaton backstack
Mateti/Android
4
Applicaton Components
• Activity: (GUI) functons that the applicaton performs.
• Service:
– run in the background; long-running; for remote processes
– no user interface.
• Content Providers facilitate data transmission among
different applicatons.
• Broadcast Receiver: responds to announcements.
• Groups of views define the applicaton’s layout.
• Each component is a different entry point of the system.
• An applicaton can have multple instances of the above.
Mateti/Android
5
Actvity
• An applicaton
typically consists of
several screens:
– Each screen is
implemented by one
actvity.
– Moving to the next
screen means
startng a new
actvity.
– An actvity may
return a result to
the previous actvity.
Mateti/Android
6
Actvity
• One of the actvites is marked as the main
one. Presented on launch.
• An actvity is usually a single screen:
– Implemented as a single class extending Actvity.
– Displays user interface controls (views).
– Reacts on user input/events.
Mateti/Android
7
Life cycle of an Actvity
Mateti/Android
8
Services
• A service does not have a (visual) user interface.
Runs in the background for an indefinite period tme.
– Examples: music player, network download, …
• Similar to daemons in Linux/Unix or Windows
services.
• Each service extends the Service base class.
• Communicate with the service through an interface
defined in AIDL (Android Interface Definiton
Language).
Mateti/Android
9
Services
• Interprocess communicaton (IPC).
• startService(); stopSelf() ; stopService()
• bindService(). Multple components can bind to the
service at once. When all of them unbind, the
service is destroyed.
• onStartCommand()
• onBind()
• onCreate()
• onDestroy()
Mateti/Android
10
Broadcast Receivers
• Broadcast announcements: Intents.
• All receivers extend the BroadcastReceiver
base class.
• Many broadcasts originate in the System.
– Ex: the tme zone has changed
– Ex: the battery is low
• Applicatons can also initate broadcasts.
Mateti/Android
11
Content Providers
• Sharing of content across applicatons
– E.g., address book, photo gallery
– the only way to share data between applicatons.
• APIs for query, delete, update and insert.
• Use ContentResolver methods to do the
above.
• Content is represented by URI and MIME type.
Mateti/Android
12
Content Providers
Application
Activity
Activity
Application
Activity
Activity
Application
Activity
Activity
Content
Content Resolver
Resolver
Service
Service
Content
Content Resolver
Resolver
Content
Content Provider
Provider
Content
Content Resolver
Resolver
Data
Data
SQLite
XML
XML
Mateti/Android
Remote
Store
13
Intent Examples
• ACTION_DIAL content://contacts/people/13
– Display the phone dialer with the person #13 filled in.
• ACTION_VIEW content://contacts/people/
– Display a list of people, which the user can browse through.
• startActvity(new Intent(Intent.VIEW_ACTION,
Uri.parse( "http://www.fhnw.ch"));
• startActvity(new Intent(Intent.VIEW_ACTION,
Uri.parse("geo:47.480843,8.211293"));
• startActvity(new Intent(Intent.EDIT_ACTION,
Uri.parse("content://contacts/people/1"));
• attributes: category, type, component, extras
Mateti/Android
14
Intent
• Intents are system messages:
– Actvity events ( launch app, press button)
– Hardware state changes (acceleraton change,
screen off, etc)
– Incoming data (Receiving call, SMS arrived)
• An intent object is an acton to be performed
on some data URI. Provides binding between
applicatons.
Mateti/Android
15
public class Intent
• startActvity to launch an actvity.
• broadcastIntent to send it to a BroadcastReceiver
• Communicate with a Service
– startService(Intent) or
– bindService(Intent, ServiceConnecton, int)
• Explicit Intents specify a component to be run.
– setComponent(ComponentName) or
– setClass(Context, Class))
• Implicit Intents match an intent against all of the
<intent-filter>s in the installed applicatons.
Mateti/Android
16
IntentReceivers
• Components that respond to Intents
• Way to respond to external notficaton or
alarms
• Apps can create and broadcast own Intents
Mateti/Android
17
Example App: Hello World!
developer.android.com/resources/tutorials
/hello-world.html
The Emulator
• QEMU-based ARM
emulator
• Displays the same
image as the device
• Limitatons:
– Camera
– GPS
Mateti/Android
19
Goal
• Create a very simple
applicaton
• Run it on the emulator
• Examine its structure
Mateti/Android
20
Building HelloAndroid
• Create a Project
– http://developer.android.com/training/basics/first
app/creatng-project.html
• Generates several files
– Next few slides
• Modify HelloAndroid.java as needed
Android-Develop-1
21
helloandroid Manifest
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3.
package="com.example.helloandroid"
4.
android:versionCode="1"
5.
android:versionName="1.0">
6.
<applicaton android:icon="@drawable/icon" android:label="@string/app_name">
7.
<actvity android:name=".HelloAndroid"
8.
android:label="@string/app_name">
9.
<intent-filter>
10.
<acton android:name="android.intent.action.MAIN" />
11.
<category android:name="android.intent.category.LAUNCHER" />
12.
</intent-filter>
13.
</actvity>
14. </applicaton>
15. </manifest>
Mateti/Android
22
HelloAndroid.java
package com.example.helloandroid;
import android.app.Actvity;
import android.os.Bundle;
public class HelloAndroid extends Actvity {
/** Called when the actvity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Set the layout of the
view as described in
Mateti/Android
the main.xml layout 23
HelloAndroid.java
Inherit
from the
Activity
Class
package com.example.helloandroid;
import android.app.Actvity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Actvity {
/** Called when the actvity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android – by hand");
setContentView(tv);
}
Set
}
Mateti/Android
the view “by
hand” – from the
program
24
Run it!
Mateti/Android
25
Android Applicaton Package: APK
• res/layout: declaraton layout
files
• res/drawable: intended for
drawing
• res/anim: bitmaps, animatons
for transitons
• res/values: externalized values
• An applicaton consists
of:
– strings, colors, styles, etc
Java
Code
Data Files
Resources
Files
• res/xml: general XML files
used at runtme
• res/raw: binary files (e.g.,
sound)
Mateti/Android
26
APK Content
All source code here
Java code for our activity
Generated Java code
Helps link resources to
Java code
Layout of the activity
All non-code
resources
Images
Strings used in the
program
Android Manifest
Mateti/Android
27
Android Applicaton Package: APK
• Using Java/Eclipse/ADT develop source files.
• An Android applicaton is bundled by the
“aapt” tool into an Android package (.apk)
– An .apk file is a zip file. Invoke unzip if you wish.
• “Installing” an Applicaton is a built-in op of
Android OS.
Mateti/Android
28
.apk Internals
1.
2.
3.
4.
5.
AndroidManifest.xml — deployment descriptor for applicatons.
IntentReceiver as advertsed by the IntentFilter tag.
*.java files implement Android actvity
Main.xml — visual elements, or resources, for use by actvites.
R.java —automatcally generated by Android Developer Tools and
"connects" the visual resources to the Java source code.
6. Components share a Linux process: by default, one process per
.apk file.
7. .apk files are isolated and communicate with each other via
Intents or AIDL.
Mateti/Android
29
Applicaton Resources
• anything relatng to the visual presentaton of
the applicaton
– images, animatons, menus, styles, colors, audio
files, …
• resource ID
• alternate resources for different device
configuratons
Mateti/Android
30
AndroidManifest.xml
• Declares all applicaton components:
– <actvity>
– <service>
– <provider> for content providers
– <receiver> for broadcast receivers
• The manifest can also:
– Identfy any user permissions the applicaton requires, such as
Internet access or read-access to the user's contacts.
– Declare hardware and software features used or required by
the applicaton
– API libraries the applicaton needs
Mateti/Android
31
/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientaton="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
Mateti/Android
Further redirection
to
/res/values/strings.xml
32
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloAndroid – by resources!</string>
<string name="app_name">Hello, Android</string>
</resources>
Mateti/Android
33
/gen/R.java
package com.example.helloandroid;
public final class R {
public statc final class attr {
}
public statc final class drawable {
public statc final int icon=0x7f020000;
}
public statc final class id {
public statc final int textview=0x7f050000;
}
public statc final class layout {
public statc final int main=0x7f030000;
}
public statc final class string {
public statc final int
app_name=0x7f040001;
public statc final int hello=0x7f040000;
}
}
Mateti/Android
• R.java is auto
generated on
build.
• Based on the
resource files
(including
layouts and
preferences)
• Do not edit.
34
Run it!
Mateti/Android
35
Debugging
• adb Android Debug Bridge
– moving and syncing files to the emulator
– running a Linux shell on the device or emulator
• Dalvik Debug Monitor Server
– DDMS is GUI + adb.
– capture screenshots
– gather thread and stack informaton
– spoof incoming calls and SMS messages
• Device or Android Virtual Device
• JDWP Java Debug Wire Protocol
– Java IDEs include a JDWP debugger
– command line debuggers such as jdb.
Mateti/Android
36
Introduce A Bug
package com.example.helloandroid;
import android.app.Actvity;
import android.os.Bundle;
public class HelloAndroid extends Actvity {
/** Called when the actvity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Object o = null;
o.toString();
setContentView(R.layout.main);
}
}
Mateti/Android
37
Run it!
Mateti/Android
38
Source Code for Android Examples
• Sources for many Android applicatons that
can be enhanced:
• http://code.google.com
• http://developer.android.com/resources/brow
ser.html?tag=sample
Mateti/Android
39