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

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

Dec 16, 2015

Download

Documents

Randell Warner
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

Android course

Introduction

dr Milan VidakovićChair of Informatics

Faculty of Technical SciencesUniversity of Novi Sad

Page 2: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University 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/

Page 3: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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)

Page 4: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

4/48

Architecture

Page 5: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 6: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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,...)

Page 7: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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);

Page 8: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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)

Page 9: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

9/48

Mobile Phone Devices

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

Page 10: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 11: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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.)

Page 12: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 13: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

13/48

Activity Life Cycle

Page 14: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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().

Page 15: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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().

Page 16: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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.

Page 17: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

17/48

Intents

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

• There are system intents• There are custom intents

Page 18: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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.)

Page 19: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

19/48

Intents

Page 20: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 21: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 22: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 23: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

23/48

Data storage

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

Page 24: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

24/48

APK

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

manifest– AndroidManifest.xml

Page 25: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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\

Page 26: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

26/48

Emulator

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

versions• Hardware accelerated (if possible)

Page 27: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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.

Page 28: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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.

Page 29: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 30: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

30/48

Project structure

Page 31: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

31/48

Project structure

Page 32: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

32/48

Create Activity

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

onStart(), etc.

Page 33: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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!

Page 34: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

34/48

Create GUI

Page 35: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

35/48

Create GUI

Page 36: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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; }}

Page 37: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 38: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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.

Page 39: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

39/48

Strings

Page 40: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

40/48

Android manifest

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

ContentProviders, etc.

Page 41: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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>

Page 42: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 43: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 44: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 45: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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)

Page 46: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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

Page 47: Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.

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/

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

48/48

Developers

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