位置监听器工作原理是从服务,但不是IntentService监听器、是从、工作原理、位置

2023-09-05 04:23:37 作者:喵骚年

我有一个应用程序,其中我试图定期获取用户的位置,并发送至服务器。我有连接到报警管理器,它每分钟执行一次(测试)的服务。该服务正确地查找到用户的位置,并记录了GPS共同里。O。一旦有一个GPS锁我取消位置请求,并停止该服务。当我要求位置更新,我开始一个执行20秒后的处理,此处理删除位置更新和停止的情况下没有锁来达到的服务。所有这一切工作。

下面是code,工程使用的服务类。

 公共类TrackingService延伸服务{

    私有静态最后字符串变量= TrackingService.class.getSimpleName();
    LocationManager mlocManager;
    LocationListener的mlocListener;
    NfcScannerApplication nfcscannerapplication;
    字符串carerID;

    处理器endServiceHandler;
    可运行endServiceRunnable;

    @覆盖
    公共无效的onCreate(){
        super.onCreate();

        nfcscannerapplication =(NfcScannerApplication)getApplication();
        mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mlocListener =新MyLocationListener();

        Log.e(TAG,服务创建,位置管理器和监听器创建);

    }

    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
        Log.e(TAG,中的onDestroy在LocationService类);
        mlocManager.removeUpdates(mlocListener);


    }

    @覆盖
    公共无效ONSTART(意向意图,诠释startId){
        super.onStart(意向,startId);
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener);
        Log.e(TAG,请求位置更新);

        enableMenuButtonsHandler();
        endServiceHandler.postDelayed(endServiceRunnable,20 * 1000);




    }

    @覆盖
    公众的IBinder onBind(意向意图){
        返回null;
    }

    私有类MyLocationListener实现LocationListener的{

        @覆盖
        公共无效onLocationChanged(位置LOC){


            Log.e(TAG,在TrackingService onlocationChanged约送经度/纬度+ loc.getLongitude()++ loc.getLatitude());


            DateTime的DT =新的日期时间();
            DateTimeFormatter DF3 = DateTimeFormat.forPattern(YYYY-MM-DD H:MM:SS.SSS);
            字符串formattedNowTime3 = df3.print(DT);
            Log.e(TAG,+ formattedNowTime3在TrackingServive =位置锁定时间);


            光标C = nfcscannerapplication.loginValidate.queryAllFromCarer();

            如果(c.getCount()大于0){
                如果(c.moveToLast()){

                carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));

                }
            }

             Log.e(TAG,照顾者ID =+ carerID);

             mlocManager.removeUpdates(mlocListener);
             Log.e(TAG,删除更新(TrackingService));

             TrackingService.this.stopSelf();
             Log.e(TAG,呼吁stopSelf上TrackingService);

        }

        @覆盖
        公共无效onProviderDisabled(字符串提供商){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onProviderEnabled(字符串提供商){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
            // TODO自动生成方法存根

        }

    MyLocationListener的} //结束


    公共无效enableMenuButtonsHandler(){

        endServiceHandler =新的处理程序();
        endServiceRunnable =新的Runnable(){
            公共无效的run(){

                endService();

            }

            私人无效endService(){

                 mlocManager.removeUpdates(mlocListener);
                 Log.e(TAG,删除更新(TrackingService)从endService处理程序);

                 TrackingService.this.stopSelf();
                 Log.e(TAG,呼吁stopSelf上TrackingService从endService处理程序);

            }
        };

    }


服务} //结束
 

我遇到的问题是,一旦我有一个GPS定位,我想从长远和LAT共同ords发送到服务器。我知道我可以使用AsyncTask的从一个服务,但我不想这样做。我想preFER使用IntentService,因为它运行在自己的后台线程。这样我可以从IntentService进行网络直接调用。

下面code实现IntentService类,但它似乎并没有得到一个锁。全球定位系统不断在手机上无限期地闪烁。该日志语句得到尽可能'请求位置更新,之后再没有什么。

