android course introduction dr milan vidaković chair of informatics faculty of technical sciences...

48
Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad

Upload: randell-warner

Post on 16-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Android course

Introduction

dr Milan VidakovićChair of Informatics

Faculty of Technical SciencesUniversity of Novi Sad

2/48

Android

• Initially: Mobile phone platform• Now: embedded devices platform– mobile phones– tablets– multimedia devices– AndroidTV/GoogleTV– Set Top Boxes (STB)– TV

• PC devices version:http://www.android-x86.org/

3/48

Android

• Purchased and controlled by the Google – Open platform– Operating System is Open Source– SDK is freehttp://developer.android.com/index.html

• Based on the Linux kernel– kernel and drivers in the kernel space– native libraries in the user space– Java libraries link Java applications to the native libraries– applications are executed in the Java VM (Dalvik VM)

4/48

Architecture

5/48

Linux kernel• Standar Linux kernel with additional drivers– Binder – IPC (Inter Process Communication) – Ashmem – shared memory– Power Management– Logger– Alarm– Low Memory Killer– Kernel Debugger

6/48

Native libraries

• Bionic Libc• Utility libraries (Webkit, Media Framework,

SQLite• Native servers (Surface Manager –

SurfaceFlinger, Audio Manager – AudioFlinger• Hardware Abstraction Libraries (Graphics,

Camera, Bluetooth, GPS, Radio, WiFi,...)

7/48

Application Framework

• Java classes and interfaces– support Android (Java) applications– link Android (Java) applications to the native layer

LocationManager lm = (LocationManager)Context.getSystemService(Context.LOCATION_SERVICE);

8/48

Android OS versions

• Versions:– 1.5 – Cupcake (API v3), – 1.6 – Donut (API v4), – 2.0-2.1 – Eclair (API v7), – 2.2 – Froyo (API v8),– 2.3.x – Gingerbread (API v9 - 10), – 3.x.x – Honeycomb (API v11 - 13), – 4.0.x – Ice Cream Sandwitch (API v14 - 15)– 4.1.x – Jelly Bean (API v16 - 17),– 5.0 – Key Lime Pie (API v18)

9/48

Mobile Phone Devices

• A large number of devices– radio (phone),– camera,– compass,– GPS– Bluetooth– Wifi– movement sensors (Gyro)

10/48

Special GUI

• Main idea is similar to the desktop GUI• Main input device: touch screen– long touch – context menu– move – scroll– fling – animated scroll– multi touch – zoom

11/48

Android applications

• Written in the Java language• Complex processing is delegated to the native

layer via JNI (Java Native Invocation)• One application over the screen– application can have multiple windows (called

activities), but each one is in the full screen mode– applications can use other application’s activities

(camera, contacts, etc.)

12/48

Types of applications

• Activity – GUI window (an application must have at least one activity)

• Service – background processing• ContentProvider – for sharing data

between applications• BroadcastReceiver – receives system-

wide messages

13/48

Activity Life Cycle

14/48

Activity Life Cycle callbacks• onCreate(): invoked when the activity is created. Used for the initialisation.

– System can shut down the application from here: No. – Next: onStart().

• onStart(): invoked when the activity becomes visible. – System can shut down the application from here: No. – Next: onResume().

• onResume(): invoked when the activity is ready for the interaction with the userSystem can shut down the application from here: No. – Next: onPause().

• onRestart(): invoked when an activity has been stopped, and then started again (prior to the restart). – System can shut down the application from here: No. – Next: onStart().

15/48

Activity Life Cycle callbacks

• onPause(): invoked before the system places another activity over this one, or an user pressed the Home key, or screen saver has been activated. Used to save data. If the application resumes later, (after screen saver), onResume() is invoked; if the activity is no longer visible, onStop() is invoked. – System can shut down the application from here: Da. – Next: onResume() or onStop().

• onStop(): invoked when an activity is no longer visible (other activity is now on the screen). If an activity will no longer be used (Back key is pressed, onDestroy() is invoked; if an activity is coming back (after some other activity), onRestart() is invoked. – System can shut down the application from here: Yes. – Next: onRestart() ili onDestroy().

16/48

Activity Life Cycle callbacks

• onDestroy(): invoked before an activity is destroyed. Invoked if an application is closed, or the system decided to close the application (due to the low resources situation). Cause can be found by invoking isFinishing() method. isFinishing() can be invoked within onPause() method too. – System can shut down the application from here: Yes. – Next: None.

17/48

Intents

• Intents are messages that are exchanged between activities, services, etc.

• There are system intents• There are custom intents

18/48

Intents

• System matches activities (Activity) with intents (Intent)

• Activities declare which intents can activate them– defined in the AndroidManifest.xml file

• One activity can invoke opther activity by sending Intent– the other activity can be custom activity (created by

the programmer), or system activity (contacts, camera, etc.)

19/48

Intents

20/48

Services

• Used for backgroud jobs (music playback, download files, etc.)

• It is possible to link application and background service via IPC (InterProcess Communication) implemented as Binder service– list of methods that can be invoked is given in AIDL

(Android Interface Definition Language) files

21/48

ContentProviders

• Used for data sharing between applications• Provide uniform interface for data fetching,

creation, removal and modification• Mainly used to share database data

between programs– database data is private data and can be

accessed by its program– ContentProviders share that private data

22/48

BroadcastReceivers

• Components which react on broadcasted system messages (battery low, SMS received, etc.)

• Broadcast messages are implemented as Intents

• BroadcastReceivers usually don’t have their own GUI, but can access status bar notification area

23/48

Data storage

• Several options: – Preferences– files in the local file system– SQLite database– ContentProvider

24/48

APK

• Android application is packed in APK file• ZIP file• Contains executable code, resources and

manifest– AndroidManifest.xml

25/48

Android SDK

• Free Development Kit• Contains:– tools and libraries• adb (Android Debug Bridge)• logcat – interni log Androida

– Emulator• Samples in:

android-sdk\samples\

26/48

Emulator

• Emulates both generic and real devices• Emulates different OS versions and API

versions• Hardware accelerated (if possible)

27/48

adb

• Android Debug Bridge• Client-server system• Client is on the host (developer) machine• Server is on the Android device (or emulator)• Provides:

– installation of an application to the Android device,• adb install apk_file

– copy files from/to Android device,• adb push my_file /sdcard/• adb pull /sdcard/file .

– watch system Log,• adb logcat

– work with shell on the Android device, • adb shell

– etc.

28/48

Eclipse plugin

• Integrates Android SDK tools into Eclipse– creates Android projects– provides debugging– visual editor for GUI– displays Log – logcat– takes screen snapshots– dumps memory usage– etc.

29/48

Android development cycle

• Create activity– override onCreate() method for initialisation

• Create GUI• Declare activity in the AndroidManifest.xml

file• Install on the device• Start/Debug

HelloAndroid

30/48

Project structure

31/48

Project structure

32/48

Create Activity

• Activity extends Activity class • Override life cycle callbacs: onCreate(),

onStart(), etc.

33/48

Create GUI

• Two ways:– program– declarative (.xml file)

• Declarative is recommended– layout folder, layout.xml

• Eclipse plugin generates the R class which holds ids of all resources– whole API works with ids, not with classes and

resources!

34/48

Create GUI

35/48

Create GUI

36/48

R class/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */

package com.blast;

public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } 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; }}

