Android-Development-1

advertisement
App Development for Android
Prabhaker Mateti
Development Tools
• (Android) Java
– Java is the same. But, not all libs are included.
– Unused: Swing, AWT, SWT, lcdui
•
•
•
•
•
Eclipse www.eclipse.org/
ADT Plugin for Eclipse developer.android.com/
Android SDK developer.android.com/
Android Device Emulator
Development Platforms: Linux, Mac OSX, or
Windows
Mateti/Android
2
(Other) Languages and IDEs
•
•
•
•
•
IntelliJ Idea
Android Studio
Corona for Android
Android Native Development Kit (NDK)
Scala
Mateti/Android
3
Application Runtime
• Each application is a different “user”.
• Each application gets a unique Linux user ID.
The system sets permissions for all the files in
an application so that only the user ID
assigned to that application can access them.
• Each process has its own Dalvik/Art VM.
• Every application runs in its own Linux
process. A process can have multiple threads.
Mateti/Android
4
Application Framework
• Views lists, grids, text boxes, buttons,
embeddable web browser
• Content Providers to access data from other
applications, or to share their own data
• Resource Manager access non-code resources;
e.g., strings, graphics, and layout files
• Notification Manager alerts in the status bar
• Activity Manager lifecycle of applications and
navigation backstack
Mateti/Android
5
Application Components
• Activity: (GUI) functions that the application performs.
• Service: no UI
– run in the background; Long-running; for remote processes
– no user interface.
• Content Providers facilitate data transmission among
different applications.
• Broadcast Receiver: respond to announcements.
• Groups of views define the application’s layout.
• Each component is a different entry point of the system.
• An application can have multiple instances of the above.
Mateti/Android
6
Activity
• An application
typically consists
of several screens:
– Each screen is
implemented by
one activity.
– Moving to the
next screen
means starting a
new activity.
– An activity may
return a result to
the previous
activity.
Mateti/Android
7
Activity
• One of the activities is marked as the main
one. Presented on launch.
• An activity is usually a single screen:
– Implemented as a single class extending Activity.
– Displays user interface controls (views).
– Reacts on user input/events.
Mateti/Android
8
Life cycle of an Activity
Mateti/Android
9
Services
• A service does not have a (visual) user interface.
Runs in the background for an indefinite period
time.
– 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
Definition Language).
Mateti/Android
10
Services
• Interprocess communication (IPC).
• startService(); stopSelf() ; stopService()
• bindService(). Multiple components can bind to
the service at once. When all of them unbind, the
service is destroyed.
• onStartCommand()
• onBind()
• onCreate()
• onDestroy()
Mateti/Android
11
Broadcast Receivers
• Broadcast announcements: Intents.
• All receivers extend the BroadcastReceiver
base class.
• Many broadcasts originate in the System.
– Ex: the time zone has changed
– Ex: the battery is low
• Applications can also initiate broadcasts.
Mateti/Android
12
Content Providers
• Enables sharing of content across applications
– E.g., address book, photo gallery
– the only way to share data between applications.
• APIs for query, delete, update and insert.
• Use ContentResolver methods to do the
above.
• Content is represented by URI and MIME type.
Mateti/Android
13
Content Providers
Application
Activity
Activity
Application
Application
Activity
Content Resolver
Service
Content Resolver
Content Provider
Content Resolver
Data
SQLite
XML
Mateti/Android
Remote
Store
14
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.
• startActivity(new Intent(Intent.VIEW_ACTION,
Uri.parse( "http://www.fhnw.ch"));
• startActivity(new Intent(Intent.VIEW_ACTION,
Uri.parse("geo:47.480843,8.211293"));
• startActivity(new Intent(Intent.EDIT_ACTION,
Uri.parse("content://contacts/people/1"));
• attributes: category, type, component, extras
Mateti/Android
15
Intent
• Intents are system messages:
– Activity events ( launch app, press button)
– Hardware state changes (acceleration change,
screen off, etc)
– Incoming data (Receiving call, SMS arrived)
• An intent object is an action to be performed
on some data URI. Provides binding between
applications.
Mateti/Android
16
public class Intent
• startActivity to launch an activity.
• broadcastIntent to send it to a BroadcastReceiver
• Communicate with a Service
– startService(Intent) or
– bindService(Intent, ServiceConnection, 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 applications.
Mateti/Android
17
IntentReceivers
• Components that respond to Intents
• Way to respond to external notification or
alarms
• Apps can create and broadcast own Intents
Mateti/Android
18
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
• Limitations:
– Camera
– GPS
Mateti/Android
20
Goal
• Create a very simple
application
• Run it on the emulator
• Examine its structure
Mateti/Android
21
Building HelloAndroid
• Create a Project
– http://developer.android.com/training/basics/first
app/creating-project.html
• Generates several files
– Next few slides
• Modify HelloAndroid.java as needed
Android-Develop-1
22
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.
<application android:icon="@drawable/icon" android:label="@string/app_name">
7.
<activity android:name=".HelloAndroid"
8.
android:label="@string/app_name">
9.
<intent-filter>
10.
<action android:name="android.intent.action.MAIN" />
11.
<category android:name="android.intent.category.LAUNCHER" />
12.
</intent-filter>
13.
</activity>
14. </application>
15. </manifest>
Mateti/Android
23
HelloAndroid.java
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Set the layout of the
Mateti/Android
24
view as described in
the main.xml layout
HelloAndroid.java
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
Inherit
from the
Activity
Class
public class HelloAndroid extends Activity {
/** Called when the activity 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 the view “by
Mateti/Android
25
hand” – from the
program
Run it!
Mateti/Android
26
Android Application Package: APK
•
res/layout: declaration
layout files
• res/drawable: intended for
drawing
• res/anim: bitmaps,
animations for transitions
• res/values: externalized
values
• An application consists of:
– strings, colors, styles, etc
Java
Code
Data Files
Resources
Files
•
res/xml: general XML files
used at runtime
• res/raw: binary files (e.g.,
sound)
Mateti/Android
27
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
28
Android Application Package: APK
• Using Java/Eclipse/ADT develop source files.
• An Android application 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 Application is a built-in op of
Android OS.
Mateti/Android
29
.apk Internals
1. AndroidManifest.xml — deployment descriptor for
applications.
2. IntentReceiver as advertised by the IntentFilter tag.
3. *.java files implement Android activity
4. Main.xml — visual elements, or resources, for use by
activities.
5. R.java —automatically 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
30
Application Resources
• anything relating to the visual presentation of
the application
– images, animations, menus, styles, colors, audio
files, …
• resource ID
• alternate resources for different device
configurations
Mateti/Android
31
AndroidManifest.xml
• Declares all application components:
–
–
–
–
<activity>
<service>
<provider> for content providers
<receiver> for broadcast receivers
• The manifest can also:
– Identify any user permissions the application requires,
such as Internet access or read-access to the user's
contacts.
– Declare hardware and software features used or required
by the application
– API libraries the application needs
Mateti/Android
32
/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="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>
Further redirection to
/res/values/strings.xml
Mateti/Android
33
/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
34
/gen/R.java
package com.example.helloandroid;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int textview=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int
app_name=0x7f040001;
public static 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.
35
Run it!
Mateti/Android
36
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 information
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
37
Introduce A Bug
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Object o = null;
o.toString();
setContentView(R.layout.main);
}
}
Mateti/Android
38
Run it!
Mateti/Android
39
Source Code for Android Examples
• Sources for many Android applications that
can be enhanced:
• http://code.google.com
• http://developer.android.com/resources/brow
ser.html?tag=sample
Mateti/Android
40
Download
Study collections