46
Application Fundamentals

Application Fundamentals

  • Upload
    keira

  • View
    47

  • Download
    0

Embed Size (px)

DESCRIPTION

Application Fundamentals. Android package. An archive file marked by an.apk suffix.  Java code + any data +resource files is bundled by the aapt  tool  into an  Android Asset Packaging Tool ( aapt ) –tools/aapt.exe IDE plugins utilize this tool  - PowerPoint PPT Presentation

Citation preview

Page 1: Application Fundamentals

Application Fundamentals

Page 2: Application Fundamentals

An archive file marked by an.apk suffix.  Java code + any data +resource files is bundled by the aapt tool into an 

◦ Android Asset Packaging Tool (aapt) –tools/aapt.exe

◦ IDE plugins utilize this tool  Is the vehicle for distributing the application

◦ can download/install on mobile devices All the code in a single .apk file is

considered to be one application.

Android package

Page 3: Application Fundamentals

By default, every application runs in its own Linux process. ◦ Android starts the process when any of the application's code

needs to be executed, and shuts down the process when it's no longer needed and system resources are required by other applications.

Each process has its own virtual machine (VM), ◦ so application code runs in isolation from the code of all other

applications. By default, each application is assigned a unique Linux

user ID. ◦ Permissions are set so that the application's files are visible

only to that user and only to the application itself — although there are ways to export them to other applications as well.

Android application

Page 4: Application Fundamentals

Activities Services Broadcast receivers Content providers

Application Components

Page 5: Application Fundamentals

Whenever there's a request that should be handled by a particular component, Android makes sure that the application process of the component is running, starting it if necessary, and that an appropriate instance of the component is available, creating the instance if necessary.

Application Components

Page 6: Application Fundamentals

Add Activities, Services , Broadcast receivers , Content providers here

Page 7: Application Fundamentals

An activity presents a visual user interface◦ for one focused endeavor the user can undertake. ◦ E.g.

present a list of menu items users can choose from display photographs along with their captions.

One application has multiple activities◦ A text messaging application might have

one activity that shows a list of contacts to send messages a second activity to write the message to the chosen contact, other activities to review old messages or change settings

◦ Activities together to form a cohesive user interface◦ Each activity is independent of the others. ◦ Each activity is implemented as a subclass of the Activity

 base class.

Activities

Page 8: Application Fundamentals

Each activity is given a default window to draw in. ◦ Activity window

fills the screen (default) smaller than the screen float on top of other windows.

An activity can make use of additional windows ◦ E.g., a pop-up dialog

calls for a user response in the midst of the activity, ◦ E.g., vital information window

when they select a particular item on-screen.

Activities

Page 9: Application Fundamentals

The visual content of the window is provided by a hierarchy of views.

Views are where the activity's interaction with the user takes place. ◦ a view might display a small image and initiate an action when the

user taps that image. ◦ buttons, text fields, scroll bars, menu items, check boxes, and more.

Views are objects derived from the base View class. Each view controls a particular rectangular space within the

window. Parent views contain and organize the layout of their children. Leaf views (those at the bottom of the hierarchy) draw in the

rectangles they control and respond to user actions directed at that space.

Activities

Page 10: Application Fundamentals

A view hierarchy is placed within an activity's window ◦ by the Activity.setContentView() method.

The content view is the View object at the root of the hierarchy. ◦ See the separate User Interface document for

more information on views and the hierarchy.

Activities

Page 11: Application Fundamentals

A service doesn't have a visual user interface◦ runs in the background for an indefinite period of time. ◦ e.g.,

play background music as the user attends to other matters,

fetch data over the network calculate something and provide the result to activities

that need it. ◦ extends the Service base class.

Services often spawn another thread for time-consuming tasks ◦ music playback

Services

Page 12: Application Fundamentals

Do nothing but receive and react to broadcast announcements.

Many broadcasts originate in system code ◦ announcements that the time zone has changed,◦ the battery is low, ◦ a picture has been taken, ◦ the user changed a language preference.

Applications can also initiate broadcasts ◦ let other applications know that some data has

