Application Fundamentals 应用基础,Android面试宝典

Activities

An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to 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. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.an activity is implemented as a subclass of [Activity]( ) and you can learn more about it in the Activities developer guide.

Activity提供了一个用户接口的单一界面,例如:一个Email程序可以提供一个activity用来显示一列新邮件,另一个activity可以写一封新邮件,同时还有一个activity用来读取邮件。尽管这些activity共同工作在一个email应用中形成一个有结合力的用户体验,每一个都独立与另外一个。同样的,不同的应用可以启动任何一个activity。例如,一个摄像程序可以启动一个activity在email程序中用来编写一封邮件,以便为了用户分享一张照片。

Services

A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it.

Service是一个在后台不间断运作的组件或者为远程进程进行工作的组件。service没有一个用户界面。例如:但用户处理其他事情时,一个service可以背景播放音乐、后台读取网络数据或者处理其他的东西并且提供给其他的activity。另一个组件,类似与activity,可以启动服务并且让他开始运作或者绑定他为了和他进行交互。


Broadcast receivers

A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcasts—for example, to let other applications know that some data has been downloaded to the device and is available for them to use. Although broadcast receivers don’t display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs. More commonly, though, a broadcast receiver is just a “gateway” to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.

Broadcast receiver这个组件响应Broadcast广播通知。很多广播来源系统,例如通知屏幕已经锁住、低电量、图片被选中、此时用户开始广播,应用程序也可以初始化广播,例如,让其他应用程序知道一些数据已经开始下载到设备上并且可以为他们使用,尽管广播接受者没有显示用户界面,他们可以在广播事件发生时创建一个状态栏通知来警示用户。更通常的是,尽管广播接受者仅仅是一个通道来通知其他组件并且做了非常少量的工作。例如,他可能基于一些事件来初始化某些服务

Content providers 内容提供

Content providers为其他程序提供数据集,这些数据可以保存在系统文件里或者sqlite数据库里,Content providers继承自 ContentProvider基类,实现了一些标准的方法,可以 让程序检索或者改写其中的数据。程序不能直接的调用那些方法。ContentResolver不能与content provider直接通信,但可以用过ContentResolver 。ContentResolver 可以跟任何的content provider通信,可以与provider合作管理通 信进程。

更多信息查看Content Providers文档。

当有需要特定的请求需要被处理时,Android都会确保他正在运行,需要时会创建一个实例。

Activating Components

Three of the four component types—activities, services, and broadcast receivers—are activated by an asynchronous message called an intent. Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your application or another.

当ContentResolver接收到一个请求时,运行content providers,其他三个组件activities、services、broadcast receiver被Intents异步消息启动。

An intent is created with an [Intent]( ) object, which defines a message to activate either a specific component or a specific type of component—an intent can be either explicit or implicit, respectively.

一个Intent可以被Intent对象创建,它定义了一条消息来激活一个指定的组件或者一类特定的组件,一个Intent可以分别显示或者隐式指定。

For activities and services, an intent defines the action to perform (for example, to “view” or “send” something) and may specify the URI of the data to act on (among other things that the component being started might need to know). For example, an intent might convey a request for an activity to show an image or to open a web page. In some cases, you can start an activity to receive a result, in which case, the activity also returns the result in an [Intent]( ) (for example, you can issue an intent to let the user pick a personal contact and have it returned to you—the return intent includes a URI pointing to the chosen contact)。

Intent可以传递给组件信息,比如显示一张图片或者打开一个网页,在一些情况下,你可以开启一个activity来接受结果,并且这个activity可以返回结果到Intent中。

For broadcast receivers, the intent simply defines the announcement being broadcast (for example, a broadcast to indicate the device battery is low includes only a known action string that indicates “battery is low”)

对于广播接受者,Intent简单的定义了一个通知。

