1 / 46

Application Fundamentals

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 

keira
Download Presentation

Application Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Application Fundamentals

  2. 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  • 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.

  3. Android application • 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.

  4. Application Components • Activities • Services • Broadcast receivers • Content providers

  5. Application Components • 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.

  6. Add Activities, Services , Broadcast receivers , Content providers here

  7. Activities • 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.

  8. Activities • 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.

  9. Activities • 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.

  10. Activities • 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.

  11. Services • 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

  12. Broadcast receivers • 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.

  13. Broadcast receivers • 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.

  14. Content providers • 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.

  15. Application Components • 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. 

  16. Application Activities Intent Services Broadcast receivers ContentResolver Content providers

  17. Activating components:Intents • 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.

  18. Activating components: Intents • 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.

  19. Intents • 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.

  20. Intents: examples of action/data pairs • ACTION_VIEWcontent://contacts/people/1 -- Display information about the person whose identifier is "1". • ACTION_DIALcontent://contacts/people/1 -- Display the phone dialer with the person filled in. • ACTION_VIEWtel: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_DIALtel:123 -- Display the phone dialer with the given number filled in. • ACTION_EDITcontent://contacts/people/1 -- Edit information about the person whose identifier is "1". • ACTION_VIEWcontent://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_VIEWcontent://contacts/N } being used to start an activity to display that person.

  21. Intents Example • Following fragments calls an Intent whose job is to invoke a built-in task (ACTION_VIEW) and explore the Contacts available in the phone. Intent myIntent= new Intent( Intent.ACTION_VIEW, Uri.parse("content://contacts/people")); startActivity(myIntent);

  22. 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); • } • }

  23. Intents Example • Following Intent uses built-in task (ACTION_VIEW) to explore a web page Intent myIntent= new Intent( Intent.ACTION_VIEW, Uri.parse(" "http://www.google.com") ); startActivity(myIntent);

  24. Intents Example • Following Intent uses built-in task (ACTION_VIEW) to make a phone call

  25. Intents to Activating Activity • 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.

  26. Intent • 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..

  27. Intent filters • 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. • IntentFilters • 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.

  28. Intent filters • 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

  29. Intent Test • 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

  30. Intent filters-Action test • 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)

  31. Intent filters-Category test • 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 Full string values, don’t use constants, e,g., CATEGORY_BROWSABLE

  32. Intent filters-Data test • Each <data> element can specify • a URI • scheme://host:port/path • content://com.example.project:200/folder/subfolder/etc • data type (MIME media type).

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

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

  35. Activating Activity • 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.

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

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

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

  39. Activating Service  • 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.

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

  41. Activating Broadcast  • An application can initiate a broadcast by passing an Intent object to methods • Context.sendBroadcast(), • Context.sendOrderedBroadcast(),and  • Context.sendStickyBroadcast() • Android delivers the intent to all interested broadcast receivers by calling their onReceive() methods.

More Related