480 likes | 617 Views
Android course. Introduction. dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad. Android. Initially: Mobile phone platform Now: embedded devices platform mobile phones tablets multimedia devices AndroidTV/GoogleTV Set Top Boxes (STB) TV
E N D
Android course Introduction dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad
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/
Android • Purchased and controlled by the Google • Open platform • Operating System is Open Source • SDK is free http://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)
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
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,...)
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);
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 CreamSandwitch (API v14 - 15) • 4.1.x – Jelly Bean (API v16 - 17), • 5.0 – Key Lime Pie (API v18)
Mobile Phone Devices • A large number of devices • radio (phone), • camera, • compass, • GPS • Bluetooth • Wifi • movement sensors (Gyro)
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
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.)
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
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().
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().
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.
Intents • Intents are messages that are exchanged between activities, services, etc. • There are system intents • There are custom intents
Intents • System matches activities (Activity) with intents (Intent) • Activities declare which intents can activate them • defined in the AndroidManifest.xmlfile • 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.)
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
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
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
Data storage • Several options: • Preferences • files in the local file system • SQLite database • ContentProvider
APK • Android application is packed in APK file • ZIPfile • Contains executable code, resources and manifest • AndroidManifest.xml
Android SDK • Free Development Kit • Contains: • tools and libraries • adb (Android Debug Bridge) • logcat – interni log Androida • Emulator • Samples in: android-sdk\samples\
Emulator • Emulates both generic and real devices • Emulates different OS versions and API versions • Hardware accelerated (if possible)
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.
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.
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
Create Activity • Activity extends Activity class • Override life cycle callbacs: onCreate(), onStart(), etc.
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!
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; } }
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
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.
Android manifest • AndroidManifest.xml file • root Java package name • minimal API level • permissions • list of activities, services, BroadcastReceivers, ContentProviders, etc.
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>
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
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
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
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)
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
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/
Developers • Recommended sites for the developers: http://stackoverflow.com http://www.xda-developers.com/