【Android】四大组件(Activity、Service、Broadcast Receiver、Content Provider)、结构目录

一、Android系统架构

https://blog.csdn.net/xzzteach/article/details/140904613

二、Android四大组件

(一)Activity

1.Activity特性

  • 一个 Activity 包含了用户能够看到的界面,从而于用户进行交互。一个应用程序中可以有零个或者多个Activity。零个 Activity 就表示,这个应用程序不包含与用户交互的界面。
  • Android应用中每一个Activity都必须要在AndroidManifest.xml配置文件中声明注册,否则系统将不识别也不执行该Activity。
  • 只有具备<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />属性才是入口Activity。
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

在这里插入图片描述

  1. Activity的页面跳转和关闭
  • 跳转

Intent intent = new Intent(context,目标Activity.class)
startActivity(intent)
示例:

    public void toSecond(View view) {
        Intent intent =new Intent();
        intent.setClass(this,SecondActivity.class);
        startActivity(intent);
    }
  • 关闭
    finish()
  1. Activity的生命周期
    Android 是使用任务(task)来管理Activity的,一个任务就是一组存放在栈里Activity的集合,这个栈也叫作返回栈。Activity的生命周期由以下几个部分组成:
函数描述
onCreat()一个Activity启动后第一个被调用的函数,常用来在此方法中进行Activity的一些初始化操作。例如创建View,绑定数据,注册监听,加载参数等。
onStart()当Activity显示在屏幕上时,此方法被调用但此时还无法进行与用户的交互操作。
onResume()英文词义简历、继续进行这个方法在onStart()之后调用,也就是在Activity准备好与用户进行交互的时候调用,此时的Activity一定位于Activity栈顶,处于运行状态。
onPause()这个方法是在系统准备去启动或者恢复另外一个Activity的时候调用,通常在这个方法中执行一些释放资源的方法,以及保存一些关键数据。
onStop()这个方法是在Activity完全不可见的时候调用的。
onDestroy()这个方法在Activity销毁之前调用,之后Activity的状态为销毁状态。
onRestart()当Activity从停止stop状态恢进入start状态时调用状态。

在这里插入图片描述
实际操作过程中,当打开启动Android应用时,MainActivity会经过onCreate-> onStart -> onResume三个阶段,此时若按home键或则back按键,MainActivity会经过onPause -> onStop这两个阶段,再进入此MainActivity时,会再调用onRestart -> onStart -> onResume三个阶段。

3、Activity 启动模式
启动模式一共有 4 中:standardsingleTopsingTasksingleInstance,可以在 AndroidManifest.xml 中通过 标签指定 android:launchMode 属性来选择启动模式。

standard模式

standard 模式是 Activity 的默认启动模式,在不进行显示指定的情况下,所有 Activity 都会自动使用这种启动模式。对于使用 standard 模式启动的 Activity,系统不会在乎这个 Activity 是否已经存在在栈中了。每次启动的时候都会创建一个新的实例。一般用于打开邮件之类的。
在这里插入图片描述

  • 下图为生成的默认Empty Activity

在这里插入图片描述

singleTop模式 (栈顶复用模式)

如果 Activity 指定为 singleTop,在启动 Activity 的时候发现返回栈的栈顶已经是该 Activity 了。则认为可以直接使用它,就不会再创建新的 Activity 实例了。因为不会创建新的 Activity 实例,所以 Activity 的生命周期就没有什么变化了。假设你在当前的Activity中又要启动同类型的Activity,此时建议将此类型Activity的启动模式指定为SingleTop,能够降低Activity的创建,节省内存!

一般用于登录页面,新闻详情页等。
在这里插入图片描述

singTask 模式(栈内单例模式)

singleTop 很好的解决了重复创建栈顶 Activity 的问题。如果 Activity 没有处于栈顶的位置,还是可能会创建多个 Activity 实例的。那就需要借助 singleTask 了。当 Activity 的启动模式为 singleTask 的时候,每次启动该 Activity 的时候系统会首先在返回栈中检查是否存在该 Activity 的实例,如果发现已经存在则直接使用该实例。并把这个 Activity 之上的所有 Activity 全部出栈,如果没有就会创建一个新的 Activity 实例。
一般主页面使用,购物页面,付款页面,浏览器主页等
在这里插入图片描述

  • 下图为uniapp
    在这里插入图片描述

singleInstance模式(堆内单例模式)

singleInstance 模式的 Activity 会启用一个新的返回栈来管理这个 Activity 。在这种模式下会有一个单独的返回栈来管理这个 Activity,不管是哪个应用程序来访问这个 Activity,都共用的同一个返回栈,也就解决了共享 Activity 实例的问题。

