1.LifeCycle组件
LifeCycle组件可以让我们自定义的类能主动感知到Activity、Fragment、Application、Service等系统组件的生命周期。
我们以百度定位为例,我们一般在Activity生命周期的onCreate回调函数里调用自定义类LocationService的Start()方法,在onStop回调里调用LocationService的stop()方法来启动和停止定位服务。
利用LifeCycle让LocationService作为观察者,能感知到Activity的生命周期,当activity执行到onCreate与onStop时,LocationService自动调用它内部的start()与stop()方法来启动与停止定位,而无需再在activity里写调用LocationService.start()与LocationService.stop的代码了。当然这里的被观察者自然就是activty了。
2.使用LifeCycle解耦Activity与自定义组件
2.1关键步骤
step1.自定义类(自定义组件):
(1)让自定义类(或者称作自定义组件)实现LifecycleObserver。
(2)在要执行的函数start()和stop()上面添加注解@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)与@OnLifecycleEvent(Lifecycle.Event.ON_STOP)。
2.2示例代码:
自定义类LocationService.java
package com.gaoting.lifecycleactivity;
import android.util.Log;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
/**
* 封装了定位的开启和停止
*/
public class LocationService implements LifecycleObserver {
String TAG = "LocationService";
/**
* 开始定位
*/
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void start() {
Log.i(TAG, "开始定位");
}
/**
* 停止定位
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void stop() {
Log.i(TAG, "停止定位");
}
}
MainActivity.java
package com.gaoting.lifecycleactivity;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
LocationService mLocationService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationService = new LocationService();
getLifecycle().addObserver(mLocationService);
}
}
3.使用LifeCycle解耦Service与自定义组件
3.1关键步骤
添加依赖:
implementation libs.lifecycle.extensions
step1:Service处理
(1)让Service继承于LifecycleService
(2)getLifecycle().addObserve()将观察者与被观察者绑定,即将自定义类与Service绑定。
step2:自定义类的处理
(1)实现LifecycleObserver接口。
(2)添加标签注解事件,感知Service的生命周期。
3.2示例代码
实现Service类MyService.java。
package com.gaoting.lifecycleactivity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleService;
public class MyService extends LifecycleService {
String TAG = "MyService";
LocationService mlocationService;
public MyService() {
mlocationService = new LocationService();
getLifecycle().addObserver(mlocationService);
}
@Override
public void onCreate() {
Log.i(TAG, "onCreate");
super.onCreate();
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
}
自定义类LocationService.java。
package com.gaoting.lifecycleactivity;
import android.util.Log;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
/**
* 封装了定位的开启和停止
*/
public class LocationService implements LifecycleObserver {
String TAG = "LocationService";
/**
* 开始定位
*/
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void start() {
Log.i(TAG, "ON_CREATE开始定位");
}
/**
* 停止定位
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void stop() {
Log.i(TAG, "ON_STOP停止定位");
}
/**
* 停止定位:用于感知Service的onDestroy生命周期
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void stopLoc() {
Log.i(TAG, "ON_DESTROY停止定位");
}
}
MainActivity类。
package com.gaoting.lifecycleactivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;
import com.gaoting.lifecycleactivity.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
String TAG = "MainActivity";
ActivityMainBinding activityMainBinding;
LocationService mLocationService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mLocationService = new LocationService();
getLifecycle().addObserver(mLocationService);
/**
* 启动服务
*/
activityMainBinding.btStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MyService.class);
startService(i);
}
});
/**
* 停止服务
*/
activityMainBinding.btStopService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MyService.class);
stopService(i);
}
});
}
@Override
protected void onStop() {
Log.i(TAG, "onStop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
}
4.使用ProcessLifecycleOwner感知Application。
4.1关键步骤
添加依赖:implementation libs.lifecycle.extensions。
step1:MyApplication处理。
(1)继承Application。
(2)ProcessLifecycleOwner.getLifecycle().addObserver() 将观察者与被观察者绑定,即将自定义类与MyApplication绑定。
step2.自定义类的处理
(1)实现LIfecycleObserver接口。
(2)添加标签注解事件,感知Application的生命周期。
4.2示例代码
MyApplication.java。
package com.gaoting.processlifecycleapplication;
import android.app.Application;
import androidx.lifecycle.ProcessLifecycleOwner;
public class MyApplication extends Application {
ApplicationObserver mApplicationObserver;
@Override
public void onCreate() {
mApplicationObserver = new ApplicationObserver();
ProcessLifecycleOwner.get().getLifecycle().addObserver(mApplicationObserver);
super.onCreate();
}
}
观察者ApplicationObserver.java。
package com.gaoting.processlifecycleapplication;
import android.util.Log;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
public class ApplicationObserver implements LifecycleObserver {
String TAG = "ApplicationObserver";
/**
* 整个应用程序只感知1次
*/
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreate(){
Log.i(TAG,"ApplicationObserver.onCreate()应用程序启动");
}
/**
* 应用程序打到前台
*/
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart(){
Log.i(TAG,"ApplicationObserver.onStart()应用程序在前台");
}
/**
* 应用程序推到后台
*/
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume(){
Log.i(TAG,"ApplicationObserver.onResume()应用程序在前台");
}
/**
* 应用程序推到后台
*/
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause(){
Log.i(TAG,"ApplicationObserver.onPause()应用程序退到后台");
}
/**
* 应用程序销毁,感知不到
*/
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy(){
Log.i(TAG,"ApplicationObserver.onStop()应用程序销毁");
}
}
AndroidManifest.xml中要增加一行:
android:name=".MyApplication"。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ProcessLifecycleApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
测试,把应用启动,转入后台、回到前台等,然后观察Logcat打印日志。
标签:Log,androidx,Jetpack,void,LifeCycle,TAG,组件,import,public From: https://blog.csdn.net/huaqianzkh/article/details/139413777