停止按钮不会停止我的位置服务

时间:2013-04-04 13:14:17

标签: android service gps intervals locationlistener

我正在尝试编写一个以间隔运行的服务。每次运行时,都应该检索手机的当前位置。就目前而言,我的间隔运行正常。但是一旦按下开始按钮,我就不能再使用停止按钮来停止它。我已经尝试过使用一个Thread,但那个只是让我犯错误。我也尝试了一个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();
}

}

2 个答案:

答案 0 :(得分:0)

你在Runnable中运行getLocation(),我猜你在runnable对象中需要一个新的处理程序,并且在onDestroy中向该Handler发送消息以停止位置监听。

在HandleMessage内的runnable中,您可以停止侦听位置。

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

答案 1 :(得分:0)

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