我无法理解为什么这个非常简单的活动会产生内存泄漏.
它遵循此处给出的指导原则:https://developer.android.com/training/location/receive-location-updates.html以及此处给出的示例代码:https://github.com/googlesamples/android-play-location /树/主/ LocationUpdates
我还注意到,如果我不重写LocationCallback类的onLocationResult方法,则会解决内存泄漏问题.但就这种方式而言,LocationCallback完全没用.
谢谢您的帮助!
public class TestLeaks extends Activity {
private FusedLocationProviderClient mFusedLocationClient; // Access to Fused Loc. Provider API
private LocationRequest mLocationRequest; // Stores parameters for requests to F.L.P.Api
private LocationCallback mLocationCallback; // Callback for Location events
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_leaks);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Location loc = locationResult.getLastLocation();
((TextView) findViewById(R.id.latitude)).setText(String.format(Locale.US, "%.6f", loc.getLatitude()));
((TextView) findViewById(R.id.longitude)).setText(String.format(Locale.US, …Run Code Online (Sandbox Code Playgroud) android memory-leaks google-api-client location-updates fusedlocationproviderclient
我正在尝试在后台运行位置跟踪器。为此,我读到我需要将endingIntent与fusedLocationProviderClient一起使用,以便我的应用程序将继续每x秒查询用户的位置,即使应用程序被最小化/关闭。
我的 mapActivity 在 kotlin 中。问题是,当我使用pendingIntent启动fusedLocationProviderClient时,我似乎无法触发任何位置更新(如果我使用回调在主线程中运行它,它会很好地工作,但是,这会减慢我的手机速度)。
有谁知道如何让 fusedLocationProviderClient 在我的情况下与未决意图一起工作?
我尝试使用intent.setAction() 中的字符串,使用我的包名称“com.russ.locationalarm”并在末尾附加“.action.PROCESS_UPDATES”。我对意图不熟悉,所以我不确定这是否是正确使用的字符串。我也尝试过将 PendingIntent.GetService() 切换为 PendingIntent.getBroadCast(),但似乎都不起作用。
这是我启动 fusedLocationProviderClient 的函数:
private fun startLocationUpdates() {
// Create the location request to start receiving updates
mLocationRequest!!.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
mLocationRequest!!.setInterval(INTERVAL)
mLocationRequest!!.setFastestInterval(FASTEST_INTERVAL)
// Create LocationSettingsRequest object using location request
val builder = LocationSettingsRequest.Builder()
builder.addLocationRequest(mLocationRequest!!)
val locationSettingsRequest = builder.build()
val settingsClient = LocationServices.getSettingsClient(this)
settingsClient.checkLocationSettings(locationSettingsRequest)
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
// new Google API SDK v11 uses getFusedLocationProviderClient(this)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { …Run Code Online (Sandbox Code Playgroud) android-intent android-pendingintent fusedlocationproviderapi location-updates fusedlocationproviderclient
我想获得用户位置,即使用户没有使用app.now我可以按下主页按钮后获得位置,应用程序进入后台状态,但经过几秒钟的位置更新后停止.当我针对stoped杀死应用程序位置更新时.这是我在app delegate中的代码.
let locationManager = CLLocationManager()
var LatitudeGPS = String()
var LongitudeGPS = String()
var speedGPS = String()
var Course = String()
var Altitude = String()
var bgtimer = Timer()
func applicationDidEnterBackground(_ application: UIApplication) {
self.doBackgroundTask()
}
func beginBackgroundUpdateTask() {
backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
self.endBackgroundUpdateTask()
})
}
func endBackgroundUpdateTask() {
UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
self.backgroundUpdateTask = UIBackgroundTaskInvalid
}
func doBackgroundTask() {
DispatchQueue.global(qos: .background).async {
self.beginBackgroundUpdateTask()
self.StartupdateLocation()
self.bgtimer = Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(self.bgtimer(timer:)), userInfo: nil, repeats: true)
RunLoop.current.add(self.bgtimer, forMode: RunLoopMode.defaultRunLoopMode)
RunLoop.current.run()
self.endBackgroundUpdateTask() …Run Code Online (Sandbox Code Playgroud)