2011-05-12 85 views
0

在這裏的朋友創建。在我銷燬服務在android仍然方法調用之後,我在服務中創建方法。我想知道如何停止呼叫方法,我把服務

這裏是我的代碼

package com.servicelistener; 

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 
import java.util.Timer; 

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
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.os.IBinder; 
import android.widget.Toast; 

import com.google.android.maps.GeoPoint; 

public class SimpleService extends Service { 
    Timer mytimer; 
    private String provider; 
    private LocationManager locationManager; 
    private LocationListener locationListener; 

//LocationManager locationManager; 
//LocationListener mlocListener; 
//private Handler toasthandler = new Handler(); 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() { 
      super.onCreate(); 
      Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show(); 
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  

      locationListener = new GPSLocationListener(); 
      Criteria criteria = new Criteria(); 
      //criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
      //criteria.setAccuracy(Criteria.ACCURACY_FINE); 
      provider = locationManager.getBestProvider(criteria, true); 

      locationManager.requestLocationUpdates(provider, 60000, 0, locationListener); 


    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(this, "Service destroy", Toast.LENGTH_LONG).show(); 
     //mytimer.cancel(); 
    } 
    private class GPSLocationListener implements LocationListener 
    { 
     @Override 
     public void onLocationChanged(Location location) { 
      if (location != null) { 
       GeoPoint point = new GeoPoint(
         (int) (location.getLatitude() * 1E6), 
         (int) (location.getLongitude() * 1E6)); 

       Toast.makeText(getBaseContext(), 
         "Latitude: " + location.getLatitude() + 
         " Longitude: " + location.getLongitude(), 
         Toast.LENGTH_SHORT).show(); 

       //Toast.makeText(getBaseContext(),"hi", 8000).show(); 

       String address = ConvertPointToLocation(point); 
       Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show(); 


      } 
     } 

     public String ConvertPointToLocation(GeoPoint point) { 
      String address = ""; 
      Geocoder geoCoder = new Geocoder(
        getBaseContext(), Locale.getDefault()); 
      try { 
       List<Address> addresses = geoCoder.getFromLocation(
        point.getLatitudeE6()/1E6, 
        point.getLongitudeE6()/1E6, 1); 

       if (addresses.size() > 0) { 
        for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++) 
         address += addresses.get(0).getAddressLine(index) + " "; 
       } 
      } 
      catch (IOException e) {     
       e.printStackTrace(); 
      } 

      return address; 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    } 
} 

銷燬服務還是從應用程序獲得位置之後。並提出了創建服務方法的應用方法。我想停止調用這個

回答

0

看來你需要註銷你的位置更新偵聽器。

考慮以下

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    locationManager.removeUpdates(locationListener); 
    Toast.makeText(this, "Service destroy", Toast.LENGTH_LONG).show(); 
    //mytimer.cancel(); 
} 
+0

感謝名單了很多.... – 2011-05-12 11:00:15

+0

是它按預期工作?它解決了你的問題? – Olegas 2011-05-12 11:25:12

+0

亞..但現在我有另一個問題,在這...你可以幫助我..問題就像我想使用requestlocationupdates方法loction。現在我只想在一些特定的間隔之後纔想要塗抹。當我從一個地方移動到另一個地方時,我也會獲得地點。我經過一段定義的時間間隔後, – 2011-05-12 11:32:07