2011-06-15 43 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