2011-06-15 42 views
4

我想用monodroid開發一個作爲後臺服務運行的android應用程序。monodroid for android服務

任何人都可以提供一個指針示例代碼如何做到這一點?

謝謝!

回答

10

我在我的samples on GitHub之一中有一個基本的服務示例。基本思想是定義一個擴展Service的類,並使用Service屬性對其進行修飾,以便在AndroidManifest.xml中生成適當的配置(您可以自己選擇,但很少需要)。

[Service] 
public class MusicService : Service 
{ 
    public override IBinder OnBind(Intent intent) 
    { 
     return null; 
    } 

    public override void OnCreate() 
    { 
     base.OnCreate(); 

     // ... 
    } 

    public override void OnStart(Intent intent, int startId) 
    { 
     base.OnStart(intent, startId); 

     // ... 
    } 

    public override void OnDestroy() 
    { 
     base.OnDestroy(); 

     // ... 
    } 
} 
+0

覆蓋哪裏 - 公共覆蓋StartCommandResult OnStartCommand(Intent intent,StartCommandFlags flags,int startId)? – samosaris 2012-07-13 14:38:52

+0

另外,根據您的需求,還有一些其他配置服務的步驟。請參閱http://www.vogella.com/articles/AndroidServices/article.html,這絕對是至關重要的。 – samosaris 2012-07-13 14:40:43

+0

對於決定在單獨過程中運行其服務的任何人,您應該看到http://mono-for-android.1047100.n5.nabble.com/Creating-service-in-a-new-process-td5710256.html並在https://bugzilla.xamarin.com/show_bug.cgi?id=763發表評論#4 – samosaris 2012-07-13 15:25:28