been downloaded to the device and is available for them to use.

Broadcast receivers

Page 13: Application Fundamentals

An application can have any number of broadcast receivers ◦ to respond to any announcements it considers important.

Broadcast receivers ◦ extend the BroadcastReceiver base class◦ do not display a user interface. ◦ may start an activity in response to the information they receive ◦ may use the NotificationManager to alert the user

Notifications ◦ can get the user's attention in various ways◦ flashing the backlight, vibrating the device, playing a sound, and

so on◦ typically place a persistent icon in the status bar, which users

can open to get the message.

Broadcast receivers

Page 14: Application Fundamentals

Makes a specific set of the application's data available to other applications. The data ◦ can be stored in the file system, ◦ in an SQLite database, ◦ in any other manner that makes sense.

Extends the ContentProvider base class to implement a standard set of methods ◦ that enable other applications to retrieve and store data of the type

it controls. Use a ContentResolver object and call its methods instead.

◦ i.e., applications do not call these methods directly. A ContentResolver

◦ can talk to any content provider; ◦ cooperates with the provider to manage any interprocess

communication that's involved.

Content providers

Page 15: Application Fundamentals

Whenever there's a request that should be handled by a particular component, ◦ Android makes sure that the application process of the

component is running, starting it if necessary, and that an appropriate instance of the component is available, creating the instance if necessary.

Question◦ Who will activate components?◦ Content providers are activated

when they're targeted by a request from a ContentResolver. ◦ Activities, services, and broadcast receivers

are activated by asynchronous messages called intents. 

Application Components

Page 16: Application Fundamentals

Activities

Services

Broadcast receivers

Content providers

Intent

ContentResolver

Application

Page 17: Application Fundamentals

An Intent in Android describes what you want to do. ◦ “I want to look up a contact record,” or ◦ “Please launch this website,” or ◦ “Show the Order Confirmation Screen.”

Intents are important because they facilitate navigation and represent the most important aspect of Android coding.

Activating components:Intents

Page 18: Application Fundamentals

Is an Intent object that holds the content of the message.

For activities and services ◦ it names the action being requested and specifies the

URI of the data to act on, among other things. ◦ for example, it might convey a request for an activity

to present an image to the user or let the user edit some text.

For broadcast receivers ◦ the Intent object names the action being announced. ◦ for example, it might announce to interested parties

that the camera button has been pressed.

Activating components: Intents

Page 19: Application Fundamentals

It is basically a passive data structure ◦ holding an abstract description of an action to be

performed. The primary pieces of information in an

intent:◦ action -- The general action to be performed,

such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.

◦ data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.

Intents

Page 20: Application Fundamentals
Page 21: Application Fundamentals

ACTION_VIEW content://contacts/people/1 -- Display information about the person whose identifier is "1".

ACTION_DIAL content://contacts/people/1 -- Display the phone dialer with the person filled in.

ACTION_VIEW tel:123 -- Display the phone dialer with the given number filled in. Note how the VIEW action does what what is considered the most reasonable thing for a particular URI.

ACTION_DIAL tel:123 -- Display the phone dialer with the given number filled in.

ACTION_EDIT content://contacts/people/1 -- Edit information about the person whose identifier is "1".

ACTION_VIEW content://contacts/people/ -- Display a list of people, which the user can browse through. This example is a typical top-level entry into the Contacts application, showing you the list of people. Selecting a particular person to view would result in a new intent { ACTION_VIEW content://contacts/N } being used to start an activity to display that person.

Intents: examples of action/data pairs

Page 22: Application Fundamentals

Following fragments calls an Intent whose job is to invoke a built-in task (ACTION_VIEW) and explore the Contacts available in the phone.

Intents Example

Intent myIntent= new Intent( Intent.ACTION_VIEW, Uri.parse("content://contacts/people"));startActivity(myIntent);

Page 23: Application Fundamentals

package com.example.helloandroid;

import android.app.Activity;import android.os.Bundle;import android.widget.TextView;

public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

Intent myIntent= new Intent( Intent.ACTION_VIEW,Uri.parse("content://contacts/people")); startActivity(myIntent); }

}

