gpt4 book ai didi

java - 如何在应用程序关闭时禁用 Android LocationListener

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:24 24 4
gpt4 key购买 nike

我有一个名为 MyLocationManager.java 的处理位置服务的类

这是代码:

package com.syariati.childzone;

import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MyLocationManager {

private Context context;
private double myLat = 0.0;
private double myLon = 0.0;
private LocationManager locationManager = null;
private Location location = null;
private Criteria criteria;
private String locationName = null;

public MyLocationManager(Context ctx) {
this.context = ctx;
}

private String setCriteria() {
this.criteria = new Criteria();
this.criteria.setAccuracy(Criteria.ACCURACY_FINE);
this.criteria.setAltitudeRequired(false);
this.criteria.setBearingRequired(false);
this.criteria.setCostAllowed(true);
this.criteria.setPowerRequirement(Criteria.POWER_LOW);
return locationManager.getBestProvider(criteria, true);
}

public double getMyLatitude() {
return this.myLat;
}

public double getMyLongitude() {
return this.myLon;
}

public String getLocation() {
return this.locationName;
}

public void onLocationUpdate() {
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
String provider = setCriteria();

location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 1000, 0,
new MyLocationListener());

}

private void updateWithNewLocation(Location location) {
if (location != null) {
this.myLat = location.getLatitude();
this.myLon = location.getLongitude();
// Toast.makeText(this.context,
// "Lokasi Anda:\n" + this.myLat + "\n" + this.myLon,
// Toast.LENGTH_LONG).show();
getLocationName(this.myLat, this.myLon);
} else {
Toast.makeText(this.context, "Lokasi Anda Tidak Diketahui",
Toast.LENGTH_SHORT).show();
}
}

private void getLocationName(double lat, double lon) {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> adresses = geocoder.getFromLocation(lat, lon, 1);
StringBuilder sb = new StringBuilder();
if (adresses.size() > 0) {
Address address = adresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getCountryName()).append("\n");
}
this.locationName = sb.toString();
// Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}

private class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location newLocation) {
// TODO Auto-generated method stub
myLat = newLocation.getLatitude();
myLon = newLocation.getLongitude();
getLocationName(myLat, myLon);
Toast.makeText(context,
"Lokasi terupdate\nLat: " + myLat + "\nLon: " + myLon,
Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}

}

}

它有一个在其上实现位置监听器的内部类。到目前为止,它有效。它可以毫无问题地收听位置。但是,当我退出我的应用程序时,它并没有完全停止监听位置。正如您从这段代码中看到的:

@Override
public void onLocationChanged(Location newLocation) {
// TODO Auto-generated method stub
myLat = newLocation.getLatitude();
myLon = newLocation.getLongitude();
getLocationName(myLat, myLon);
Toast.makeText(context,
"Lokasi terupdate\nLat: " + myLat + "\nLon: " + myLon,
Toast.LENGTH_SHORT).show();
}

位置更新时会显示toast,位置更新时仍然会出现。即使我关闭了应用程序。

在我的案例中如何完全停止位置监听器。

这个帮助我停止了更新:

android how to stop gps

最佳答案

您可以在 LocationManager 上调用 removeUpdates。

public void removeUpdates (PendingIntent intent)

Removes any current registration for location updates of the currentactivity with the given PendingIntent. Following this call, updateswill no longer occur for this intent.

Parameters:

intent {#link PendingIntent} object that no longer needs locationupdates

Throws:

IllegalArgumentException if intent is null

参见此处:http://developer.android.com/reference/android/location/LocationManager.html#removeUpdates(android.app.PendingIntent)

更新:

在这种情况下,您可以更改此行:

locationManager.requestLocationUpdates(provider, 1000, 0,
new MyLocationListener());

到:

MyLocationListener myLL = new MyLocationListener();
locationManager.requestLocationUpdates(provider, 1000, 0,
myLL);

当你想删除你的监听器调用时,如下所示:

locationManager.removeUpdates(myLL);

关于java - 如何在应用程序关闭时禁用 Android LocationListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com