2013-10-21 65 views
1

所以,我被要求做一個位置跟蹤。 即使應用已關閉,位置跟蹤器也應該跟蹤... 我的想法是通過調用startService(intent)從該活動開始我自己的服務(讓我們稱之爲TrackingService);所以服務將永遠運行(我猜...),然後從我自己創建的TrackingService連接到位置服務。 TrackingService應該在應用程序關閉後監聽位置更改。 我寫了一些代碼,啓動了TrackingService,並在新線程中請求位置更新。 無論如何,我退出應用後位置更新會停止,但服務仍在運行。服務,這聽服務定位服務

編輯: 好了,所以我設法改善我的代碼位,所以現在當我的應用程序運行時,我得到日誌的,我的線程(在單獨的服務運行)正在運行,並且它接收位置更新。 當我退出Ÿ應用程序,我仍然得到日誌,我的線程正在運行,但它沒有收到位置更新... 任何人都可以指出我的原因爲什麼? P.S.我知道可能有更好的方法來完成工作,但我真的希望修復我的代碼。 這裏去服務類

public class TrackingService extends Service { 
// DEBUG 
public final static String TAG = "TrackingService"; 
public final static boolean D = true; 
// Global constants 
private static final long UPDATE_INTERVAL = 10000; // Update frequency in milliseconds 
private static final long FASTEST_INTERVAL = 4000; // A fast frequency ceiling in milliseconds  
// 
int mStartMode;  // indicates how to behave if the service is killed 
private final IBinder mBinder = new LocalBinder();  // interface for clients that bind 
boolean mAllowRebind; // indicates whether onRebind should be used 
private int number; // testavimui 
LocationThread mLocationThread; 


@Override 
public void onCreate() { 
    if (D) {Log.d(TAG, "service - onCreated started");}; 
    mLocationThread = new LocationThread(this); 
    mLocationThread.start(); 
    // mLocationThread.run(); 
} 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    if (D) {Log.d(TAG, "service - onStartCommand started");}; 
    // The service is starting, due to a call to startService() 
    return mStartMode; 
} 
@Override 
public IBinder onBind(Intent intent) { 
    if (D) {Log.d(TAG, "service - onBind started");}; 
    // A client is binding to the service with bindService() 
    return mBinder; 
} 
@Override 
public boolean onUnbind(Intent intent) { 
    if (D) {Log.d(TAG, "service - onUnBind started");}; 
    // All clients have unbound with unbindService() 
    return mAllowRebind; 
} 
@Override 
public void onRebind(Intent intent) { 
    if (D) {Log.d(TAG, "service - onReBind started");}; 
    // A client is binding to the service with bindService(), 
    // after onUnbind() has already been called 
} 
@Override 
public void onDestroy() { 
    if (D) {Log.d(TAG, "service - onDestroy started");}; 
    // The service is no longer used and is being destroyed 
    mLocationThread.cancel(); 
} 

public class LocalBinder extends Binder { 
    TrackingService getService() { 
     // Return this instance of LocalService so clients can call public methods 
     return TrackingService.this; 
    } 
} 

public int number(){ 
    number += 1; 
    return number; 
} 



private class LocationThread extends Thread implements 
         GooglePlayServicesClient.ConnectionCallbacks, 
         GooglePlayServicesClient.OnConnectionFailedListener, 
         LocationListener{ 


    private boolean keepOn; 
    private Context mContext; 
    private LocationClient mLocationClient; 
    private LocationRequest mLocationRequest; 

    public LocationThread (Context context){ 
     mContext = context; 
     keepOn = true; 

    } 

    public void cancel() { 
     keepOn = false; 
     if (D){Log.d(TAG, "thread was canceled");}; 


    } 

    public void run(){ 
     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // Use high accuracy 
     mLocationRequest.setInterval(UPDATE_INTERVAL); // Set the update interval to 5 seconds 
     mLocationRequest.setFastestInterval(FASTEST_INTERVAL); // Set the fastest update interval to 1 second 
     mLocationClient = new LocationClient(mContext, this, this); 
     mLocationClient.connect(); 

     while (keepOn){ 
      try { 
       Thread.sleep(10000); 
       if(D){Log.d(TAG, "thread running");}; 
      } catch (Exception e){ 

      } 
     } 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     if(D){Log.d(TAG, "connection failed");}; 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     if(D){Log.d(TAG, "connected to location service");}; 
     mLocationClient.requestLocationUpdates(mLocationRequest, this); 
    } 

    @Override 
    public void onDisconnected() { 
     if(D){Log.d(TAG, "disconnected from location service");}; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     if(D){Log.d(TAG, "Location changed");}; 

    } 
} 

}

+0

我不太明白,你如何預期它的行爲?你的意思是你的活動第一次更新,但不是以後的時間? – Piovezan

+0

我希望更新位置並將其存儲到PolylineOptions中,並在生活中將PolylineOptions傳遞迴活動。但那是未來的任務。現在我只需要證明應用程序被銷燬時位置更新的證據。我說我的線程無法正確運行。 – PauliusM

回答

0

雖然文件沒有具體說明的話,我建議你在主(UI)線程中執行調用LocationManager.requestLocationUpdates()。如果呼叫來自單獨的線程,則可以使用Handler來完成該操作。

順便說一句,如果你想讓你的服務運行在一個單獨的線程中,我建議你擴展IntentService並覆蓋onHandleIntent()方法,這樣更容易。

更多建議:

如果你希望你的Service運行,即使手機處於睡眠模式時,你需要一個喚醒鎖。

提示:您不必使其連續運行,這將不必要地消耗大量電池。讓你Service收集在一個位置,將其保存到本地數據庫或將其提供給綁定活動,然後停下來,然後計劃該Service從時間使用AlarmManager運行時間。

它是這樣的:AlarmManager調用WakefulBroadcastReceiver,它依次調用您的Service

我建議你閱讀WakefulBroadcastReceiver文檔,它將爲您自動服務喚醒鎖(你必須手動釋放該服務停止前)。

+0

感謝您的回答,我會在未來考慮這個,但現在我真的不關心電池壽命短:) 我覺得我的主題DOS無法正常運行。 – PauliusM

+0

@PauliusM我已經更新了我的答案。它可能有些用處。 – Piovezan