有没有人为什么没有锁来达到的任何想法?先谢谢了。

 公共类TrackingService扩展IntentService {

   私有静态最后字符串变量= TrackingService.class.getSimpleName();
   LocationManager mlocManager;
   LocationListener的mlocListener;
   NfcScannerApplication nfcscannerapplication;
   字符串carerID;

   处理器endServiceHandler;
   可运行endServiceRunnable;



    公共TrackingService(){
        超级(TrackingService);



    }

    @覆盖
    保护无效onHandleIntent(意向意图){

           nfcscannerapplication =(NfcScannerApplication)getApplication();
           mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
           mlocListener =新MyLocationListener();
           Log.e(TAG,服务创建,位置管理器和监听器创建);

           mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener);
           Log.e(TAG,请求位置更新);

           enableMenuButtonsHandler();
           endServiceHandler.postDelayed(endServiceRunnable,20 * 1000);

    }



     私有类MyLocationListener实现LocationListener的{

                 @覆盖
                 公共无效onLocationChanged(位置LOC){


                    Log.e(TAG,在TrackingService onlocationChanged约送经度/纬度+ loc.getLongitude()++ loc.getLatitude());


                     DateTime的DT =新的日期时间();
                     DateTimeFormatter DF3 = DateTimeFormat.forPattern(YYYY-MM-DD H:MM:SS.SSS);
                     字符串formattedNowTime3 = df3.print(DT);
                     Log.e(TAG,+ formattedNowTime3在TrackingServive =位置锁定时间);


                     光标C = nfcscannerapplication.loginValidate.queryAllFromCarer();

                    如果(c.getCount()大于0){
                        如果(c.moveToLast()){

                        carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));

                        }
                    }

                     Log.e(TAG,照顾者ID =+ carerID);

                     mlocManager.removeUpdates(mlocListener);
                     Log.e(TAG,删除更新(TrackingService));

                     TrackingService.this.stopSelf();
                     Log.e(TAG,呼吁stopSelf上TrackingService);

                 }

                 @覆盖
                 公共无效onProviderDisabled(字符串提供商){
                     // TODO自动生成方法存根

                 }

                 @覆盖
                 公共无效onProviderEnabled(字符串提供商){
                     // TODO自动生成方法存根

                 }

                 @覆盖
                 公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
                     // TODO自动生成方法存根

                 }

             MyLocationListener的} //结束


     公共无效enableMenuButtonsHandler(){

                endServiceHandler =新的处理程序();
                endServiceRunnable =新的Runnable(){
                    公共无效的run(){

                        endService();

                    }

                    私人无效endService(){

                         mlocManager.removeUpdates(mlocListener);
                         Log.e(TAG,删除更新(TrackingService)从endService处理程序);

                         TrackingService.this.stopSelf();
                         Log.e(TAG,呼吁stopSelf上TrackingService从endService处理程序);

                    }
                };

     }

trackingService的} //结束
 

 公共类TrackingService延伸服务{

    私有静态最后字符串变量= TrackingService.class.getSimpleName();
    LocationManager mlocManager;
    LocationListener的mlocListener;
    NfcScannerApplication nfcscannerapplication;
    字符串carerID;

    处理器endServiceHandler;
    可运行endServiceRunnable;

    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志,诠释startId){


        nfcscannerapplication =(NfcScannerApplication)getApplication();
        mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mlocListener =新MyLocationListener();

        Log.e(TAG,服务创建,位置管理器和监听器创建);

        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener);
        Log.e(TAG,请求位置更新);

        enableMenuButtonsHandler();
        endServiceHandler.postDelayed(endServiceRunnable,20 * 1000);

            返回super.onStartCommand(意向,标志,startId);
    }



    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
        Log.e(TAG,中的onDestroy在LocationService类);
        mlocManager.removeUpdates(mlocListener);


    }








    @覆盖
    公众的IBinder onBind(意向意图){
        返回null;
    }

    私有类MyLocationListener实现LocationListener的{

        @覆盖
        公共无效onLocationChanged(位置LOC){


            Log.e(TAG,在TrackingService onlocationChanged约送经度/纬度+ loc.getLongitude()++ loc.getLatitude());


            DateTime的DT =新的日期时间();
            DateTimeFormatter DF3 = DateTimeFormat.forPattern(YYYY-MM-DD H:MM:SS.SSS);
            字符串formattedNowTime3 = df3.print(DT);
            Log.e(TAG,+ formattedNowTime3在TrackingServive =位置锁定时间);


            光标C = nfcscannerapplication.loginValidate.queryAllFromCarer();

            如果(c.getCount()大于0){
                如果(c.moveToLast()){

                carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));

                }
            }

             Log.e(TAG,照顾者ID =+ carerID);

             mlocManager.removeUpdates(mlocListener);
             Log.e(TAG,删除更新(TrackingService));

             TrackingService.this.stopSelf();
             Log.e(TAG,呼吁stopSelf上TrackingService);

        }

        @覆盖
        公共无效onProviderDisabled(字符串提供商){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onProviderEnabled(字符串提供商){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
            // TODO自动生成方法存根

        }

    MyLocationListener的} //结束


    公共无效enableMenuButtonsHandler(){

        endServiceHandler =新的处理程序();
        endServiceRunnable =新的Runnable(){
            公共无效的run(){

                endService();

            }

            私人无效endService(){

                 mlocManager.removeUpdates(mlocListener);
                 Log.e(TAG,删除更新(TrackingService)从endService处理程序);

                 TrackingService.this.stopSelf();
                 Log.e(TAG,呼吁stopSelf上TrackingService从endService处理程序);

            }
        };

    }


