Android.Documentation

本文深入介绍了Android应用程序的四大核心组件:Activity、IntentReceiver、Service和ContentProvider。详细解释了每个组件的功能及其应用场景,并探讨了它们如何协同工作以构建完整的Android应用。

这两天一直在研究Android,有些辛苦!

  • Activity
  • Intent Receiver
  • Service
  • Content Provider

在Android应用程序中有四个构建块:

  • 活动
  • 活动内容接受器(意图接收器?)
  • 服务
  • 内容提供器

Activity

是Android构造块中最基本的一种,在应用中,一个activity通常就是一个单独的屏幕。每一个activity都被实现为一个独立的类,并且继承于Activity这个基类。这个activity类将会显示由几个Views控件组成的用户接口,并对事件做出响应。大部份的应用都会包含多个的屏幕。例如,一个短消息应用程序将会有一个屏幕用于显示联系人列表,第二个屏幕用于写短消息,同时还会有用于浏览旧短消息及进行系统设置的屏幕。每一个这样的屏幕,就是一个activity。从一个屏幕导航到另一个屏幕是很简单的。在一些应用中,一个屏幕甚至会返回值给前一个屏幕。

当一个新的屏幕打开后,前一个屏幕将会暂停,并保存在历史堆栈中。用户可以返回到历史堆栈中的前一个屏幕。当屏幕不再使用时,还可以从历史堆栈中删除。默认情况下,Android将会保留从主屏幕到每一个应用的运行屏幕。

Intent and Intent Filters

Android使用了Intent这个特殊类,实现在屏幕与屏幕之间移动。Intent类用于描述一个应用想要做什么事。在Intent的描述结构中,有两个最重要的部分:动作和动作对应的数据。典型的动作类型有:MAIN(activity的门户)、VIEW、PICK、EDIT等。而动作对应的数据则以URI的形式进行表示。例如:要查看一个人的联系方式,你需要创建一个动作类型为VIEW的intent,以及一个表示这个人的URI。

Android利用一个叫Intent的特殊类来实现屏幕间的切换。一个Intent描述了一个应用程序想要做什么。intent数据结构中两个最重要的部分是动作和动作对应的数据。一个典型动作的值是MAIN(活动的入口),VIEW,PICK,EDIT,和其它一些。数据表示为URI。例如,浏览一个人的联系信息,你应该创建一个带有VIEW活动的intent,并且URI的数据集指向那个人。

与之有关系的一个类叫IntentFilter。相对于intent是一个有效的做某事的请求,一个intent filter则用于描述一个activity(或者Intent Receiver)能够操作哪些intent。一个activity如果要显示一个人的联系方式时,需要声明一个IntentFilter,这个IntentFilter要知道怎么去处理VIEW动作和表示一个人的URI。IntentFilter需要在AndroidManifest.xml中定义。

一个屏幕到另一个屏幕之间的导航是通过解析意图(Intent)来实现的。当向前导航时,activity将会调用startActivity (Intent myIntent)方法。然后,系统会在所有安装的应用程序中定义的IntentFilter中查找,找到最匹配myIntent的 Intent对应的activity。新的activity接收到myIntent的通知后,开始运行。当startActivity方法被调用将触发解析 myIntent的动作,这个机制提供了两个关键好处:

  • Activity能够简单的通过在Intent表中发送请求,从其他组件中复用功能
  • Activity能够在任何时候由一个带有相同IntentFilter的Activity替换

Intent Receiver

当你希望你的应用能够对一个外部的事件(如当电话呼入时,或者数据网络可用时,或者到了晚上时)做出响应,你可以使用一个Intent Receiver。虽然Intent Receiver在感兴趣的事件发生时,会使用NotificationManager 通知用户,但它并不能生成一个UI。Intent Receiver在AndroidManifest.xml中注册,但也可以在代码中使用 Context.registerReceiver()进行注册。当一个intent receiver被触发时,你的应用不必对请求调用intent receiver,系统会在需要的时候启动你的应用。各种应用还可以通过使用Context.broadcastIntent()将它们自己的 intent receiver广播给其它应用程序。

Service

一个Service是一段长生命周期的,没有用户界面的程序。比较好的一个例子就是一个正在从播放列表中播放歌曲的媒体播放器。在一个媒体播放器的应用中,应该会有多个activity,让使用者可以选择歌曲并播放歌曲。然而,音乐重放这个功能并没有对应的activity,因为使用者当然会认为在导航到其它屏幕时音乐应该还在播放的。在这个例子中,媒体播放器这个activity会使用Context.startService()来启动一个service,从而可以在后台保持音乐的播放。同时,系统也将保持这个service一直执行,直到这个service运行结束。另外,我们还可以通过使用Context.bindService()方法,连接到一个service上(如果这个service还没有运行将启动它)。当连接到一个service之后,我们还可以service提供的接口与它进行通讯。拿媒体播放器这个例子来说,我们还可以进行暂停、重播等操作。

Content Provider

应用程序能够将它们的数据保存到文件中、SQL数据库中,甚至是任何有效的设备中。当你想将你的应用数据与其它的应用共享时,Content Provider将会很有用。一个Content Provider类实现了一组标准的方法,从而能够让其它的应用保存或读取此Content Provider处理的各种数据类型。

更详细的Content Provider资料,可以参考附带文档中的Accessing Content Providers。

