2012-03-19 33 views
7

Tôi có một lớp học mà xử lý một dịch vụ vị trí gọi là MyLocationManager.javaLàm thế nào để Vô hiệu hoá Android LocationListener khi ứng dụng được đóng

này là mã:

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 
     } 

    } 

} 

trong đó có một lớp Inner mà thực hiện vị trí người nghe trên đó. Cho đến nay, nó hoạt động. Nó lắng nghe vị trí mà không có vấn đề gì. Tuy nhiên, khi tôi thoát khỏi ứng dụng của mình, nó không hoàn toàn dừng việc lắng nghe vị trí đó. Như bạn thấy từ mã này:

@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(); 
      } 

trong khi vị trí được cập nhật, bánh mì nướng sẽ được hiển thị và nó vẫn xuất hiện khi vị trí được cập nhật. Ngay cả khi tôi đã đóng ứng dụng.

Cách dừng trình nghe vị trí hoàn toàn trong trường hợp của tôi.

một này đã giúp tôi để ngăn chặn sự updat:

android how to stop gps

Trả lời

15

Bạn có thể gọi removeUpdates trên LocationManager của bạn.

trống removeUpdates công cộng (PendingIntent mục đích)

Loại bỏ bất kỳ đăng ký hiện tại để cập nhật vị trí của hoạt động hiện tại với PendingIntent nhất định. Theo sau cuộc gọi này, cập nhật sẽ không còn xảy ra cho mục đích này nữa.

Tham số:

ý định {#link PendingIntent} đối tượng đó không còn cần vị trí cập nhật

Ném:

IllegalArgumentException nếu ý định là null

Xem ở đây: http://developer.android.com/reference/android/location/LocationManager.html#removeUpdates(android.app.PendingIntent)

Cập nhật:

Trong trường hợp này, bạn có thể thay đổi dòng này:

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

tới:

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

Khi bạn muốn loại bỏ cuộc gọi nghe của bạn như sau:

locationManager.removeUpdates(myLL); 
+0

bạn có ví dụ nào không? – farissyariati

+0

Tôi đã cập nhật câu trả lời của mình. – PhatHV

+0

Cảm ơn bạn. Nó giải quyết vấn đề của tôi :) – farissyariati

Các vấn đề liên quan