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

问题描述:

我正在尝试编写一个以间隔运行的服务。每次运行时,它都应该检索手机的当前位置。只要它看起来我的间隔正常运行。但只要按下启动按钮,我就不能再使用停止按钮来停止它。我已经尝试过使用一个线程,但那个只是给我错误。我也尝试了一段时间循环,但不知何故我的应用程序崩溃。 反正这是我的代码现在:停止按钮不停止我的位置服务

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(),我想你需要一个新的处理程序在运行的对象,并在的onDestroy发送邮件到处理程序停止位置侦听。

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

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

+0

不,即使它冻结。并且只回复主页按钮。 – user 2013-04-08 11:57:15

+0

看到我的编辑为GPS记录器库.. – Akhil 2013-04-08 12:08:34

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

+0

因为它必须有可能在任何时候停止服务。无需在运行时等待位置。 – user 2013-04-08 14:06:21