Nothing

android的四大组件

2024/07/21

四大组件

  1. 活动(activity),用于表现功能
  2. 服务(service),后台运行服务,不提供界面呈现
  3. 广播接受者(Broadcast Receive),勇于接收广播
  4. 内容提供者(Content Provider),支持多个应用中存储和读取数据,相当于数据库

活动

定义:是用户操作的可视化界面;它为用户提供了一个完成操作指令的窗口。当我们创建完毕Activity之后,需要调用setContentView()方法来完成界面的显示

一个Activity通常就是一个单独的屏幕

可以类比为windows开发中的窗口

使用

1
2
3
4
5
6
7

public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1" />

</LinearLayout>


Actiity生命周期

在Android中会维持一个Activity Stack(Activity栈),当一个新的Activity创建时,它就会放到栈顶,这个Activity就处于运行状态。当再有一个新的Activity被创建后,会重新压人栈顶,而之前的Activity则会在这个新的Activity底下,就像枪梭压入子弹一样。而且之前的Activity就会进入后台

四种状态

  • a.运行中(Running/Active):这时Activity位于栈顶,可见可交互
  • b.暂停(Paused):可见不可交互。当一个新的非全屏的Activity或者一个透明的Activity放置在栈顶,Activity就处于暂停状态;这个时候Activity的各种数据还被保持着;只有在系统内存在极低的状态下,系统才会自动的去销毁Activity。
  • c.停止(Stoped):当一个Activity被另一个Activity完全覆盖,或者点击HOME键退入了后台,这时候Activity处于停止状态。这里有些是跟暂停状态相似的:这个时候Activity的各种数据还被保持着;当系统的别的地方需要用到内容时,系统会自动的去销毁Activity。
  • d.销毁(Detroyed):点击返回键或者系统在内存不够用的情况下就会把Activity从栈里移除销毁,被系统回收,这时候,Activity处于销毁状态。

服务

它通常用作在后台处理耗时的逻辑,与Activity一样,它存在自己的生命周期,也需要在AndroidManifest.xml配置相关信息

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

不过需要注意的是,服务并不是运行在一个独立的进程当中的,而是依赖于创建服务时所在的应用程序进程

类似于c++中的窗口接口函数

service用于在后台

  • started(启动):当应用程序组件(如activity)调用startService()方法启动服务时,服务处于started状态。
  • bound(绑定):当应用程序组件调用bindService()方法绑定到服务时,服务处于bound状态。

startService()

started service(启动服务)是由其他组件调用startService()方法启动的,其生命周期与启动它的组件无关,并且可以在后台无限期运行,即使启动服务的组件已经被销毁。因此,服务需要在完成任务后调用stopSelf()方法停止,或者由其他组件调用stopService()方法停止

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class TestOneService extends Service{

@Override
public void onCreate() {
Log.i("Kathy","onCreate - Thread ID = " + Thread.currentThread().getId());
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Kathy", "onStartCommand - startId = " + startId + ", Thread ID = " + Thread.currentThread().getId());
return super.onStartCommand(intent, flags, startId);
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("Kathy", "onBind - Thread ID = " + Thread.currentThread().getId());
return null;
}

@Override
public void onDestroy() {
Log.i("Kathy", "onDestroy - Thread ID = " + Thread.currentThread().getId());
super.onDestroy();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Kathy", "Thread ID = " + Thread.currentThread().getId());
Log.i("Kathy", "before StartService");

//连续启动Service
Intent intentOne = new Intent(this, TestOneService.class);
startService(intentOne);
Intent intentTwo = new Intent(this, TestOneService.class)
//停止Service
Intent intentFour = new Intent(this, TestOneService.class);
stopService(intentFour);

//再次启动Service
Intent intentFive = new Intent(this, TestOneService.class);
startService(intentFive);

Log.i("Kathy", "after StartService");
}

广播接受器

广播接收器是对发送出来的广播进行过滤接受并响应的一类组件。可以使用广播接收器来让应用对一个外部时间做出响应,广播接收器通过NotificationManager方法来通知用户这些事情发生了。广播接收器既可以在AndroidManifest.xml中注册,也可以在运行时的代码中使用

广播接收器没有用户界面。然而,它们可以启动一个activity或serice来响应它们收到的信息,或者用NotificationManager来通知用户。通知可以用很多种方式来吸引用户的注意力,例如闪动背灯、震动、播放声音等。一般来说是在状态栏上放一个持久的图标,用户可以打开它并获取消息。

注册有两种方法,分别是程序动态注册【在运行时的代码中使用Context.registerReceive()进行注册】和AndroidManifest文件中进行静态注册

类似于windows中的消息接受处理函数

使用

创建一个广播接收器BroadcastReceiver ,广播也是通过Intent来传递数据。

1
2
3
4
5
6
7
8
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String msg=intent.getStringExtra("msg");
Log.e(TAG, "onReceive: "+msg);
}
}

清单文件注册该广播

1
2
3
4
5
6
7
8
9
10
11
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">

<receiver android:name=".receiver.MyReceiver"
android:exported="true"/>

</application>

在其它组件里面发送广播,比如Activity里面

1
2
3
4
private void Acitivity(){
Intent intent = new Intent(this, MyReceiver.class);
sendBroadcast(intent);
}

内容提供器

给多个应用提供数据,例,APP A的数据库内容不可被APP B进行读取的,此时,一个内容提供者,可以将APP A中的数据信息提供给APP B

  • 获取通讯录中的联系人,申请好友。
  • 获取其他软件搜索记录,大数据计算,进行产品推送。
  • 预约直播,将预约信息写入手机备忘

这一组件暂时不做过多记录

CATALOG
  1. 1. 四大组件
    1. 1.1. 活动
      1. 1.1.1. 使用
      2. 1.1.2. Actiity生命周期
      3. 1.1.3. 四种状态
    2. 1.2. 服务
      1. 1.2.1. service用于在后台
      2. 1.2.2. startService()
      3. 1.2.3. 使用
    3. 1.3. 广播接受器
      1. 1.3.1. 使用
    4. 1.4. 内容提供器