所以,我被要求做一個位置跟蹤。 即使應用已關閉,位置跟蹤器也應該跟蹤... 我的想法是通過調用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");};
}
}
}
我不太明白,你如何預期它的行爲?你的意思是你的活動第一次更新,但不是以後的時間? – Piovezan
我希望更新位置並將其存儲到PolylineOptions中,並在生活中將PolylineOptions傳遞迴活動。但那是未來的任務。現在我只需要證明應用程序被銷燬時位置更新的證據。我說我的線程無法正確運行。 – PauliusM