服务} //结束
 
苏联历史上最牛的窃听器,如今这项技术我们每个人都在使用

  @覆盖
    公众诠释onStartCommand(意向意图,诠释标志,诠释startId){


        nfcscannerapplication =(NfcScannerApplication)getApplication();
        mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mlocListener =新MyLocationListener();
        Log.e(TAG,服务创建,位置管理器和监听器创建);


            HandlerThread handlerThread =新HandlerThread(MyHandlerThread);
            handlerThread.start();
            活套活套= handlerThread.getLooper();

            处理程序处理程序=新的处理程序(活套);

        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener,尺蠖);
        Log.e(TAG,请求位置更新);

        enableMenuButtonsHandler();
        endServiceHandler.postDelayed(endServiceRunnable,20 * 1000);

            返回super.onStartCommand(意向,标志,startId);
    }
 

 公共类TrackingService延伸服务{

    私有静态最后字符串变量= TrackingService.class.getSimpleName();
    LocationManager mlocManager;
    LocationListener的mlocListener;
    NfcScannerApplication nfcscannerapplication;
    字符串carerID;

    处理器endServiceHandler;
    可运行endServiceRunnable;
    HandlerThread handlerThread;
    活套活套;

    @覆盖
    公众诠释onStartCommand(意向意图,诠释标志,诠释startId){


        nfcscannerapplication =(NfcScannerApplication)getApplication();
        mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mlocListener =新MyLocationListener();
        Log.e(TAG,服务创建,位置管理器和监听器创建);


            Log.e(TAG,创造handlerthread和弯针);
            handlerThread =新HandlerThread(MyHandlerThread);
            handlerThread.start();
            尺蠖= handlerThread.getLooper();





        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener,尺蠖);
        Log.e(TAG,请求位置更新);

        enableMenuButtonsHandler();
        endServiceHandler.postDelayed(endServiceRunnable,20 * 1000);

            返回super.onStartCommand(意向,标志,startId);
    }



    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
        Log.e(TAG,中的onDestroy在LocationService类);
        mlocManager.removeUpdates(mlocListener);


    }







    @覆盖
    公众的IBinder onBind(意向意图){
        返回null;
    }

    私有类MyLocationListener实现LocationListener的{

        @覆盖
        公共无效onLocationChanged(位置LOC){


            Log.e(TAG,在TrackingService onlocationChanged约送经度/纬度+ loc.getLongitude()++ loc.getLatitude());


            DateTime的DT =新的日期时间();
            DateTimeFormatter DF3 = DateTimeFormat.forPattern(YYYY-MM-DD H:MM:SS.SSS);
            字符串formattedNowTime3 = df3.print(DT);
            Log.e(TAG,+ formattedNowTime3在TrackingServive =位置锁定时间);


            光标C = nfcscannerapplication.loginValidate.queryAllFromCarer();

            如果(c.getCount()大于0){
                如果(c.moveToLast()){

                carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));

                }
            }

             Log.e(TAG,照顾者ID =+ carerID);

             nfcscannerapplication.loginWebservice.sendCarerLocation(carerID,formattedNowTime3,将String.valueOf(loc.getLatitude()),将String.valueOf(loc.getLongitude()));



             Log.e(TAG,在quiting handlerthread);
             handlerThread.quit();

             mlocManager.removeUpdates(mlocListener);
             Log.e(TAG,删除更新(TrackingService));

             TrackingService.this.stopSelf();
             Log.e(TAG,呼吁stopSelf上TrackingService);

        }

        @覆盖
        公共无效onProviderDisabled(字符串提供商){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onProviderEnabled(字符串提供商){
            // TODO自动生成方法存根

        }

        @覆盖
        公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
            // TODO自动生成方法存根

        }

    MyLocationListener的} //结束


    公共无效enableMenuButtonsHandler(){

        endServiceHandler =新的处理程序();
        endServiceRunnable =新的Runnable(){
            公共无效的run(){

                endService();

            }

            私人无效endService(){

                 mlocManager.removeUpdates(mlocListener);
                 Log.e(TAG,删除更新(TrackingService)从endService处理程序);

                 TrackingService.this.stopSelf();
                 Log.e(TAG,呼吁stopSelf上TrackingService从endService处理程序);

                 Log.e(TAG,从endService处理器在quiting handlerthread);
                 handlerThread.quit();

            }
        };

    }


