我正在尝试构建一个使用某些包作为位置的应用程序(https://pub.dev/packages/location https://pub.dev/packages/location)和指南针(https://pub.dev/packages/flutter_compass https://pub.dev/packages/flutter_compass)并保持后台服务跟踪用户位置。
一切正常,直到我启动跟踪位置的服务。
当服务处于活动状态时,整个应用程序永远不会停止,例如,当我在没有服务的情况下关闭应用程序时,指南针也会停止,但随着服务运行,指南针也会继续运行。实际上它返回一个错误“尝试向 Flutter 发送平台消息,但 FlutterJNI 与本机 C++ 分离。无法发送。通道:hemanthraj/flutter_compass”。位置也会发生同样的情况:“尝试向 Flutter 发送平台消息,但 FlutterJNI 与本机 C++ 分离。无法发送。通道:lyokone/locationstream”。
在此之后,即使我再次打开该项目,它也不再工作了......
我正在尝试使服务完全独立于项目的其余部分。
我将向您展示服务实现(Android)
public class CompassApplication extends FlutterApplication {
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("messages", "Messages", NotificationManager.IMPORTANCE_LOW);
NotificationManager manager = getSystemService(NotificationManager.class);
if (manager != null) {
manager.createNotificationChannel(channel);
}
}
}
}
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
val intent = Intent(this, LocationService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
}
}
}
public class LocationService extends Service {
static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10 * 60 * 1000; // 10 minutes
static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
private LocationRequest mLocationRequest;
private FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;
@Override
public void onCreate() {
super.onCreate();
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
onNewLocation(locationResult.getLastLocation());
}
};
createLocationRequest();
getLastLocation();
requestLocationUpdates();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "messages")
.setSmallIcon(R.mipmap.ic_launcher_foreground);
startForeground(101, builder.build());
}
}
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void getLastLocation() {
try {
mFusedLocationClient.getLastLocation()
.addOnCompleteListener(task -> {
if (task.isSuccessful() && task.getResult() != null) {
onNewLocation(task.getResult());
}
});
} catch (SecurityException ignored) {}
}
public void requestLocationUpdates() {
Utils.setRequestingLocationUpdates(this, true);
startService(new Intent(getApplicationContext(), LocationUpdatesService.class));
try {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
} catch (SecurityException unlikely) {
Utils.setRequestingLocationUpdates(this, false);
}
}
private void onNewLocation(Location location) {
// TODO: deal with current location
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
另一个问题是,即使我不关闭应用程序,它也会消耗大量电池。
谢谢!