The other component type, content provider, is not activated by intents. Rather, it is activated when targeted by a request from a [ContentResolver]( ). The content resolver handles all direct transactions with the content provider so that the component that’s performing transactions with the provider doesn’t need to and instead calls methods on the [ContentResolver]( ) object. This leaves a layer of abstraction between the content provider and the component requesting information (for security).

对于其他组件类型,内容提供者,不是被Intent所激活,而是被目标contentResolver所激活。

  • You can start an activity (or give it something new to do) by passing an [Intent]( ) to [startActivity()]( ) or [startActivityForResult()]( ) (when you want the activity to return a result).

你可以通过startActivity或者startActivtiyForRespult来启动一个activity

  • You can start a service (or give new instructions to an ongoing service) by passing an [Intent]( ) to [startService()]( ). Or you can bind to the service by passing an[Intent]( ) to [bindService()]( ).

你可以启动一个service,通过Intent到startService,或者你可以绑定一个服务通过bindService

  • You can initiate a broadcast by passing an [Intent]( ) to methods like [sendBroadcast()]( )[sendOrderedBroadcast()]( ), or [sendStickyBroadcast()]( ).

你可以初始化一个broadcast通过传一个 [sendBroadcast()]( )[sendOrderedBroadcast()]( ), or [sendStickyBroadcast()]( ).

  • You can perform a query to a content provider by calling [query()]( ) on a [ContentResolver]( ) 你可以执行一个查询通过调用 `[query()]( )` on a `[ContentResolver]( ) `

The Manifest File


Before the Android system can start an application component, the system must know that the component exists by reading the application’sAndroidManifest.xml file (the “manifest” file). Your application must declare all its components in this file, which must be at the root of the application project directory.

在Android系统启动一个程序组件之前,系统必须通过读取AndroidManifest文件来知道需要使用哪些组件,你的程序必须声明所有的组件在那个文件里面,文件在工程的根目录下。

The manifest does a number of things in addition to declaring the application’s components, such as:

  • Identify any user permissions the application requires, such as Internet access or read-access to the user’s contacts.

确认程序访问网络,读取用户通讯录的权限等

  • Declare the minimum API Level required by the application, based on which APIs the application uses.

声明程序的最低的APILevel

  • Declare hardware and software features used or required by the application, such as a camera, bluetooth services, or a multitouch screen.

声明程序需要使用到硬件与软件的功能

API libraries the application needs to be linked against (other than the Android framework APIs), such as the Google Maps library.

除了Android Frame API文件里面的Library需要进行链接

Declaring components

如下声明Manifest文件

[html] view plain copy print ?

  1. <?xml** version\="1.0" encoding\="utf-8"**?>

  2. <manifest … >

  3. <application android:icon="@drawable/app_icon.png" … >

  4. <activity android:name=“com.example.project.ExampleActivity”

  5. android:label="@string/example_label" … >

  6. </activity>

  7. </application>

  8. </manifest>

<?xml version="1.0" encoding="utf-8"?>

设置应用的图标

In the [<activity>]( ) element, the android:name attribute specifies the fully qualified class name of the [Activity]( ) subclass and the android:label attributes specifies a string to use as the user-visible label for the activity.

设置activity的显示名称

You must declare all application components this way:

  • [<activity>]( ) elements for activities

  • [<service>]( ) elements for services

  • [<receiver>]( ) elements for broadcast receivers

  • [<provider>]( ) elements for content providers

使用上面的标签进行相应的声明

Activities, services, and content providers that you include in your source but do not declare in the manifest are not visible to the system and, consequently, can never run.However, broadcast receivers can be either declared in the manifest or created dynamically in code (as [BroadcastReceiver]( ) objects) and registered with the system by calling [**registerReceiver()**]( )没有声明的组件对系统不可见,也不可以运行,然而,broadcast receiver可以在代码中被动态的创建,并且被系统使用registerReceiver进行注册

Declaring component capabilities

