使用lifecycle解耦页面和组件
对于一个需要在activity生命周期的某个阶段执行响应操作的组件,可以让其实现LifecycleObserver接口,并通过注解的方式,在响应的阶段进行操作:
public class MyChronometer extends Chronometer implements LifecycleObserver {
private long elapsedTime;
public MyChronometer(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
private void startMeter(){
setBase(SystemClock.elapsedRealtime() - elapsedTime);
start();
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
private void stopMeter(){
elapsedTime = SystemClock.elapsedRealtime()-getBase();
stop();
}
}
并在activity中注册:
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
getLifecycle().addObserver(binding.chronometer);
}
}
使用LifecycleService解耦Service与组件
执行的操作放在一个实现了LifecycleObserver接口的观察者中:
public class MyLocationObserver implements LifecycleObserver {
private Context context;
private MyLocationListener listener;
private LocationManager locationManager;
private static String TAG="shopkeepertag";
public MyLocationObserver(Context context) {
this.context = context;
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
private void startGetLocation() {
Log.d(TAG, "startGetLocation: ");
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 1, listener);
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private void stopGetLocation(){
Log.d(TAG, "stopGetLocation: ");
locationManager.removeUpdates(listener);
}
static class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(@NonNull Location location) {
Log.d(TAG, "onLocationChanged: "+location.toString());
}
}
}
service要继承自LifecycleService,并注册一个观察者:
public class MyLocationService extends LifecycleService {
public MyLocationService() {
Log.d("sk", "MyLocationService: ");
MyLocationObserver observer=new MyLocationObserver(this);
getLifecycle().addObserver(observer);
}
}
在activity中启动或者关闭服务时,观察者类就能自动执行操作。
使用ProcessLifecycleOwner监听应用程序生命周期
在Application的不同生命周期要执行的操作用注解的方式放在一个实现了LifecycleObserver接口的观察者中:
public class ApplicationObserver implements LifecycleObserver {
private static String TAG="shopkeepertag";
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void onCreated(){
Log.d(TAG, "onCreated: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart(){
Log.d(TAG, "onStart: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void onResume(){
Log.d(TAG, "onResume: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void onPause(){
Log.d(TAG, "onPause: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop(){
Log.d(TAG, "onStop: ");
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy(){
Log.d(TAG, "onDestroy: ");
}
}
在application通过ProcessLifecycleOwner注册一个观察者:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle()
.addObserver(new ApplicationObserver());
}
}