2012-10-22 21 views
0

我正在開發應用程序,其中包含記錄GPS位置的活動。它註冊LocationListener更新並將記錄的位置存儲到SQLite數據庫。我的問題是,當用戶隱藏我的應用程序或運行另一個應用程序時,我的應用程序和記錄活動將進入後臺並可能被系統殺死。因此,不存儲GPS數據。我希望即使應用程序轉到後臺,我的應用程序和錄製活動仍會記錄GPS位置。 如何實現這一目標?Android活動在後臺 - 錄製GPS

由於

+1

您可以編寫代碼來服務,而不是一個活動中獲取位置。 – 13hsoj

回答

0

使用的Android的其它部件等BroadcastReceiverService可以在背景沒有任何GUI運行。

public class UploadData extends BroadcastReceiver 
{ 
    private static LocationManager locationManager = null; 
    private static LocationListener locationListener = null; 

     @Override 
    public void onReceive(Context context, Intent intent) 
    { 
      // Create and AlarmManager that will periodically fetch the GPS 
    } 
} 

在服務的情況下,

public class myservice extends Service 
{ 
    LocationManager locationManager = null; 
    LocationListener loclistener = null; 

     // Bind it, Its important 
    public IBinder onBind(Intent intent) 
    { 
     return null; 
    } 
    @Override 
    public void onCreate() 
    { 
      // Use AlarmManager to fetch the GPS periodically. 
    } 
} 
2

一個Service是最適合你打算做什麼。

製作舒爾啓動服務STICKY