服务} //结束
 

解决方案

作为的Pankaj库马尔指出,一个 IntentService 不是个案一个合适的解决方案,需要做的工作在本质上是异步的。一旦 onHandleIntent()的回报,你的服务被销毁。

使用普通的服务,注册在 onStartCommand位置(),使用 HandlerThread 的处理结果(这样你就可以通过它尺蠖 requestLocationUpdates()) 。一旦你的位置收到,或适当的超时为止,做的服务,您的工作和呼叫 stopSelf()来关闭它。

I have an app where i am trying to periodically get user location and send to server. I have a service attached to an Alarm manager which executes every minute (for testing). The service finds the user location correctly and logs out the gps co-ords. Once there is a gps lock i cancel the location request and stop the service. When i ask for location updates, i start a handler that executes 20 seconds later, this handler removes the location update and stops the service in case no lock is acheived. All this works.

Below is the code that works using the Service class.

public class TrackingService extends Service {

    private static final String TAG = TrackingService.class.getSimpleName();
    LocationManager             mlocManager;
    LocationListener            mlocListener;
    NfcScannerApplication       nfcscannerapplication;
    String carerID;

    Handler endServiceHandler;
    Runnable endServiceRunnable;

    @Override
    public void onCreate() {
        super.onCreate();

        nfcscannerapplication = (NfcScannerApplication) getApplication();
        mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();

        Log.e(TAG, "Service created and location manager and listener created");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "in onDestroy in LocationService class");
        mlocManager.removeUpdates(mlocListener);


    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        Log.e(TAG, "requesting location updates");

        enableMenuButtonsHandler();
        endServiceHandler.postDelayed(endServiceRunnable,20 * 1000);




    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {


            Log.e(TAG, "in TrackingService onlocationChanged and about to send lon/lat " + loc.getLongitude() + " " + loc.getLatitude());


            DateTime dt = new DateTime();
            DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
            String formattedNowTime3 = df3.print(dt);
            Log.e(TAG, "Time of location fix in TrackingServive = " + formattedNowTime3);


            Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();

            if (c.getCount() > 0) {
                if(c.moveToLast()){

                carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));

                }
            }

             Log.e(TAG, "carer ID = " + carerID);

             mlocManager.removeUpdates(mlocListener);
             Log.e(TAG, "removed updates(TrackingService)");

             TrackingService.this.stopSelf();
             Log.e(TAG, "called stopSelf on TrackingService");

        }

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

        }

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

        }

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

        }

    }// end of MyLocationListener


    public void enableMenuButtonsHandler() {

        endServiceHandler = new Handler();
        endServiceRunnable = new Runnable() {
            public void run() {

                endService();

            }

            private void endService() {

                 mlocManager.removeUpdates(mlocListener);
                 Log.e(TAG, "removed updates(TrackingService) from the endService handler");

                 TrackingService.this.stopSelf();
                 Log.e(TAG, "called stopSelf on TrackingService from the endService handler");

            }
        };

    }


}// end of service 

.

The problem i am having is once i have a gps lock, i want to send the long and lat co-ords to the server. I know i can use AsyncTask from a service but i don't want to do this. I would prefer to use IntentService as it runs in its own background thread. This way i can make network call directly from the IntentService.

