200 likes | 365 Views
Fundamental of Applications. Chien -Chung Shen CIS, UD cshen@cis.udel.edu. App Fundamentals. Android applications are written in Java Android SDK tools compile the code —along with any data and resource files —into an Android package with . apk suffix
E N D
Fundamental of Applications Chien-Chung Shen CIS, UD cshen@cis.udel.edu
App Fundamentals • Android applications are written in Java • Android SDK tools compile the code—along with any data and resource files—into an Android packagewith .apksuffix • All the code in a single .apkfile is considered to be one application and is the file that Android-powered devices use to install the app • Once installed on an Android device, each Android application lives in its own security sandbox • sandbox is a security mechanism for separating running programs • e.g., virtual machine, which emulate a complete host computer
Ingredients of an App • Resources in res/ • Layout XML code in res/layout • Values of strings in res/values • Drawable images • Java source code in src/ • Manifest file AndroidManifest.xml • The R file in gen/ • Glue between Java and resources
App Fundamentals • Android operating system (OS) is a multi-user Linux system in which each application is a different user • By default, the system assigns each application a unique Linux user ID (on a given device) • User ID is used only by the system and is unknown to the application • Android sets permissions for all the files in an application so that only the application assigned with that user ID can access them • By default, every application runs in its own Linux process. Each process has its own virtual machine (VM), so an application's code runs in isolation from other applications • Android starts the process when any of the application's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other applications
App Fundamentals • There are ways for an application to share data with other applications and for an application to access system services • Possible for two apps to share the same Linux user ID, in which case they are able to access each other's files. To conserve system resources, apps with the same user ID can also arrange to run in the same Linux process and share the same VM (the apps must also be signed with the same certificate) • An application can request permission to access device data such as the user's contacts, SMS messages, the mountable storage (SD card), camera, Bluetooth, and more. All application permissions must be granted by the system at install time
Application Components • The essential building blocks of an Android application • An Android app = { components } • 4 types of application components • Activities • Services • Content providers • Broadcast receivers
Activities • An activity represents a single screen with a user interface - e.g., an email app • Might have one activity that shows a list of new emails, another activityto compose an email, and another activity for reading emails • Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others
Activities • One unique aspect of Android is that one app can start another app’s activity – e.g., app A wants to share (email) a photo • There is already one “camera” app that takes photos • App A doesn't need to link to the code from the camera app • Instead, app A simply starts the activity in the camera app to captures a photo. When complete, the photo is returned to app A • To user, it seems as if the camera is actually a part of app A • When Android starts an activity, it starts the process for that application (if it's not already running) and instantiates the classes needed for the activity • For example, app A starts the activity in the camera app that captures a photo, that activity runs in the process that belongs to the camera app, not in app A's process • Therefore, unlike applications on most other systems, Android applications don't have a single entry point (there's no main()function)
Activities • An Activity is an application component that provides a screen with which users can interact in order to do something • e.g., dial a phone, take a photo, send an email, or view a map • Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.
Activities • An app usually consists of multiple activities that are loosely bound to each other • Typically, one activity in an app is specified as the "main" activity, which is presented to the user when launching the application for the first time • Each activity can then start another activity in order to perform different actions • Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack("back stack”) • When a new activity starts, it is pushed onto the back stack and takes user focus • The back stack abides to the "last in, first out" stack mechanism, so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes
Activities • When an activity is stopped because a new activity starts, it is notified of this change in state through the activity's lifecycle callback methods • There are several callback methods that an activity might receive, due to a change in its state • the system is creating it, stopping it, resuming it, or destroying it • Each callback provides the activity the opportunity to perform specific work that's appropriate to that state change • when stopped, an activity should release any large objects, such as network or database connections. When the activity resumes, it can reacquire the necessary resources and resume actions that were interrupted • State transitions are all part of the activity lifecycle.
Java Inheritence • Different kinds of objects often have a certain amount in common with each other • Mountain bikes, road bikes, and tandem bikes, etc., all share the characteristics of bicycles • Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; • Object-oriented programming allows classes to inheritcommonly used state and behavior from other classes • Bicycle now becomes the superclassof MountainBike, RoadBike, and TandemBike.
Java Inheritence class MountainBikeextends Bicycle { // new fields and methods defining // a mountain bike would go here }
Create an Activity in Java • To create an activity, create a subclass of Activity • In the subclass, implement callback methods that the system calls when the activity transitions between various states of its lifecycle • e.g., when the activity is being created, stopped, resumed, or destroyed • Two most important callback methods: onCreate() and onPause()
OverrideonCreate() • You must implement this method • The system calls this when creating the activity. • Within implementation, you should initialize the essential components of your activity. Most importantly, call setContentView() to define the layout for the activity's user interface
Java Annotation • @Override– informs compiler that the element is meant to override an element declared in a superclass @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
Declaring Activity in Manifest • Must declare activity in manifest file for it to be accessible to the system • Attribute android:name is the only required attribute—specify the class name of the activity • An <activity> element can also specify intent filters element to declare how other application components may activate it
Intent and Intent Filter • Because Android runs each application in a separate process with file permissions that restrict access to other apps, one app cannot directly activate an activity from another app • To activate an activity in another app, one app must deliver a message to the system that specifies the intentto start a particular activity. The system then activates the activity for the app • Intent filter describes a set of intents that the component is willing to receive. which filters in intents of a desired type, while filtering out unwanted intents
(Default) Intent Filter <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /></intent-filter> • action specifies that this is the “main” entry point to the app • category specifies that this activity should be listed in the systems’ application launcher(an app that launches other apps) • Only one activity should have the MAINaction and LAUNCHER category