使用lifecycle解耦组件和service

使用lifecycle解耦组件和service,这里使用的是通过service获取位置信息,点击按钮获取,点击按钮结束获取。
Activity代码,开启service和结束service

public class ServiceLifecycleTest02 extends AppCompatActivity {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_lifecycle_test02);
    }

    public void startGps(View view) {
    
    
        startService(new Intent(this,MyService.class));
    }

    public void stopGps(View view) {
    
    
        stopService(new Intent(this,MyService.class));
    }
}

Service代码,这里我们需要继承lifecycleService,这个库需要我们导入依赖

androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

public class MyService extends LifecycleService {
    
    
    private static final String TAG ="路" ;
    MyServiceObserve observe;
    public MyService() {
    
    
        Log.d(TAG, "MyService:绑定service ");
        observe = new MyServiceObserve(this);
        //service的生命周期添加observe
        getLifecycle().addObserver(observe);
    }
}

这里的逻辑就是获取位置信息。有两个方法:startGetLocation()和startGetLocation()。

public class MyServiceObserve implements LifecycleObserver {
    
    
    private static final String TAG = "路";
    private Context context;
    LocationManager locationManager;
    MyLocationListener listener;
    public MyServiceObserve(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{
    
    

       private static final String TAG = "路";

       @Override
        public void onLocationChanged(@NonNull Location location) {
    
    
            Log.d(TAG, "onLocationChanged: 位置信息改变");
        }
    }
}

xml文件就是两个按钮:

<Button
        android:id="@+id/start"
        android:onClick="startGps"
        android:text="开始"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.371" />

    <Button
        android:id="@+id/stop"
        android:onClick="stopGps"
        android:text="停止"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.651" />

信息打印成功,实现了组件和service的解耦。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43867812/article/details/127903954