Page 24: Application Fundamentals

Following Intent uses built-in task (ACTION_VIEW) to explore a web page

Intents Example

Intent myIntent= new Intent( Intent.ACTION_VIEW, Uri.parse(" "http://www.google.com") );startActivity(myIntent);

Page 25: Application Fundamentals

Intents Example Following Intent uses built-in task

(ACTION_VIEW) to make a phone call

Page 26: Application Fundamentals

Need to pass an Intent object to ◦ Context.startActivity() or◦ Activity.startActivityForResult().

The responding activity ◦ looks at the initial intent that caused it to be launched by calling

its getIntent() method◦ Android calls the activity's onNewIntent() method to pass it any

subsequent intents. Activity.startActivityForResult().

◦ One activity often starts the next one. ◦ expects a result back from the activity it's starting◦ e.g., if it starts an activity that lets the user pick a photo, it might

expect to be returned the chosen photo. The result is returned in an Intent object that's passed to the calling activity's onActivityResult() method.

Intents to Activating Activity

Page 27: Application Fundamentals

Explicit intents ◦ designate the target component by its name (the 

component name field, mentioned earlier, has a value set). ◦ Since component names would generally not be known to

developers of other applications, explicit intents are typically used for application-internal messages e.g., an activity starting a subordinate service e.g., launching a sister activity.

Implicit intents ◦ do not name a target (the field for the component name is blank). ◦ often used to activate components in other applications◦ in the absence of a designated target, the Android system must find

the best component (or components) to handle the intent e.g., a single activity or service to perform the requested action or e.g., the set of broadcast receivers to respond to the broadcast

announcement..

Intent

Page 28: Application Fundamentals

How Android system find the best component (or components) to handle the intent ◦ comparing the contents of the Intent object to intent filters,

structures associated with components that can potentially receive intents. Intent Filters

◦ advertise the capabilities of a component and delimit the intents it can handle.

◦ open the component to the possibility of receiving implicit intents of the advertised type. an explicit intent is always delivered to its target, no matter what it

contains; the filter is not consulted. an implicit intent is delivered to a component only if it can pass through

one of the component's filters. if a component does not have any intent filters, it can receive only explicit

intents. A component with filters can receive both explicit and implicit intents.

Intent filters

Page 29: Application Fundamentals

For implicit intent Only three aspects of an Intent object are

consulted when the object is tested against an intent filter:◦ action ◦ data (both URI and data type) ◦ category

Intent filters

Page 30: Application Fundamentals

If a component does not have any intent filters, it can receive only explicit intents.

A component with filters can receive both explicit and implicit intents

The intent object is tested against an intent filter using following items:◦ action ◦ data (both URI and data type)◦ Category

Multiple intent filters◦ Only need pass one of them

Intent Test

Page 31: Application Fundamentals

To pass this test, ◦ the action specified in the Intent object must match one of the actions listed

in the filter. If the object or the filter does not specify an action, the results are

as follows:◦ If the filter fails to list any actions-No intents can get through the filter.

Default= Take nothing◦ On the other hand, an Intent object that doesn't specify an action

automatically passes the test — as long as the filter contains at least one action. Default =* (except empty)

Intent filters-Action test

Page 32: Application Fundamentals

For an intent to pass the category test, ◦ every category in the Intent object must match a category in the filter.

the filter can list additional categories, but it cannot omit any that are in the intent.

◦ an Intent object with no categories should always pass this test, regardless of what's in the filter. one exception - Android treats all implicit intents passed to startActivity()

 as if they contained at least one category: "android.intent.category.DEFAULT“

In other words, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters

Intent filters-Category test

Full string values, don’t use constants, e,g.,CATEGORY_BROWSABLE

Page 33: Application Fundamentals

Each <data> element can specify ◦ a URI

scheme://host:port/path content://com.example.project:200/folder/subfolder/

etc◦ data type (MIME media type).

Intent filters-Data test

Page 34: Application Fundamentals

• the main entry point into the Note Pad application.• The standard MAIN action is an entry point that does not require any other information in the Intent (no data specification, for example)•the LAUNCHER category says that this entry point should be listed in the application launcher.

This filter declares the things that the activity can do on a directory of notes. •view the directory •edit the directory •pick a particular note from the directory

DEFAULT category is required for all filters  • Context.startActivity() and Activity.startActivityForResult() methods treat all intents as if they contained the DEFAULT category•two exceptions:

• Intents that explicitly name the target activity

• Intents consisting of the MAIN action and LAUNCHER category

Describes the ability return to the caller a note selected by the user without needing to know where it came from. vnd.android.cursor.item/vnd.google.note: URI from which a Cursor of exactly one (vnd.android.cursor.item) item can be retrieved which contains our note pad data (vnd.google.note). The GET_CONTENT return to its caller a piece of data selected by the user.

Page 35: Application Fundamentals

intents will resolved { action=android.app.action.MAIN } matches all of the activities that

can be used as top-level entry points into an application. { action=android.app.action.MAIN,

category=android.app.category.LAUNCHER } is the actual intent used by the Launcher to populate its top-level list.

{ action=android.intent.action.VIEW data=content://com.google.provider.NotePad/notes } displays a list of all the notes under "content://com.google.provider.NotePad/notes", which the user can browse through and see the details on.

{ action=android.app.action.PICK data=content://com.google.provider.NotePad/notes } provides a list of the notes under "content://com.google.provider.NotePad/notes", from which the user can pick a note whose data URL is returned back to the caller.

{ action=android.app.action.GET_CONTENT type=vnd.android.cursor.item/vnd.google.note } is similar to the pick action, but allows the caller to specify the kind of data they want back so that the system can find the appropriate activity to pick something of that data type.

Page 36: Application Fundamentals

How declare the components (e.g., activates) you have developed?◦ Before Android can start an application

component, it must learn that the component exists.

Applications declare their components in a manifest file ◦ Those file are bundled into the Android package,

the .apk file that also holds the application's code, files, and resources.

Activating Activity

Page 37: Application Fundamentals

 name : names the Activity subclass that implements the activity  icon, label: point to resource files containing an icon and label that can be displayed to users to represent the activity.

 <service> elements for services, <receiver> elements for broadcast receivers,  <provider> elements for content providers. Activities, services, and content providers that are not declared in the manifest are not visible to the system and are consequently never run. However, broadcast receivers can either be declared in the manifest, or they can be created dynamically in code (as BroadcastReceiver objects) and registered with the system by callingContext.registerReceiver().

Page 38: Application Fundamentals

Activating Activity How does Android know which activity will be

activated when “sees” an intent?◦ An Intent object can explicitly name a target component.◦ If a target is not explicitly named

Looking for IntentFilters IntentFilters

◦ define relationship between the Intent and the application◦ specified to the data portion of the Intent, the action

portion, or both. ◦ Contain a field known as a category. A category helps

classify the action. CATEGORY_LAUNCHER - instructs Android that the Activity

containing this IntentFilter should be visible in the home screen.

Page 39: Application Fundamentals

the activity is the entry point for the application, the initial one users would see when they choose the application in the launcher.

The second filter declares an action that the activity can perform on a particular type of data.

Page 40: Application Fundamentals
Page 41: Application Fundamentals

Passing an Intent object to Context.startService()◦ Android calls the service's onStart() method and passes it the

Intent object. Passing an intent to Context.bindService()

◦ to establish an ongoing connection between the calling component and a target service.

◦ The service receives the Intent object in an onBind() call. If the service is not already running, bindService() can optionally start

it◦ For example, an activity might establish a connection with the

music playback service mentioned earlier so that it can provide the user with the means (a user interface) for

controlling the playback. ◦ The activity would call bindService() to set up that connection, and

then call methods defined by the service to affect the playback.

Activating Service 

Page 42: Application Fundamentals

This Services Demo is simple as it plays a audio file and by listening to click events of the buttons invokes the MyService service. 

Demo

Page 43: Application Fundamentals
Page 44: Application Fundamentals
Page 45: Application Fundamentals