Android 停止按钮不是';不要停止我的定位服务

Android 停止按钮不是';不要停止我的定位服务,android,service,gps,intervals,locationlistener,Android,Service,Gps,Intervals,Locationlistener,我正在尝试编写一个间隔运行的服务。每次运行时,它都应该检索手机的当前位置。就目前看来,我的间歇跑得很正常。但是一旦我按下开始按钮,我就不能再使用停止按钮来停止它了。我已经试过使用一个线程,但那个线程总是给我错误。我也尝试过使用while循环,但不知怎的,我的应用程序崩溃了。 无论如何,这是我现在的代码: public class Location extends Service { LocationManager locMan; LocationListener myLocListener;

我正在尝试编写一个间隔运行的服务。每次运行时,它都应该检索手机的当前位置。就目前看来,我的间歇跑得很正常。但是一旦我按下开始按钮,我就不能再使用停止按钮来停止它了。我已经试过使用一个线程,但那个线程总是给我错误。我也尝试过使用while循环,但不知怎的,我的应用程序崩溃了。 无论如何,这是我现在的代码:

public class Location extends Service {

LocationManager locMan;
LocationListener myLocListener;
int intervalTime = 1000 * 30;
int minTime = 0;
float minDistance = 0;

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

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            //check if GPS is on
            if (!locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                Toast.makeText(
                        getApplicationContext(),
                        "Failed to start Location service! Please turn you GPS on!",
                        Toast.LENGTH_LONG).show();
                stopSelf();
            } else if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                Toast.makeText(getApplicationContext(),
                        "Service Started",
                        Toast.LENGTH_SHORT).show();
                getLocation();
            }
        }

        private void getLocation() {
            // TODO Auto-generated method stub
            try {

                locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                //while GPS remains on, run this script.
                while (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

                    myLocListener = new LocationListener() {

                        @Override
                        public void onStatusChanged(String provider,
                                int status, Bundle extras) {
                            // TODO Auto-generated method stub

                        }

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

                        }

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

                        }

                        @Override
                        public void onLocationChanged(
                                android.location.Location location) {
                            // TODO Auto-generated method stub
                            String loc = "Latitude is: "
                                    + location.getLatitude()
                                    + "Longitude is: "
                                    + location.getLongitude();

                            Toast.makeText(getApplicationContext(),
                                    loc,
                                    Toast.LENGTH_SHORT).show();
                        }
                    };

                    // Specify criteria for a gps provider and get the provider
                    Criteria criteria = new Criteria();
                    criteria.setPowerRequirement(Criteria.POWER_LOW);
                    criteria.setAccuracy(Criteria.ACCURACY_FINE);
                    criteria.setAltitudeRequired(false);
                    criteria.setBearingRequired(false);
                    criteria.setCostAllowed(true);
                    criteria.setSpeedRequired(false);

                    String BestProvider = locMan.getBestProvider(criteria,
                            false);

                    locMan.requestLocationUpdates(BestProvider,
                            minTime,
                            minDistance,
                            myLocListener);

                    Toast.makeText(getApplicationContext(),
                            "Location Retrieved",
                            Toast.LENGTH_SHORT).show();

                    handler.postDelayed(this, intervalTime);
                }
                stopSelf();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    handler.postDelayed(runnable, intervalTime);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    if (myLocListener != null) {
        locMan.removeUpdates(myLocListener);
    }

    Toast.makeText(getApplicationContext(),
            "Service stopped",
            Toast.LENGTH_LONG).show();
    super.onDestroy();
}

}

您正在Runnable中运行getLocation(),我想您需要在Runnable对象中添加一个新的处理程序,并在onDestroy中向该处理程序发送消息以停止位置侦听

在runnable inside HandleMessage中,您可以停止侦听位置


使用库如何?它完全符合您的要求。

为什么不让服务运行,并告诉LocationManager仅在
间隔时间通知您。换句话说,移除
stopSelf()
并将
minTime
设置为
1000*30

否,即使它冻结。并且只回复主页按钮。因为它必须能够随时停止服务。运行时无需等待位置。