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

我有一个类来处理名为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

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

}

}

}

它有一个Inner类,可以在其上实现位置监听器.到目前为止,它的确有效.它听取位置没有问题.但是,当我退出我的应用程序时,它并没有完全停止监听位置.从这段代码中可以看出:

@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,并且在更新位置时仍会显示.即使我关闭了应用程序.

如何在我的情况下完全停止位置监听器.

这个帮我阻止了更新:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值