1
我有一個類,爲MyService。這是我的Android服務。我想生成兩個隨機數字,這個服務應該返回這些數字的總和。我怎樣才能做到這一點?所以,我應該在Android的服務問題
public void onStart(Intent intent, int startid) {
//Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
// here i generate the numbers (random) and i compute the sum , nr1+nr2.
}
生成的數字。然後,我怎樣才能將結果返回到主活動,並顯示數字的總和作爲警告(例如),或在編輯框中時,按鈕被按下?因此,在主類,我有
public void onClick(View src) {
switch (src.getId()) {
//start the service
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, MyService.class));
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
break;
}
的問題是如何/我在哪裏可以返回服務(即NR1 + NR 2)的結果,我應該如何顯示的結果呢?
謝謝!
,所以我不知道我的情況如何辦理......我需要生成只有兩個數字,計算總和,而總和顯示某個地方..就到我發佈的代碼,我應該在哪裏寫代碼? 感謝 – qwerty 2010-05-25 14:44:20
我看到,例如從你給我的鏈接,但它是一個有點複雜..所以我需要的東西更容易,一個服務來計算兩個數字的總和 – qwerty 2010-05-25 14:54:51
你並不需要一個服務 - 你需要一個工人線。 http://developer.android.com/guide/appendix/faq/commontasks.html#threading – 2010-05-25 15:29:01