2013-12-14 13 views
0

我一直卡在一個問題一段時間了。我的活動啓動並使用ServiceName.PutExtra(name,value)在活動OnPause()中向我的服務發送數據(DT開始,DT完成,Char操作),但它也需要讀取相同的新值。檢索數據從服務到活動OnResume()

該活動使用GPS記錄設備何時移動以及何時移動,以及這些移動的開始和結束時間。

當我回到我的活動OnResume()需要讀取服務當前記錄的內容時,請繼續此記錄並停止服務。

我已經閱讀了很多關於綁定的服務,但不能把這個使用簡單的代碼樣本的解釋(來源@ Xamarin網站,Android開發者網站,計算器)工作

有人能告訴我一個代碼服務活動之間明確規定綁定,以及如何將服務中的數據轉化爲活動OnResume()?

我與Xamarin和C#的工作,但如果需要的話

我沒有用很長一段時間(因爲我使用的是現在這個目的LocalBroadcasts)

回答

1

我可以從Java代碼適應,所以我可以錯過的東西,但在總體上是類似的東西(在Java中):

的活動:

/** Defines callbacks for service binding, passed to bindService() */ 
private ServiceConnection mConnection = new ServiceConnection() { 

public void onServiceConnected(ComponentName arg0, IBinder service) { 
    LocalBinder binder = (LocalBinder) service; 
    mStateChecker = binder.getService(); 
    mBound = true; 

    Intent intent2 = getIntent(); 
    finish(); 
    startActivity(intent2); 
} 


public void onServiceDisconnected(ComponentName arg0) { 
    mBound = false; 

    Intent intent2 = getIntent(); 
    finish(); 
    startActivity(intent2); 

    } 
}; 




//start the service & bind to it 
    startService(new Intent(this, StateChecker.class)); 
    Intent intent = new Intent(this, StateChecker.class); 
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 

在服務:

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

/** 
* Class used for the client Binder. Because we know this service always 
* runs in the same process as its clients, we don't need to deal with IPC. 
*/ 
public class LocalBinder extends Binder { 
    StateChecker getService() { // StateChecker is the name of this class 
     // Return this instance of LocalService so clients can call public methods 
     return StateChecker.this; 
    } 
} 
+0

欣賞幫助,但無法讓它適用於我....只是在沒有數據交換的情況下進行綁定調查(在活動進行時保持活動狀態)並使用廣播傳輸數據。 – bbbwex