• JetPack入门


     

    先导入依赖

       implementation("androidx.lifecycle:lifecycle-extensions:2.2.0")

    1.使用LifeCycle解耦页面与组件 

    Activity

    1. package com.tiger.lifecycle;
    2. import android.annotation.SuppressLint;
    3. import android.os.Bundle;
    4. import android.os.SystemClock;
    5. import android.widget.Chronometer;
    6. import androidx.appcompat.app.AppCompatActivity;
    7. public class Step2Activity extends AppCompatActivity {
    8. private MyChronometer chronometer;
    9. @Override
    10. protected void onCreate(Bundle savedInstanceState) {
    11. super.onCreate(savedInstanceState);
    12. setContentView(R.layout.activity_main);
    13. chronometer = findViewById(R.id.chronometer);
    14. getLifecycle().addObserver(chronometer);//给自定义组件添加 Activity生命周期的这个一个监听
    15. }
    16. }

    自定义组件

    1. package com.tiger.lifecycle;
    2. import android.content.Context;
    3. import android.os.SystemClock;
    4. import android.util.AttributeSet;
    5. import android.widget.Chronometer;
    6. import androidx.lifecycle.Lifecycle;
    7. import androidx.lifecycle.LifecycleObserver;
    8. import androidx.lifecycle.OnLifecycleEvent;
    9. public class MyChronometer extends Chronometer implements LifecycleObserver {
    10. private long elapsedTime=0;
    11. public MyChronometer(Context context, AttributeSet attrs) {
    12. super(context, attrs);
    13. }
    14. @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) //在 resume 生命周期执行
    15. private void startMeter(){
    16. setBase(SystemClock.elapsedRealtime()-elapsedTime);
    17. start();
    18. }
    19. @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)//在 PAUSE 生命周期执行
    20. private void stopMeter(){
    21. elapsedTime = SystemClock.elapsedRealtime() - getBase();
    22. stop();
    23. }
    24. }

    xml

    1. "1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".Step1Activity">
    8. <com.tiger.lifecycle.MyChronometer
    9. android:id="@+id/chronometer"
    10. android:layout_width="wrap_content"
    11. android:layout_height="wrap_content"
    12. android:text="Hello World!"
    13. android:textSize="30sp"
    14. app:layout_constraintBottom_toBottomOf="parent"
    15. app:layout_constraintLeft_toLeftOf="parent"
    16. app:layout_constraintRight_toRightOf="parent"
    17. app:layout_constraintTop_toTopOf="parent">
    18. com.tiger.lifecycle.MyChronometer>
    19. androidx.constraintlayout.widget.ConstraintLayout>

    2.使用LifeCycle解耦Service与组件

    activity

    1. package com.tiger.lifecycle;
    2. import android.content.Intent;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import androidx.appcompat.app.AppCompatActivity;
    6. public class Step3Activity extends AppCompatActivity {
    7. @Override
    8. protected void onCreate(Bundle savedInstanceState) {
    9. super.onCreate(savedInstanceState);
    10. setContentView(R.layout.activity_main_two);
    11. }
    12. public void startGps(View view) {
    13. startService(new Intent(this,MyLocationService.class));
    14. }
    15. public void stopGps(View view) {
    16. stopService(new Intent(this,MyLocationService.class));
    17. }
    18. }

    service 

    1. package com.tiger.lifecycle;
    2. import android.util.Log;
    3. import androidx.lifecycle.LifecycleService;
    4. public class MyLocationService extends LifecycleService {
    5. public MyLocationService() {
    6. Log.d("ning","MyLocationService init");
    7. MyLocationObserver myLocationObserver = new MyLocationObserver(this);
    8. getLifecycle().addObserver(myLocationObserver);
    9. }
    10. }

    observer

    1. package com.tiger.lifecycle;
    2. import android.content.Context;
    3. import android.content.pm.PackageManager;
    4. import android.location.Location;
    5. import android.location.LocationListener;
    6. import android.location.LocationManager;
    7. import android.os.Bundle;
    8. import android.util.Log;
    9. import androidx.annotation.NonNull;
    10. import androidx.core.app.ActivityCompat;
    11. import androidx.lifecycle.Lifecycle;
    12. import androidx.lifecycle.LifecycleObserver;
    13. import androidx.lifecycle.OnLifecycleEvent;
    14. import java.util.List;
    15. public class MyLocationObserver implements LifecycleObserver {
    16. private Context context;
    17. private LocationManager locationManager;
    18. private MyLocationListener myLocationListener;
    19. public MyLocationObserver(Context context) {
    20. this.context = context;
    21. }
    22. @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    23. private void startGetLocation() {
    24. Log.d("ning","startGetLocation");
    25. locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    26. myLocationListener = new MyLocationListener();
    27. if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    28. //不满足就退出方法
    29. return;
    30. }
    31. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 1, myLocationListener);//提供监听 GPS提供 ,每3秒获取一下信息,每拿手机移动一米获取一下信息
    32. }
    33. @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    34. private void stopGetLocation(){
    35. Log.d("ning","stopGetLocation");
    36. locationManager.removeUpdates(myLocationListener);
    37. }
    38. static class MyLocationListener implements LocationListener{
    39. @Override
    40. public void onLocationChanged(@NonNull Location location) {
    41. Log.d("ning","Location Changed "+location.toString());
    42. }
    43. @Override
    44. public void onLocationChanged(@NonNull List locations) {
    45. LocationListener.super.onLocationChanged(locations);
    46. }
    47. @Override
    48. public void onFlushComplete(int requestCode) {
    49. LocationListener.super.onFlushComplete(requestCode);
    50. }
    51. @Override
    52. public void onStatusChanged(String provider, int status, Bundle extras) {
    53. LocationListener.super.onStatusChanged(provider, status, extras);
    54. }
    55. @Override
    56. public void onProviderEnabled(@NonNull String provider) {
    57. LocationListener.super.onProviderEnabled(provider);
    58. }
    59. @Override
    60. public void onProviderDisabled(@NonNull String provider) {
    61. LocationListener.super.onProviderDisabled(provider);
    62. }
    63. }
    64. }

    3.使用ProcessLifecycleOwner监听应用程序生命周期

    1. package com.tiger.lifecycle;
    2. import android.util.Log;
    3. import androidx.lifecycle.Lifecycle;
    4. import androidx.lifecycle.LifecycleObserver;
    5. import androidx.lifecycle.OnLifecycleEvent;
    6. public class ApplicationObserver implements LifecycleObserver {
    7. private String TAG = "ning";
    8. @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    9. public void onCreate() {
    10. Log.d(TAG, "Lifecycle.Event.ON_CREATE");
    11. }
    12. @OnLifecycleEvent(Lifecycle.Event.ON_START)
    13. public void onStart() {
    14. Log.d(TAG, "Lifecycle.Event.ON_START");
    15. }
    16. @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    17. public void onResume() {
    18. Log.d(TAG, "Lifecycle.Event.ON_RESUME");
    19. }
    20. @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    21. public void onPause() {
    22. Log.d(TAG, "Lifecycle.Event.ON_PAUSE");
    23. }
    24. @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    25. public void onStop() {
    26. Log.d(TAG, "Lifecycle.Event.ON_STOP");
    27. }
    28. @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    29. public void onDestroy() {
    30. Log.d(TAG, "Lifecycle.Event.ON_DESTROY");
    31. }
    32. }

     MyApplication

    1. package com.tiger.lifecycle;
    2. import android.app.Application;
    3. import android.util.Log;
    4. import androidx.lifecycle.ProcessLifecycleOwner;
    5. public class MyApplication extends Application {
    6. @Override
    7. public void onCreate() {
    8. super.onCreate();
    9. ProcessLifecycleOwner.get().getLifecycle().addObserver(new ApplicationObserver());//只有一个会执行,多个Activity只执行一次
    10. Log.d("ning","MyApplication onCreate");
    11. }
    12. }

  • 相关阅读:
    图论-二分图
    go语言中结构体tag使用
    会计制度设计名词解释
    elasticsearch面试题
    生产者消费者模型
    「Spring Boot 系列」04. Spring Boot配置------获取数据
    Mysql存储过程大全
    axios详解以及完整封装方法
    一个diors的未来展望噜
    中小企业办公自动化系统设计与实现(SSH)
  • 原文地址:https://blog.csdn.net/qq_53374893/article/details/136560668