在这里插入图片描述

4、Activity 之间相互跳转及通信
Intent(意图)是应用程序种各个组件联系的桥梁,通信的载体,负责应用程序中数据的传递。

  • 不带数据跳转
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);

此段代码实现从当前Activity,跳转到MainActivity,不带任何参数。

  • 带数据或多个数据跳转
    //带数据跳转
String data = "是我主活动调用了你";
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("ext", data);
startActivity(intent);
  • 带数据跳转,带数据返回
Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
Bundle bundle = new Bundle() ;
bundle.putString("register","请开始注册!");
intent.putExtras(bundle) ;
startActivityForResult(intent,1);

在跳转时使用startActivityForResult方法,此方法传入两个参数一个是Intent ,另外一个是给当前请求设置的一个请求ID,此ID在结束数据时辨识是否使当前请求的返回结果。

同时要在跳转源Activity中实现 onActivityResult 方法来接收处理目的Activity返回的数据。

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 1:
            if (resultCode == RESULT_OK) {
                final EditText loginUsername = findViewById(R.id.username);
                String returnUsername = data.getStringExtra("userName");
                //序列化方式取出实体
                User user = (User)data.getSerializableExtra("user");
                loginUsername.setText(returnUsername);
                loginUsername.setSelection(returnUsername.length());
            }
            break;
        default:
    }
}
  • Intent隐式实现启动其他程序Activity
    在当前Activity中调用百度请求界面。
Uri uri = Uri.parse("https://www.baidu.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

直接调用打电话功能

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:13207690000"));
startActivity(i);

在使用系统电话功能时需要给当前应用程序授权,在AndroidManifest.xml文件中增加
<uses-permission android:name="android.permission.CALL_PHONE" />
打开地图界面

Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

(二)Service

  1. Service简单介绍
  • Service它可以在后台执行长时间运行操作而没有用户界面的应用组件,不依赖任何用户界面,例如后台播放音乐,后台下载文件等。
  • 虽然服务是在后台运行的,但是Service和Activity都是运行在当前APP所在的main thread(UI主线程)中的,而耗时操作(如网络请求、拷贝数据、大文件)会阻塞主线程,给用户带来不好的体验。如果需要在服务中进行耗时操作,可以选择 IntentService,IntentService是Service的子类,用来处理异步请求。
  1. Service启动的两种方式
  • startService()
    在Acitivity界面通过显式意图(或隐式意图)的方式来启动服务和关闭服务。
Intent intentService = new Intent(MainActivity.this, AudioServiceOnBind.class);
startService(intentService);

生命周期顺序:onCreate->onStartCommand->onDestroy

onCreate() 当Service第一次被创建时调用。

onStartCommand() 当startService方法启动Service时,该方法被调用。

onDestroy() 当Service不再使用时调用。

  • bindService()绑定服务
    当应用组件通过调用 bindService() 绑定到服务时,服务即处于“绑定”状态。绑定服务提供了一个客户端-服务器接口,允许组件与服务进行交互、发送请求、获取结果。 仅当与另一个应用组件绑定时,绑定服务才会运行。 多个组件可以同时绑定到该服务,但全部取消绑定后,该服务即会被销毁。

首先在我们的服务中创建一个内部类AudioBinder继承Binder来获取当服务实例,然后在onBind方法中返回当前服务的实例。

public class AudioServiceOnBind extends Service implements MediaPlayer.OnCompletionListener{
 
    private final IBinder binder = new AudioBinder();
    //用于播放音乐等媒体资源
    private MediaPlayer mediaPlayer;
    public AudioServiceOnBind() {
        super();
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onStartCommand()");
        return 0;
    }
 
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
 
    @Override
    public void onCreate(){
        super.onCreate();
        Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onCreate()");
        if (mediaPlayer==null){
            mediaPlayer=MediaPlayer.create(this,R.raw.gumeng);
            mediaPlayer.setOnCompletionListener(this);
        }
        mediaPlayer.start();
    }
 
    @Override
    public void onCompletion(MediaPlayer mp) {
        stopSelf();
    }
 
    @Override
    public void onDestroy(){
        if(mediaPlayer.isPlaying()){
            mediaPlayer.stop();
        }
        mediaPlayer.release();
        stopForeground(true);
 
        Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onDestroy()");
    }
 
    //为了和Activity交互,我们需要定义一个Binder对象
    class AudioBinder extends Binder {
 
        //返回Service对象
        AudioServiceOnBind getService(){
            return AudioServiceOnBind.this;
        }
    }
 
}

然后在调用的Activity中创建一个ServiceConnection对象重写其中的onServiceConnected方法获取所要绑定的服务。在触发事件的方法中调用bindService实现服务的最终绑定。

public class MainActivity extends AppCompatActivity {
 
    private AudioServiceOnBind audioServiceOnBind;
 
    //使用ServiceConnection来监听Service状态的变化
    private ServiceConnection conn = new ServiceConnection() {
 
        @Override
        public void onServiceDisconnected(ComponentName name) {
            audioServiceOnBind = null;
        }
 
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            //这里我们实例化audioService,通过binder来实现
            audioServiceOnBind = ((AudioServiceOnBind.AudioBinder) binder).getService();
        }
    };
 
  public void click_music_open(View view) {
 
        Intent intentService = new Intent(MainActivity.this, AudioServiceOnBind.class);
        bindService(intentService, conn, Context.BIND_AUTO_CREATE);
 
    }
}