The following code implements the IntentService class but it doesn't seem to get a lock. The gps keeps flashing on the phone indefinately. The logging statements get as far as 'requesting location updates', then nothing after that.

Has anyone any ideas why no lock is acheived? Thanks in advance.

public class TrackingService extends IntentService {

   private static final String TAG = TrackingService.class.getSimpleName();
   LocationManager             mlocManager;
   LocationListener            mlocListener;
   NfcScannerApplication       nfcscannerapplication;
   String carerID;

   Handler endServiceHandler;
   Runnable endServiceRunnable;



    public TrackingService() {
        super("TrackingService");



    }

    @Override
    protected void onHandleIntent(Intent intent) {

           nfcscannerapplication = (NfcScannerApplication) getApplication();
           mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
           mlocListener = new MyLocationListener();
           Log.e(TAG, "Service created and location manager and listener created");

           mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
           Log.e(TAG, "requesting location updates");

           enableMenuButtonsHandler();
           endServiceHandler.postDelayed(endServiceRunnable,20 * 1000);

    }



     private class MyLocationListener implements LocationListener {

                 @Override
                 public void onLocationChanged(Location loc) {


                    Log.e(TAG, "in TrackingService onlocationChanged and about to send lon/lat " + loc.getLongitude() + " " + loc.getLatitude());


                     DateTime dt = new DateTime();
                     DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
                     String formattedNowTime3 = df3.print(dt);
                     Log.e(TAG, "Time of location fix in TrackingServive = " + formattedNowTime3);


                     Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();

                    if (c.getCount() > 0) {
                        if(c.moveToLast()){

                        carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));

                        }
                    }

                     Log.e(TAG, "carer ID = " + carerID);

                     mlocManager.removeUpdates(mlocListener);
                     Log.e(TAG, "removed updates(TrackingService)");

                     TrackingService.this.stopSelf();
                     Log.e(TAG, "called stopSelf on TrackingService");

                 }

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

                 }

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

                 }

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

                 }

             }// end of MyLocationListener


     public void enableMenuButtonsHandler() {

                endServiceHandler = new Handler();
                endServiceRunnable = new Runnable() {
                    public void run() {

                        endService();

                    }

                    private void endService() {

                         mlocManager.removeUpdates(mlocListener);
                         Log.e(TAG, "removed updates(TrackingService) from the endService handler");

                         TrackingService.this.stopSelf();
                         Log.e(TAG, "called stopSelf on TrackingService from the endService handler");

                    }
                };

     }

}//end of trackingService

[Edit1]

public class TrackingService extends Service {

    private static final String TAG = TrackingService.class.getSimpleName();
    LocationManager             mlocManager;
    LocationListener            mlocListener;
    NfcScannerApplication       nfcscannerapplication;
    String carerID;

    Handler endServiceHandler;
    Runnable endServiceRunnable;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        nfcscannerapplication = (NfcScannerApplication) getApplication();
        mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();

        Log.e(TAG, "Service created and location manager and listener created");

        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        Log.e(TAG, "requesting location updates");

        enableMenuButtonsHandler();
        endServiceHandler.postDelayed(endServiceRunnable, 20 * 1000);

            return super.onStartCommand(intent, flags, startId);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "in onDestroy in LocationService class");
        mlocManager.removeUpdates(mlocListener);


    }








    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {


            Log.e(TAG, "in TrackingService onlocationChanged and about to send lon/lat " + loc.getLongitude() + " " + loc.getLatitude());


            DateTime dt = new DateTime();
            DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
            String formattedNowTime3 = df3.print(dt);
            Log.e(TAG, "Time of location fix in TrackingServive = " + formattedNowTime3);


            Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();

            if (c.getCount() > 0) {
                if(c.moveToLast()){

                carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));

                }
            }

             Log.e(TAG, "carer ID = " + carerID);

             mlocManager.removeUpdates(mlocListener);
             Log.e(TAG, "removed updates(TrackingService)");

             TrackingService.this.stopSelf();
             Log.e(TAG, "called stopSelf on TrackingService");

        }

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

        }

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

        }

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

        }

    }// end of MyLocationListener


    public void enableMenuButtonsHandler() {

        endServiceHandler = new Handler();
        endServiceRunnable = new Runnable() {
            public void run() {

                endService();

            }

            private void endService() {

                 mlocManager.removeUpdates(mlocListener);
                 Log.e(TAG, "removed updates(TrackingService) from the endService handler");

                 TrackingService.this.stopSelf();
                 Log.e(TAG, "called stopSelf on TrackingService from the endService handler");

            }
        };

    }


}// end of service 

