【Android论文栏目提醒】:网学会员--在 Android论文编辑为广大网友搜集整理了:应用程序基础Android Developers 毕业论文英文翻译 - 毕业设计绩等信息,祝愿广大网友取得需要的信息,参考学习。
Application Fundamentals
Android applications are written in the Java programming language. The compiledJava code — along with any data and resource files required by the application — isbundled by the aapt tool into an
Android package an archive file marked by an .apksuffix. This file is the vehicle for distributing the application and installing it on mobiledevices its the file users download to their devices. All the code in a single .apk file isconsidered to be one application. In many ways each
Android application lives in its own world: 1. By default every application runs in its own Linux process.
Android starts theprocess when any of the applications code needs to be executed and shuts down theprocess when its no longer needed and system resources are required by otherapplications. 2. Each process has its own virtual machine VM so application code runs inisolation from the code of all other applications. 3. By default each application is assigned a unique Linux user ID. Permissions are setso that the applications 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. Its possible to arrange for two applications to share the same user ID in which casethey will be able to see each others files. To conserve system resources applicationswith the same ID can also arrange to run in the same Linux process sharing the sameVM.Application Components A central feature of
Android is that one application can make use of elements of otherapplications provided those applications permit it. For example if your applicationneeds to display a scrolling list of images and another application has developed asuitable scroller and made it available to others you can call upon that scroller to do thework rather than develop your own. Your application doesnt incorporate the code of theother application or link to it. Rather it simply starts up that piece of the otherapplication when the need arises. For this to work the system must be able to start an application process when any partof it is needed and instantiate the
Java objects for that part. Therefore unlikeapplications on most other systems
Android applications dont have a single entry pointfor everything in the application no main function for example. Rather they haveessential components that the system can instantiate and run as needed. There are fourtypes of components:Activities An activity presents a visual user interface for one focused endeavor the user canundertake. For example an activity might present a list of menu items users can choosefrom or it might display photographs along with their captions. A text messagingapplication might have one activity that shows a list of contacts to send messages to asecond activity to write the message to the chosen contact and other activities to reviewold messages or change settings. Though they work together to form a cohesive userinterface each activity is independent of the others. Each one is implemented as asubclass of the Activity base class. An application might consist of just one activity or like the text messagingapplication just mentioned it may contain several. What the activities are and howmany there are depends of course on the application and its design. Typically one ofthe activities is marked as the first one that should be presented to the user when theapplication is launched. Moving from one activity to another is accomplished by havingthe current activity start the next one. Each activity is given a default window to draw in. Typically the window fills thescreen but it might be smaller than the screen and float on top of other windows. Anactivity can also make use of additional windows — for example a pop-up dialog thatcalls for a user response in the midst of the activity or a window that presents users withvital information when they select a particular item on-screen. The visual content of the window is provided by a hierarchy of views — objectsderived from the base View class. Each view controls a particular rectangular spacewithin the window. Parent views contain and organize the layout of their children. Leafviews those at the bottom of the hierarchy draw in the rectangles they control andrespond to user actions directed at that space. Thus views are where the activitysinteraction with the user takes place. For example a view might display a small image and initiate an action when the usertaps that image.
Android has a number of ready-made views that you can use —including buttons text fields scroll bars menu items check boxes and more. A view hierarchy is placed within an activitys window by theActivity.setContentView method. The content view is the View object at the root of thehierarchy. See the separate User Interface document for more information on views andthe hierarchy.Services A service doesnt have a visual user interface but rather runs in the background for anindefinite period of time. For example a service might play background music as theuser attends to other matters or it might fetch data over the network or calculatesomething and provide the result to activities that need it. Each service extends theService base class. A prime example is a media player playing songs from a play list. The playerapplication would probably have one or more activities that allow the user to choosesongs and start playing them. However the music playback itself would not be handledby an activity because users will expect the music to keep playing even after they leavethe player and begin something different. To keep the music going the media playeractivity could start a service to run in the background. The system would then keep themusic playback service running even after the activity that started it leaves the screen. Its possible to connect to bind to an ongoing service and start the service if its notalready running. While connected you can communicate with the service through aninterface that the service exposes. For the music service this interface might allow usersto pause rewind stop and restart the playback. Like activities and the other components services run in the main thread of theapplication process. So that they wont block other components or the user interfacethey often spawn another thread for time-consuming tasks like music playback. SeeProcesses and Threads later.Broadcast receivers A broadcast receiver is a component that does nothing but receive and react tobroadcast announcements. Many broadcasts originate in system code — for exampleannouncements that the timezone has changed that the battery is low that a picture hasbeen taken or that the user changed a language preference. Applications can also initiatebroadcasts — for example to let other applications know that some data has beendownloaded to the device and is available for them to use. An application can have any number of broadcast receivers to respond to anyannouncements it considers important. All receivers extend the BroadcastReceiver baseclass. Broadcast receivers do not display a user interface. However they may start anactivity in response to the information they receive or they may use theNotificationManager to alert the user. Notifications can get the users attention in variousways — flashing the backlight vibrating the device playing a sound and so on. Theytypically place a persistent icon in the status bar which users can open to get themessage.Content providers A content provider makes a specific set of the applications data available to otherapplications. The data can be stored in the file system in an SQLite database or in anyother manner that makes sense. The content provider extends the ContentProvider baseclass to implement a standard set of methods that enable other applications to retrieveand store data of the type it controls. However applications do not call these methodsdirectly. Rather they use a ContentResolver object and call its methods instead. AContentResolver can talk to any content provider it cooperates with the provider tomanage any interprocess communication thats involved. See the separate Content Providers document for more information on using contentproviders. Whenever theres a request that should be handled by a particular component Androidmakes sure that the application process of the component is running starting it ifnecessary and that an appropriate instance of the component is available creating theinstance if necessary.Activating components: intents Content providers are activated when theyre targeted by a request from aContentResolver. The other three components — activities services and broadcastreceivers — are activated by asynchronous messages called intents. An intent is anIntent object that holds the content of the message. For activities and services it namesthe action being requested and specifies the URI of the data to act on among otherthings. For example it might convey a request for an activity to present an image to theuser or let the user edit some text. For broadcast receivers theIntent object names the action being announced. For example it might announce tointerested parties that the camera button has been pressed. There are separate methods for activating each type of component: 1. An activity is launched or given something new to do by passing an Intent objectto Context.startActivity or Activity.startActivityForResult. The responding activitycan look at the initial intent that caused it to be launched by calling its getIntentmethod.
Android calls the activitys onNewIntent method to pass it any subsequentintents. One activity often starts the next one. If it expects a result back from the activityits starting it calls startActivityForResult instead of startActivity. For example ifit starts an activity that lets the user pick a photo it might expect to be returned thechosen photo. The result is returned in an Intent object thats passed to the callingactivitys onActivityResult method. 2. A service is started or new instructions are given to an ongoing service by passingan Intent object to Context.startService.
Android calls the services onStart methodand passes it the Intent object. Similarly an intent can be passed to Context.bindServiceto 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 alreadyrunning bindService can optionally start it. For example an activity might establish aconnection with the music playback service mentioned earlier so that it can provide theuser with the means a user interface for controlling the playback. The activity wouldcall bindService to set up that connection and then call methods defined by the serviceto affect the playback.A later section Remote procedure calls has more details about binding to a service. 3. An application can initiate a broadcast by passing an Intent object to methods likeContext.sendBroadcast Context.sendOrderedBroadcast andContext.sendStickyBroadcast in any of their variations.
Android delivers the intent to all interested broadcast receivers by calling theironReceive methods. For more on intent messages see the separate article Intents andIntent Filters.Shutting down components A content provider is active only while its responding to a request from aContentResolver. And a broadcast receiver is active only while its responding to abroadcast message. So theres no need to explicitly shut down these components.Activities on the other hand provide the user interface. Theyre in a long-runningconversation with the user and may remain active even when idle as long as theconversation continues. Similarly services may also remain running for a long time. SoAndroid has methods to shut down activities and services in an orderly way: 1. An activity can be shut down by calling its finish method. One activity can shutdown another activity one it started with startActivityForResult by callingfinishActivity. 2. A service can be stopped by calling its stopSelf method or by callingContext.stopService. Components might also be shut down by the system when they are no longer beingused or when
Android must reclaim memory for more active components. A latersection Component Lifecycles discusses this possibility and its ramifications in moredetail.The manifest file Before
Android can start an application component it must learn that the componentexists. Therefore applications declare their components in a manifest file thats bundledinto the
Android package the .apk file that also holds the applications code files andresources. The manifest is a structured XML file and is always named AndroidManifest.xml forall applications. It does a number of things in addition to declaring the applicationscomponents such as naming any libraries the application needs to be linked againstbesides the default
Android library and identifying any per
missions the applicationexpects to be granted. But the principal task of the manifest is to inform
Android about the applicationscomponents. For example an activity might be declared as follows: The name attribute of the ltactivitygt element names the Activity subclass thatimplements the activity. The icon and label attributes point to resource files containingan icon and label that can be displayed to users to represent the activity. The other components are declared in a similar way — ltservicegt elements forservices ltreceivergt elements for broadcast receivers and ltprovidergt elements forcontent providers. Activities services and content providers that are not declared in themanifest are not visible to the system and are consequently never run. Howeverbroadcast receivers can either be declared in the manifest or they can be createddynamically in code as BroadcastReceiver objects and registered with the system bycalling Context.registerReceiver. For more on how to structure a manifest file for your application see The AndroidManifest.
xml File.Intent filters An Intent object can explicitly name a target component. If it does
Android finds thatcomponent based on the declarations in the manifest file and activates it. But if a targetis not explicitly named
Android must locate the best component to respond to the intent.It does so by comparing the Intent object to the intent filters of potential targets. Acomponents intent filters inform
Android of the kinds of intents the component is ableto handle. Like other essential information about the component theyre declared in themanifest file. Heres an extension of the previous example that adds two intent filters tothe activity: The first filter in the example — the combination of the action quotandroid.intent.action.MAINquot and the category quotandroid.intent.category.LAUNCHERquot — is a common one. It marks the activityas one that should be represented in the application launcher the screen listingapplications users can launch on the device. In other
words the activity is the entry pointfor the application the initial one users would see when they choose the application inthe launcher. The second filter declares an action that the activity can perform on a particular typeof data. A component can have any number of intent filters each one declaring a different setof capabilities. If it doesnt have any filters it can be activated only by intents thatexplicitly name the component as the target. For a broadcast receiver thats created and registered in code the intent filter isinstantiated directly as an IntentFilter object. All other filters are set up in the manifest. For more on intent filters see a separate document Intents and Intent Filters. 应用程序基础
Android Developers
Android 应用
程序使用 Java 编程语言开发。
aapt 工具把编译后的 Java 代码连同应用程序所需的其他数.