2013-07-14 153 views
1

我想寫一個android應用程序,它記錄通過使用照片uri,時間戳和地理位置戳進入文本文件所拍攝的所有照片。這應該在點擊照片時發生。
爲此,我正在運行在默認照片目錄上使用FileObserver的服務。 (我知道這不是傻瓜證明)。
在Android中恢復活動狀態

用戶最初受到GUI的歡迎,該GUI將允許他選擇文件名和開始按鈕以開始錄製。當用戶按下開始錄製時,後臺服務啓動並且用戶返回拍攝一些照片,1當他回來時他應該有一個停止錄製的選項,這將結束後臺服務。

現在我的問題是這樣的
1.活動如何知道服務何時運行以及何時不運行?
2. Activity的前一狀態如何恢復並重新連接到特定服務?就像我在恢復活動時活動與服務的關聯是如何發生的? (consedring某種關聯是必要的,如果我的活動已停止服務)

這裏是我的參考代碼:[ExperienceLoggerService是一個內部類MainActivity的]

public class ExperienceLoggerService extends Service 
/* This is an inner class of our main activity, as an inner class makes good use of resources of outer class */ 
{ 


    private final IBinder mBinder = new LocalBinder();  
    File file; 
    FileOutputStream fOut; 
    OutputStreamWriter fWrite; 
    /** Called when the activity is first created. */ 
    private void startLoggerService() 
    { 
     try 
     { 
      //initialise the file in which to log 
      this.file = new File(Environment.getExternalStorageDirectory(), "MyAPPNostalgia"); 
      System.out.println("1:"+Environment.getExternalStorageDirectory()+ "MyAPPNostalgia"); 
      file.createNewFile(); 
      fOut = new FileOutputStream(file); 
      fWrite = new OutputStreamWriter(fOut); 
     } 
     catch(Exception e) 
     { 
      System.out.println("Error in logging data, blame navjot"); 
     } 
     FileObserver observer = new MyFileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA"); 
     observer.startWatching(); // start the observer 
    } 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     //mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     startLoggerService(); 

     // Display a notification about us starting. We put an icon in the 
     // status bar. 
     //showNotification(); 
    } 

    public void onDestroy() 
    { 
     try 
     { 
      //close the file, file o/p stream and out writer. 
      fWrite.close(); 
      fOut.close(); 
     } 

     catch(Exception e) 
     { 
     } 

     super.onDestroy(); 

    } 

    class MyFileObserver extends FileObserver 
    { 
     public MyFileObserver(String path) 
     { 
      super(path); 
     } 

     public void onEvent(int event, String file) 
     { 
      if(event == FileObserver.CREATE && !file.equals(".probe")) 
      { // check if its a "create" and not equal to .probe because thats created every time camera is launched 
       String fileSaved = "New photo Saved: " + file +"\n"; 
       try 
       { 
        ExperienceLoggerService.this.fWrite.append(fileSaved); 
       } 
       catch(Exception e) 
       { 
        System.out.println("Problem in writing to file"); 
       } 
      } 
     } 
    } 


    @Override 
    public IBinder onBind(Intent intent) 
    { 
     return mBinder; 
    } 

    public class LocalBinder extends Binder 
    { 
     ExperienceLoggerService getService() 
     { 
      return ExperienceLoggerService.this; 
     } 
    } 

} 

回答

1

你並不需要一個與服務「連接」以阻止其從活動中移除。你可以這樣做:

Intent intent = new Intent(this, ExperienceLoggerService.class); 
stopService(intent); 

我不確定你真的需要知道你的服務是否在運行。如果你真的需要做到這一點,你可以使用ActivityManager.getRunningServices()做到這一點,看到http://developer.android.com/reference/android/app/ActivityManager.html#getRunningServices%28int%29

編輯:注:關於約束服務

你沒有張貼在您的活動結合到服務代碼,但後再次查看您的源代碼,我發現您使用的是綁定服務。在這種情況下,您的活動只需撥打unbindService()即可通過調用bindService()時使用的相同ServiceConnection對象。一旦服務沒有綁定的客戶端,它就會關閉。

+0

但是當我回到我的應用程序時,我的服務已經在運行(我早就開始了)。我沒有得到的是如何創建一個新的意圖,並調用stopService停止我以前的服務? – deepak

+0

這就是你如何停止服務。通過調用'stopService()'並傳遞一個描述你想停止服務的'Intent'。 –

+0

對不起,我沒有意識到你正在使用**綁定服務**。我已經更新了我的答案以反映這一點。有不同的方式來使用服務,'startService()/ stopService()'就是其中之一。你也可以使用'bindService()/ unbindService()'。 –