生命周期:onCreate->onBind->onUnBind->onDestroy

  • onCreate() 当Service被创建时,由系统调用。

  • onBind() 当bindService方法启动Service时,该方法被调用。

  • onUnbind() 当unbindService方法解除绑定时,该方法被调用。

  • onDestroy() 当Service不再使用时,由系统调用。

  1. 实现前台Service
    Android 8.0 后系统不允许后台应用创建后台服务。 因此,Android 8.0 引入了一种全新的方法,即 Context.startForegroundService(),以在前台启动新服务。

在service onStartCommand方法中增加以下代码

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(AudioServiceOnBind.this.getClass().getName(),"执行onStartCommand()");
        Intent nfIntent = new Intent(this, MainActivity.class);
 
        Notification.Builder builder = new Notification.Builder(this.getApplicationContext())
                .setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0))
                .setSmallIcon(R.mipmap.touxiang)
                .setContentTitle("wu")
                .setContentText("Android测试")
                .setWhen(System.currentTimeMillis());
 
        String CHANNEL_ONE_ID = "com.wu";
        String CHANNEL_ONE_NAME = "Channel One";
        //安卓8.1以上系统
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_MIN);
        notificationChannel.enableLights(false);
        notificationChannel.setShowBadge(true);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.createNotificationChannel(notificationChannel);
        builder.setChannelId(CHANNEL_ONE_ID);
 
        Notification notification = builder.build();
        startForeground(1, notification);
        return super.onStartCommand(intent,flags,startId);
    }
        在调用Activity中调用startForegroundService方法。

 public void click_music_open(View view) {
 
        Intent intentService = new Intent(MainActivity.this, AudioServiceOnBind.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intentService);
        } else {
            startService(intentService);
        }
    }
  1. 使用IntentService
    由于Service中的逻辑都是执行在主线程中的,此时如果在Service中处理一下耗时操作可能就会给用户带来不好的体验。所以此时就可以使用Android多线程Service.
    本质上IntentService是在Service里开启线程去做任务处理。IntentService会在任务执行完成后自行结束自己,而不需要外部去调用stopService了。

使用IntentService定义的服务,要开启线程,只要重写一个onHandleIntent()方法就可以了,而且在运行完之后会自动停止。

例如数据下载 数据上传等。

public class MyIntentService extends IntentService {
 
    private static final String TAG = "MyIntentService";
 
    public MyIntentService() {
        super("MyIntentService");
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d(TAG, "当前是子线程: " + Thread.currentThread().getId());
        //在这里实现异步多线程处理逻辑
        //例如数据下载 数据上传等。
        Log.d(TAG, "我在后台默默下载数据中。。。。");
    }
 
    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: ");
        super.onCreate();
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }
 
    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy: ");
        super.onDestroy();
    }
}

(三)Broadcast Receiver

(四)Content Provider

三、两大视图

  • project
  • android

在这里插入图片描述

四、主要结构目录

  • bulid
    编译时自生成文件
    在这里插入图片描述

  • libs
    第三方jar包,会被自动添加到构建路径中去
    在这里插入图片描述

  • java
    java代码

在这里插入图片描述

  • res
    项目中使用的所有资源,drawable存放图片,layout存放布局,values存放字符,mipmap存放图标
    在这里插入图片描述

  • Manifest
    AndroidManifest.xml配置文件,程序中定义的四大组件都需要在这个文件里注册,另外这个文件中还可以给应用程序添加权限声明
    在这里插入图片描述

  • bulid.gradle
    项目构建工具,通常有两个bulid.gradle,一个是项目级一个是app级

在这里插入图片描述

  • com.android.application
    应用程序模块

  • com.android.library
    库程序模块

区别:一个是可以直接运行的,一个只能作为代码库依附于别的应用程序模块来运行

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值