Use lifecycle to decouple components and services. Here, you use the service to obtain location information, click the button to obtain, and click the button to end the acquisition.
Activity code, start service and end 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 code, here we need to inherit lifecycleService, this library requires us to import dependencies
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);
}
}
The logic here is to obtain location information. There are two methods: startGetLocation() and 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: 位置信息改变");
}
}
}
The xml file is just two buttons:
<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" />
The information is printed successfully, realizing the decoupling of components and services.