我有一个位置服务,我可以使我目前的位置吐司,但我不知道如何发送这些信息到我的Map在我的活动。
你能帮我实现吗?
我需要发送回的代码在这里:
@Override
public void onCreate() {
super.onCreate();
new Notification();
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) createNotificationChanel() ;
else startForeground(
1,
new Notification()
);
locationRequest = LocationRequest.create();
locationRequest.setInterval(3000);
locationRequest.setFastestInterval(3000);
locationRequest.setMaxWaitTime(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(@NonNull LocationResult locationResult) {
Location location = locationResult.getLastLocation();
Toast.makeText(getApplicationContext(),
"Lat: "+Double.toString(location.getLatitude()) + '\n' +
"Long: " + Double.toString(location.getLongitude()), Toast.LENGTH_LONG).show();
currentCoor = new LLcoor(location.getLongitude(), location.getLatitude());
//locationArrayList.add(new LatLng(location.getLatitude(), location.getLongitude()));
/* for (Location location : locationResult.getLocations()) {
location
}*/
}
};
startLocationUpdates();
}
我需要不断更新我在Map上的位置。我使用azureMaps
如果您需要完整的类代码,请访问:
public class LocationService extends Service {
public static ArrayList<LatLng> locationArrayList = new ArrayList<LatLng>();
FusedLocationProviderClient fusedLocationClient;
LocationRequest locationRequest;
LocationCallback locationCallback;
LLcoor currentCoor;
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
fusedLocationClient.requestLocationUpdates(locationRequest,
locationCallback,
Looper.getMainLooper());
}
protected void createLocationRequest() {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(3000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
public void onCreate() {
super.onCreate();
new Notification();
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) createNotificationChanel() ;
else startForeground(
1,
new Notification()
);
locationRequest = LocationRequest.create();
locationRequest.setInterval(3000);
locationRequest.setFastestInterval(3000);
locationRequest.setMaxWaitTime(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(@NonNull LocationResult locationResult) {
Location location = locationResult.getLastLocation();
Toast.makeText(getApplicationContext(),
"Lat: "+Double.toString(location.getLatitude()) + '\n' +
"Long: " + Double.toString(location.getLongitude()), Toast.LENGTH_LONG).show();
currentCoor = new LLcoor(location.getLongitude(), location.getLatitude());
//locationArrayList.add(new LatLng(location.getLatitude(), location.getLongitude()));
/* for (Location location : locationResult.getLocations()) {
location
}*/
}
};
startLocationUpdates();
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChanel() {
String notificationChannelId = "Location channel id";
String channelName = "Background Service";
NotificationChannel chan = new NotificationChannel(
notificationChannelId,
channelName,
NotificationManager.IMPORTANCE_NONE
);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, notificationChannelId);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("Location updates:")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
fusedLocationClient.removeLocationUpdates(locationCallback);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
1条答案
按热度按时间nmpmafwu1#
您可以在LocationService中创建静态回调或LiveData,并更新要在MainActivity.类中使用的特定值:
并在您的活动中添加以下内容: