[Mono-bugs] [Bug 665345] New: Services: Some suggestions to improve the API and .netify it
bugzilla_noreply at novell.com
bugzilla_noreply at novell.com
Tue Jan 18 19:57:09 EST 2011
https://bugzilla.novell.com/show_bug.cgi?id=665345
https://bugzilla.novell.com/show_bug.cgi?id=665345#c0
Summary: Services: Some suggestions to improve the API and
.netify it
Classification: Mono
Product: MonoDroid
Version: SVN
Platform: Macintosh
OS/Version: Mac OS X 10.6
Status: NEW
Severity: Enhancement
Priority: P5 - None
Component: Class Libraries
AssignedTo: mkestner at novell.com
ReportedBy: jondick at gmail.com
QAContact: mono-bugs at lists.ximian.com
Found By: ---
Blocker: ---
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US)
AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10
So, for services there's the concept of binding a service to an activity on
android, to get a reference to the service that you can use directly from the
activity.
It all works right now as is, but I've created a couple of simple classes that
would really make it a lot easier and less verbose to work with.
using System;
using Android.OS;
using Android.Service;
using Android.App;
using Android.Content;
using Android.Runtime;
namespace Chapter13Threading
{
public class ServiceConnection<TService, TBinder> : Java.Lang.Object,
IServiceConnection where TService : Service where TBinder :
ServiceBinder<TService>
{
public event Action<TService> Connected;
public event Action Disconnected;
public void OnServiceConnected(ComponentName className, IBinder
serviceBinder)
{
if (this.Connected != null)
this.Connected((serviceBinder as
ServiceBinder<TService>).GetService());
}
public void OnServiceDisconnected(ComponentName className)
{
if (this.Disconnected != null)
this.Disconnected();
}
}
public class ServiceBinder<TService> : Binder where TService : Service
{
TService _service;
public ServiceBinder(TService service)
{
this._service = service;
}
public TService GetService()
{
return _service;
}
}
}
So, in your service you'd create a new ServiceBinder<MyService>(); instance and
return that in your OnBind overload for your Service. Simple.
In your Activity you'd create a new ServiceConnection<MyService,
ServiceBinder<MyService>>(); instance, and subscribe to its Connected and
Disconnected events:
myServiceConnection.Connected += delegate(MyService serviceInstance) {
this.ServiceInstance = serviceInstance;
this.button.Click += delegate {
this.ServiceInstance.SomeServiceMethod();
};
};
myServiceConnection.Disconnected += delegate {
this.ServiceInstance = null;
};
The idea here is you get an event for when your reference to the server is
valid (after your connection is connected) at which point you can use your
reference to the service. This is way less verbose than the way you have to do
it now, which is basically to create a class implementing IServiceConnection in
your activity, as well as a subclassing Binder in your service. These are just
helper classes that would be optional to use, but make things a bit quicker for
us.
Like? Dislike?
Reproducible: Always
--
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
More information about the mono-bugs
mailing list