首先,我是一个Android新手。现在已经寻找解决方案已有一段时间了,但是到目前为止,在正确的方向上找不到任何有用的提示。这通常是由问题本身的性质引起的,这是相当利基的。
以下工作代码基于https://codelabs.developers.google.com/codelabs/realtime-asset-tracking上的代码实验室。
不过,我想知道,由于现在在多个资源中都提到它,因此您将如何基于Android的工作管理器而不是带有持续通知的服务来执行类似的操作?
public class TrackerService extends Service {
@Override
public void onCreate() {
super.onCreate();
requestLocationUpdates();
}
private void requestLocationUpdates() {
LocationRequest request = new LocationRequest();
request.setInterval(5 * 60 * 1000);
request.setFastestInterval(30 * 1000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(this);
int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (permission == PackageManager.PERMISSION_GRANTED) {
client.requestLocationUpdates(request, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
Log.d(TAG, "location update " + location);
}
}
}, null); …Run Code Online (Sandbox Code Playgroud) android android-service android-fusedlocation google-location-services android-workmanager
android ×1