37/48

Resources

• Resources can be:– animation –anim folder– lists of used colors –color folder– multimedia (pictures, animations, etc.) –drawable

folder– the rest –raw folder• android.resource://com.blast/raw/famous

– xml files which define GUI –layout folder

38/48

Resources

• Resources can be:– menu description –menu folder– strings –values folder, strings.xml file• i18n• localisation• there can be several xml files with strings

– tablestrings.xml, connectionstrings.xml, etc.

39/48

Strings

40/48

Android manifest

• AndroidManifest.xml file– root Java package name – minimal API level– permissions– list of activities, services, BroadcastReceivers,

ContentProviders, etc.

41/48

Android manifest<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blast" android:versionCode="1" android:versionName="1.0" >

<uses-sdk android:minSdkVersion="9" />

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".LayoutExamplesActivity" 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>

42/48

Installation

• Application is placed in the APK file– zip file with defined structure

• Each application is digitally signed– no need for the commercial certificate

• Eclipse plugin packs and signs the application– can be done manually

43/48

Installation

• Real device is connected to the developer machine via USB cable, or network

• If connected via USB cable:– device needs to have USB debug turned on

• If connected via network:– needs to be connected using the following adb

command:adb connect x.x.x.x:port

• How to check if the device is connected:adb devices

44/48

Installation

• From the Ecplise – just run the project• Manually:

– adb install Application.apk– adb uninstall com.my.package

• From the Google Play– APK file is downloaded from the Google server and installed on the

device– “safe”– users comments, number of downloads, ranking, etc.

• From the SD card– must be enabled in the system settings due to the security reasons

45/48

Installation

• During installation, the application is copied to the /system/app folder– users do not have priviledges to modifay this folder’s

content• Application saves its private data into the

/data/data/app folder• The /data folder is the only folder with the full

access for the user• The other one folder with the full access for users is

the SD card (if exists)

46/48

Installation

• Each installed application has user priviledges– changing system files and folders is not permitted

• Only system processes have the Superuser priviledge (root priviledge)

• Rooting the device– subsitute system su application with the hacked one– install the superuser.apk application– consequence:

• full access to the device files and applications• rooting error would make the device become the bricked device

47/48

Custom firmware

• Since the Android OS is open source, it is possible to build and install custom OS into the device– only device drivers are needed in addition to the

Android source

http://www.cyanogenmod.com/

48/48

Developers

• Recommended sites for the developers:http://stackoverflow.comhttp://www.xda-developers.com/