65.9K
CodeProject 正在变化。 阅读更多。
Home

MonoAndroid:使用绑定服务

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.89/5 (3投票s)

2013 年 9 月 6 日

CPOL

2分钟阅读

viewsIcon

13895

downloadIcon

140

绑定服务演示!

引言

前天我发布了一篇关于启动服务的文章,这篇文章讨论的是绑定服务。由于区别非常细微,我更倾向于将其发布为技巧而不是完整的文章。

启动的服务绑定服务之间的区别在于,绑定服务与调用 Activity 的生命周期绑定,而启动的服务则不绑定。

要启动启动的服务,我们必须使用 StartService 方法,而要启动绑定服务,我们必须使用 BindService 方法。 同样,要停止启动的服务,我们必须依赖外部的 StopService 和服务代码内的 StopSelf。 对于绑定服务,我们可以调用 StartedService 中提到的方法,或者直接退出主 Activity。

使用代码 

现在,基本上要创建绑定服务,我们需要创建一个派生自 IServiceConnection 的类,它将充当服务和 Activity 之间的连接,以及派生自 Binder 的类,它提供进程间通信的方式,请参阅以下代码:-

  • BoundServiceBinder
    class BoundServiceBinder : Binder
    {
        Service _boundService = null;
     
        public BoundServiceBinder (Service boundServiceCls)
        {
            _boundService = boundServiceCls;
        }
     
        public Service GetBoundService ()
        {
            return _boundService;
        }
    } 
    


  •  

  • BoundServiceConnection
    class BoundServiceConnection: Java.Lang.Object,IServiceConnection
    {
        Activity _activity;
        public BoundServiceConnection (Activity activity){
            _activity = activity;            
        }
     
        #region IServiceConnection implementation
        void IServiceConnection.OnServiceConnected (ComponentName name, 
                                                    IBinder service){        
        }
     
        void IServiceConnection.OnServiceDisconnected (ComponentName name)  {        
        }
        #endregion
    }
    


现在我们的服务代码看起来像这样,与 StartedService 唯一的区别是,我们不再从 OnBind 方法中传递 NULL,而是返回 binder 对象,其类代码在上面提供。 在 OnCreateOnDestroy 函数中,我们将创建和销毁消息记录到控制台。

[Service]
class BoundServiceCls: Service
{
	BoundServiceBinder binder = null;
	#region implemented abstract members of Service
 
	public override IBinder OnBind (Intent intent){
		binder = new BoundServiceBinder (this);
		return binder;		
	}
 
	#endregion
 
	public override void OnCreate (){
		base.OnCreate ();
		Log.Debug ("BoundServiceBinder", "BoundServiceBinder Created");
	}
 
	public override void OnDestroy (){
		base.OnDestroy ();
		Log.Debug ("BoundServiceBinder", "BoundServiceBinder Stopped");
	}
}

现在在 Main.axml 文件中,我添加了两个按钮,一个用于启动服务,另一个用于停止服务。

MainActivity.cs 中,代码如下:-

[Activity (Label = "BoundServiceTest", MainLauncher = true)]
public class MainActivity : Activity{	
	public Binder _binder=null;
	Intent _intentServie;
	IServiceConnection _conn;
 
	protected override void OnCreate (Bundle bundle){
 
		base.OnCreate (bundle);
		SetContentView (Resource.Layout.Main);
 
		Button button = FindViewById<Button> (Resource.Id.myButton);
		Button button1 = FindViewById<Button> (Resource.Id.myButton1);
		
		button.Click += delegate {
			_intentServie = new Intent(this,typeof(BoundServiceCls));
			_conn = new BoundServiceConnection(this);
			this.BindService(_intentServie,_conn,Bind.AutoCreate);
		};
 
		button1.Click += delegate {
			this.UnbindService(_conn);
		};		
	}
}

要创建服务,我们创建服务 Intent,然后创建连接并使用 BindService 方法将连接绑定到启动的服务。 要停止服务,我们只需要调用 UnbindService

构建并运行应用程序。 单击每个按钮,您可以看到以下输出:-

本系列文章     

本系列技巧/窍门   

历史 

  • 2013年9月6日:第一个版本。
  • 2013年11月22日:更新了其他文章部分

© . All rights reserved.