[edit2]

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        nfcscannerapplication = (NfcScannerApplication) getApplication();
        mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();
        Log.e(TAG, "Service created and location manager and listener created");


            HandlerThread handlerThread = new HandlerThread("MyHandlerThread");
            handlerThread.start();
            Looper looper = handlerThread.getLooper();

            Handler handler = new Handler(looper);

        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener, looper);
        Log.e(TAG, "requesting location updates");

        enableMenuButtonsHandler();
        endServiceHandler.postDelayed(endServiceRunnable, 20 * 1000);

            return super.onStartCommand(intent, flags, startId);
    }

.

[edit3]

public class TrackingService extends Service {

    private static final String TAG = TrackingService.class.getSimpleName();
    LocationManager             mlocManager;
    LocationListener            mlocListener;
    NfcScannerApplication       nfcscannerapplication;
    String carerID;

    Handler endServiceHandler;
    Runnable endServiceRunnable;
    HandlerThread handlerThread;
    Looper looper;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        nfcscannerapplication = (NfcScannerApplication) getApplication();
        mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mlocListener = new MyLocationListener();
        Log.e(TAG, "Service created and location manager and listener created");


            Log.e(TAG, "creating handlerthread and looper");
            handlerThread = new HandlerThread("MyHandlerThread");
            handlerThread.start();
            looper = handlerThread.getLooper();





        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener, looper);
        Log.e(TAG, "requesting location updates");

        enableMenuButtonsHandler();
        endServiceHandler.postDelayed(endServiceRunnable, 20 * 1000);

            return super.onStartCommand(intent, flags, startId);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "in onDestroy in LocationService class");
        mlocManager.removeUpdates(mlocListener);


    }







    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {


            Log.e(TAG, "in TrackingService onlocationChanged and about to send lon/lat " + loc.getLongitude() + " " + loc.getLatitude());


            DateTime dt = new DateTime();
            DateTimeFormatter df3 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.SSS");
            String formattedNowTime3 = df3.print(dt);
            Log.e(TAG, "Time of location fix in TrackingServive = " + formattedNowTime3);


            Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();

            if (c.getCount() > 0) {
                if(c.moveToLast()){

                carerID = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));

                }
            }

             Log.e(TAG, "carer ID = " + carerID);

             nfcscannerapplication.loginWebservice.sendCarerLocation(carerID, formattedNowTime3, String.valueOf(loc.getLatitude()), String.valueOf(loc.getLongitude()));



             Log.e(TAG, "quiting handlerthread");
             handlerThread.quit();

             mlocManager.removeUpdates(mlocListener);
             Log.e(TAG, "removed updates(TrackingService)");

             TrackingService.this.stopSelf();
             Log.e(TAG, "called stopSelf on TrackingService");

        }

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

        }

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

        }

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

        }

    }// end of MyLocationListener


    public void enableMenuButtonsHandler() {

        endServiceHandler = new Handler();
        endServiceRunnable = new Runnable() {
            public void run() {

                endService();

            }

            private void endService() {

                 mlocManager.removeUpdates(mlocListener);
                 Log.e(TAG, "removed updates(TrackingService) from the endService handler");

                 TrackingService.this.stopSelf();
                 Log.e(TAG, "called stopSelf on TrackingService from the endService handler");

                 Log.e(TAG, "quiting handlerthread from the endService handler");
                 handlerThread.quit();

            }
        };

    }


}// end of service

解决方案

As Pankaj Kumar notes, an IntentService is not an appropriate solution for cases where the work to be done is intrinsically asynchronous. Once onHandleIntent() returns, your service is destroyed.

Use a regular Service, register for locations in onStartCommand(), using a HandlerThread for processing the results (so you can pass its Looper into requestLocationUpdates()). Once your location is received, or a suitable timeout is reached, do your work and call stopSelf() on the service to shut it down.