初探Android四大组件

平时手机玩得挺多,但对于我自己对于一个APP的构成却是不太了解。我了解到的一些Android教程、书籍中,有提到“四大组件”这个词,一听就挺重要的,那么就先来了解一下。

首先,“四大组件”就是Activity、Service、BroadcastReceiver、ContentProvider这四个东西,它们的基本功能如下:

组件基本功能
Activity界面展示
Service后台服务
BroadcastReceiver发送、接收广播
ContentProvider跨程序共享数据

知道了基本功能,就再稍微深入地了解一下。
我这里直接贴上最权威的Android官方文档作为说明,
再加上郭霖大神的《第一行代码》作为辅助说明,
另外再补充一些我之后会学习的内容。

我从官网“DEVELOP”那部分截取而来的,也不能说是定义,可能稍微有点断章取义了,反正就是先了解到有这么个东西吧,其实真做个APP不一定全都用上的。

Activity

Activity官网说明截取

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of another activity (using ActivityGroup).

《第一行代码》截取

Activity是一种可以包含用户界面的组件,主要用于和用户进行交互。一个应用程序可包含零个或多个Activity。


Service

Service官网说明截取

A Service is an application component representing either an application’s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

《第一行代码》截取

Service是Android中实现程序后台运行的解决方案,它非常适合于去执行那些不需要和用户交互而且还要求长期运行的任务。Service的运行不依赖与任何界面,即使当界面被切换到后台,或者用户打开另一个应用,Service仍能正常运行。

就是你用“网易云音乐”听歌,然后切到“微信”去聊天,音乐仍然可以正常播放。


BroadcastReceiver

BroadcastReceiver官网说明截取

If you don’t need to send broadcasts across applications, consider using this class with LocalBroadcastManager instead of the more general facilities described below. This will give you a much more efficient implementation (no cross-process communication needed) and allow you to avoid thinking about any security issues related to other applications being able to receive or send your broadcasts.

《第一行代码》截取

没有一句明确的定义,截取一些稍微重要的句子吧。

  1. 为了方便于进行系统级别的消息通知,Android也引入了一套类似的广播消息机制。(类似学校教室的喇叭)
  2. Android内置了很多系统级别的广播,我们可以在应用程序中通过监听这些广播得到各种系统的状态信息。
  3. 广播是一种可以跨进程的通信方式。在我们应用内发出的广播,其他的应用应该也可以收到。(注意:可以,不代表一定要这样。)
  4. 为了简单解决广播的安全性问题,Android引入了一套本地广播机制,使用这个机制发出的广播只能够在应用程序的内部进行传递,并且BroadcastReceiver也只能接收来自本应用程序发出的广播,这样所有的安全性问题就都不存在了。

ContentProvider

ContentProvider官网说明截取

Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver interface. A content provider is only required if you need to share data between multiple applications. For example, the contacts data is used by multiple applications and must be stored in a content provider. If you don’t need to share data amongst multiple applications you can use a database directly via SQLiteDatabase.

《第一行代码》截取

  1. ContentProvider主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问数据的安全性。
  2. 当一个应用程序通过ContentProvider对其数据提供了外部访问接口,任何其他的应用程序都可以对这部分数据进行访问。Android系统中自带的电话簿、短信、媒体库等程序提供了类似的访问接口,这就使得第三方应用程序可以充分地利用这部分数据来实现更好的功能。

下一步需要学习的内容

上面已经对“四大组件”的初步概念有所了解了,接下来就要继续深入,不过内容上我还是决定有所取舍,毕竟先要还有点成果出来最实际。Activity显然是最重要的,毕竟用户首先关注的就是你应用的界面。其次就是Service,让应用能够通过后台服务,进行一些提升用户体验的操作。再然后就是BroadcastReceiver,可以通过广播,进行一些锦上添花的消息提示。ContentProvider暂时就不需要了,独立开发的应用我想暂时也没有提供数据给别的应用这个需要。

综上所述,接下来需要学习一下以下的内容:
1. 怎么创建Activity?
2. Activity之间的跳转
3. Activity的生命周期
4. 了解一些UI控件
5. 接收系统广播
6. 发送本地广播
7. Service的基本用法
8. Activity和Service之间进行通信

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值