2013-04-04 104 views
1

我正在嘗試編寫一個以間隔運行的服務。每次運行時,它都應該檢索手機的當前位置。只要它看起來我的間隔正常運行。但只要按下啓動按鈕,我就不能再使用停止按鈕來停止它。我已經嘗試過使用一個線程,但那個只是給我錯誤。我也嘗試了一段時間循環,但不知何故我的應用程序崩潰。 反正這是我的代碼現在:停止按鈕不停止我的位置服務

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(); 
} 

}

回答

0

你是在一個Runnable運行的getLocation(),我想你需要一個新的處理程序在運行的對象,並在的onDestroy發送郵件到處理程序停止位置偵聽。

在您的可運行內部HandleMessage中,您可以停止偵聽位置。

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

+0

不,即使它凍結。並且只回復主頁按鈕。 – user 2013-04-08 11:57:15

+0

看到我的編輯爲GPS記錄器庫.. – Akhil 2013-04-08 12:08:34

0

爲什麼不讓服務運行,並告訴LocationManager僅在intervalTime s處通知您。換句話說,刪除stopSelf()並將minTime設置爲1000 * 30

+0

因爲它必須有可能在任何時候停止服務。無需在運行時等待位置。 – user 2013-04-08 14:06:21