我想知道如何停止调用我在oncreate of service中的方法

时间:2011-05-12 09:55:30

标签: android

这里有朋友。在我在Android静态方法调用中销毁服务后,我在服务中放入了oncreate方法。

这是我的代码

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) {
        }
    }
}

销毁服务后仍然从应用程序获取位置。并获得我在服务中创建的方法的applciaiton方法。我想停止调用这个

1 个答案:

答案 0 :(得分:0)

您似乎需要取消注册位置更新侦听器。

考虑以下

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