As discussed above, in Activating Components, you can use an [Intent]( ) to start activities, services, and broadcast receivers. You can do so by explicitly naming the target component (using the component class name) in the intent. However, the real power of intents lies in the concept of intent actions. With intent actions, you simply describe the type of action you want to perform (and optionally, the data upon which you’d like to perform the action) and allow the system to find a component on the device that can perform the action and start it. If there are multiple components that can perform the action described by the intent, then the user selects which one to use.

我们可以使用Intent来启动activity,service,broadcast Receiver。你可以在Intent中明确指定目标组件。然而Intent的体现依赖于Intent动作的概念。在Intent动作下,你可以简单的描述你希望执行的动作类型,并且允许系统在设备上找到相应的组件并启动它。如果有多种组件可以执行Intent描述的这个动作,那么用户来选择使用哪一个

The way the system identifies the components that can respond to an intent is by comparing the intent received to the intent filters provided in the manifest file of other applications on the device.

系统是与从Manifest文件中提供的Intent filter进行对比来选择相应的组件

When you declare a component in your application’s manifest, you can optionally include intent filters that declare the capabilities of the component so it can respond to intents from other applications. You can declare an intent filter for your component by adding an <intent-filter> element as a child of the component’s declaration element.

当你在应用的Manifest文件中声明一个组件时,你可以选择是否添加Intent filter,它用来声明组件相应其他应用Intent的能力。你可以使用  [<intent-filter>]( )标签作为组件声明标签的自标签来声明一个Intent filter。

Declaring application requirements

There are a variety of devices powered by Android and not all of them provide the same features and capabilities. In order to prevent your application from being installed on devices that lack features needed by your application, it’s important that you clearly define a profile for the types of devices your application supports by declaring device and software requirements in your manifest file. Most of these declarations are informational only and the system does not read them, but external services such as Android Market do read them in order to provide filtering for users when they search for applications from their device.

有许多不同的Android设备,并不是所有设备都提供同样的功能与能力。为了防止你的应用装上设备后却发现设备缺少应用需要的功能,必须清楚的在Manifest文件中为你的应用支持的设备类型与软件需要定义属性。大多数声明仅仅是个信息而系统并不读取他们,但是外部服务如Android market在他们从设备上查找应用时会去读取filter。

However, you can also declare that your applicaiton uses the camera, but does not require it. In that case, your application must perform a check at runtime to determine if the device has a camera and disable any features that use the camera if one is not available

你可以声明你的应用来使用camera,当并不是必须的。在那种情况下,你的应用在执行的时候会去验证是否这个设备具有camera,并且会在其中一个需要不能提供的时候关闭所有的功能。

下面是我们开发应用时需要考虑的设备属性:

Screen size and density:屏幕大小与分辨率

最后

光有这些思路和搞懂单个知识的应用是还远远不够的,在Android开源框架设计思想中的知识点还是比较多的,想要搞懂还得学会整理和规划:我们常见的**Android热修复框架、插件化框架、组件化框架、图片加载框架、网络访问框架、RxJava响应式编程框架、IOC依赖注入框架、最近架构组件Jetpack等等Android第三方开源框架,**这些都是属于Android开源框架设计思想的。如下图所示:

image

这位阿里P8大佬针对以上知识点,熬夜整理出了一本长达1042页的完整版如何解读开源框架设计思想PDF文档,内容详细,把Android热修复框架、插件化框架、组件化框架、图片加载框架、网络访问框架、RxJava响应式编程框架、IOC依赖注入框架、最近架构组件Jetpack等等Android第三方开源框架这些知识点从源码分析到实战应用都讲的简单明了。

由于文档内容过多,篇幅受限,只能截图展示部分,更为了不影响阅读,这份文档已经打包在GitHub,有需要的朋友可以直接点此处前往免费下载

image

image

整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~

id-P7/blob/master/Android%E5%BC%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)。**

[外链图片转存中…(img-Qz5dapVu-1647446012320)]

[外链图片转存中…(img-Cd8waUkv-1647446012321)]

整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~

你的支持,我的动力;祝各位前程似锦,offer不断!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值