Executing tasks: [:app:assembleDebug] in project D:\TestProject\MyApplication > Task :app:createDebugVariantModel UP-TO-DATE > Task :app:preBuild UP-TO-DATE > Task :app:preDebugBuild UP-TO-DATE > Task :app:mergeDebugNativeDebugMetadata NO-SOURCE > Task :app:compileDebugAidl NO-SOURCE > Task :app:compileDebugRenderscript NO-SOURCE > Task :app:generateDebugBuildConfig UP-TO-DATE > Task :app:javaPreCompileDebug UP-TO-DATE > Task :app:checkDebugAarMetadata UP-TO-DATE AGPBI: {"kind":"warning","text":"Your project has set `android.useAndroidX=true`, but configuration `:app:debugRuntimeClasspath` still contains legacy support libraries, which may cause runtime issues.\nThis behavior will not be allowed in Android Gradle plugin 8.0.\nPlease use only AndroidX dependencies or set `android.enableJetifier=true` in the `gradle.properties` file to migrate your project to AndroidX (see https://developer.android.com/jetpack/androidx/migrate for more info).\nThe following legacy support libraries are detected:\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> com.android.support:support-annotations:28.0.0\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> com.android.support:collections:28.0.0\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> android.arch.lifecycle:runtime:1.1.1\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> android.arch.lifecycle:runtime:1.1.1 -> android.arch.lifecycle:common:1.1.1\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> android.arch.lifecycle:runtime:1.1.1 -> android.arch.core:common:1.1.1\n:app:debugRuntimeClasspath -> com.android.support:support-compat:28.0.0 -> com.android.support:versionedparcelable:28.0.0","sources":[{}]} > Task :app:generateDebugResValues UP-TO-DATE > Task :app:mapDebugSourceSetPaths UP-TO-DATE > Task :app:generateDebugResources UP-TO-DATE > Task :app:mergeDebugResources UP-TO-DATE > Task :app:packageDebugResources UP-TO-DATE > Task :app:parseDebugLocalResources UP-TO-DATE > Task :app:createDebugCompatibleScreenManifests UP-TO-DATE > Task :app:extractDeepLinksDebug UP-TO-DATE > Task :app:processDebugMainManifest FAILED [androidx.versionedparcelable:versionedparcelable:1.1.1] C:\Users\wangsen\.gradle\caches\transforms-3\4b621581b14be943fef7e9025808a5ef\transformed\versionedparcelable-1.1.1\AndroidManifest.xml Warning: Namespace 'androidx.versionedparcelable' used in: androidx.versionedparcelable:versionedparcelable:1.1.1, com.android.support:versionedparcelable:28.0.0. D:\TestProject\MyApplication\app\src\main\AndroidManifest.xml:22:18-91 Error: Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 is also present at [androidx.core:core:1.7.0] AndroidManifest.xml:24:18-86 value=(androidx.core.app.CoreComponentFactory). Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:7:5-25:19 to override. See https://developer.android.com/r/studio-ui/build/manifest-merger for more information about the manifest merger. > Task :app:mergeDebugShaders UP-TO-DATE > Task :app:compileDebugShaders NO-SOURCE > Task :app:generateDebugAssets UP-TO-DATE > Task :app:mergeDebugAssets UP-TO-DATE > Task :app:compressDebugAssets UP-TO-DATE > Task :app:processDebugJavaRes NO-SOURCE > Task :app:desugarDebugFileDependencies UP-TO-DATE > Task :app:mergeDebugJniLibFolders UP-TO-DATE > Task :app:mergeDebugNativeLibs NO-SOURCE > Task :app:stripDebugDebugSymbols NO-SOURCE > Task :app:validateSigningDebug UP-TO-DATE > Task :app:writeDebugAppMetadata UP-TO-DATE > Task :app:writeDebugSigningConfigVersions UP-TO-DATE > Task :app:checkDebugDuplicateClasses FAILED > Task :app:mergeDebugJavaResource FAILED FAILURE: Build completed with 3 failures. 1: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:processDebugMainManifest'. > Manifest merger failed with multiple errors, see logs * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. ============================================================================== 2: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:checkDebugDuplicateClasses'. > A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable > Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.graphics.drawable.IconCompatParcelizer found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.ResultReceiver$1 found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class androidx.core.graphics.drawable.IconCompatParcelizer found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class androidx.core.internal.package-info found in modules core-1.7.0-runtime (androidx.core:core:1.7.0) and support-compat-28.0.0-runtime (com.android.support:support-compat:28.0.0) Duplicate class androidx.versionedparcelable.CustomVersionedParcelable found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.NonParcelField found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.ParcelField found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.ParcelImpl found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.ParcelImpl$1 found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.ParcelUtils found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcel found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcel$1 found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcel$ParcelException found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcelParcel found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcelStream found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcelStream$FieldBuffer found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcelable found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Duplicate class androidx.versionedparcelable.VersionedParcelize found in modules versionedparcelable-1.1.1-runtime (androidx.versionedparcelable:versionedparcelable:1.1.1) and versionedparcelable-28.0.0-runtime (com.android.support:versionedparcelable:28.0.0) Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>. * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. ============================================================================== 3: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':app:mergeDebugJavaResource'. > A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction > 2 files found with path 'META-INF/androidx.core_core.version' from inputs: - C:\Users\wangsen\.gradle\caches\transforms-3\0b22a6b07b3e498281e2fdb9d93c9c6b\transformed\support-compat-28.0.0\jars\classes.jar - C:\Users\wangsen\.gradle\caches\transforms-3\0212d0c0c0b99c5d00c15a82a811f4d1\transformed\core-1.7.0\jars\classes.jar Adding a packagingOptions block may help, please refer to https://developer.android.com/reference/tools/gradle-api/7.4/com/android/build/api/dsl/ResourcesPackagingOptions for more information * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. ============================================================================== * Get more help at https://help.gradle.org BUILD FAILED in 436ms 22 actionable tasks: 3 executed, 19 up-to-date
最新发布
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值