我制作了一个应用程序,它可以获取用户的位置并显示有关位置的信息。但问题是它并没有停止显示位置,而且还在同一位置不断更新我做了一个Toast,一旦应用程序运行,Toast也不会从屏幕上消失。这就像这个位置在运行一个无限循环或其他什么,但我就是搞不清楚。
package com.example.mymap;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
mMap.clear();
LatLng Helsinki = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(Helsinki).title("Here!").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(Helsinki));
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> addressList = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addressList != null && addressList.size() > 0) {
String address = "";
if (addressList.get(0).getThoroughfare() != null) {
address += addressList.get(0).getThoroughfare() + " ";
}
if (addressList.get(0).getLocality() != null) {
address += addressList.get(0).getLocality() + " ";
}
if (addressList.get(0).getPostalCode() != null) {
address += addressList.get(0).getPostalCode() + " ";
}
if (addressList.get(0).getAdminArea() != null) {
address += addressList.get(0).getAdminArea();
}
Toast.makeText(MapsActivity.this, address, Toast.LENGTH_SHORT).show();
Log.i("Address", address);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
if (Build.VERSION.SDK_INT <= 23) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mMap.clear();
LatLng Helsinki = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(Helsinki).title("Here!").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(Helsinki));
}
}
}
}
调试详细信息,但调试器被手动停止,因为它正在无限更新同一位置
I/zygote: Do partial code cache collection, code=29KB, data=27KB
I/zygote: After code cache collection, code=29KB, data=28KB
I/zygote: Increasing code cache capacity to 128KB
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/OpenGLRenderer: HWUI GL Pipeline
I/zygote: Do partial code cache collection, code=61KB, data=44KB
I/zygote: After code cache collection, code=61KB, data=44KB
Increasing code cache capacity to 256KB
D/: HostConnection::get() New Host Connection established 0xa3fb3f80, tid 8221
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED,
retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xa6b870a0: maj 2 min 0 rcv 2
I/Address: Avenue de Suffren Paris 75007 Île-de-France
D/EGL_emulation: eglMakeCurrent: 0xa6b870a0: ver 2 0 (tinfo 0xa6b83900)
D/: HostConnection::get() New Host Connection established 0xa405d6c0, tid 8218
D/EGL_emulation: eglMakeCurrent: 0xa6b870a0: ver 2 0 (tinfo 0xa6b83900)
D/EGL_emulation: eglCreateContext: 0x91663780: maj 1 min 0 rcv 1
D/EGL_emulation: eglMakeCurrent: 0x91663780: ver 1 0 (tinfo 0x9111b310)
D/EGL_emulation: eglMakeCurrent: 0xa6b870a0: ver 2 0 (tinfo 0xa6b83900)
I/Address: Avenue de Suffren Paris 75007 Île-de-France
I/Address: Avenue de Suffren Paris 75007 Île-de-France
D/EGL_emulation: eglMakeCurrent: 0xa6b870a0: ver 2 0 (tinfo 0xa6b83900)
I/zygote: Do full code cache collection, code=123KB, data=120KB
After code cache collection, code=102KB, data=81KB
I/Address: Avenue de Suffren Paris 75007 Île-de-France
I/Address: Avenue de Suffren Paris 75007 Île-de-France
W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:6
Selected remote version of com.google.android.gms.googlecertificates, version >= 6
W/zygote: Skipping duplicate class check due to unrecognized classloader
D/EGL_emulation: eglMakeCurrent: 0xa6b870a0: ver 2 0 (tinfo 0xa6b83900)
I/Address: Avenue de Suffren Paris 75007 Île-de-France
I/Address: Avenue de Suffren Paris 75007 Île-de-France
D/EGL_emulation: eglMakeCurrent: 0xa6b870a0: ver 2 0 (tinfo 0xa6b83900)
I/zygote: Do partial code cache collection, code=122KB, data=97KB
I/Address: Avenue de Suffren Paris 75007 Île-de-France
I/zygote: After code cache collection, code=122KB, data=97KB
Increasing code cache capacity to 512KB
I/Address: Avenue de Suffren Paris 75007 Île-de-France
if (Build.VERSION.SDK_INT <= 23) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 100, locationListener);
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 100, locationListener);
Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mMap.clear();
LatLng Helsinki = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(Helsinki).title("Here!").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(Helsinki));
}
}
}
以上代码的值发生了变化只需将更新时间从0分钟和0米更改为5分钟和100米。它将解决一切问题。
代码本身的单行示例:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 200, locationListener);