Thursday, July 21, 2011

[android-developers] Re: Bet practice for determining when a Service has done its work.

Thanks. That's all I needed to make it click.

For those searching after the fact, what I did was rework the service
so I could bind to it instead of simply starting it. In my Service I
ended up with

private final IBinder binder = new AutoLoginBinder();
private ICallback mCallback;

public class AutoLoginBinder extends Binder
{
public AutoLoginService getService()
{
return AutoLoginService.this;
}

public void setCallback(ICallback callback)
{
mCallback = callback;
}

public void doWork()
{
//work I need to do
}
}


private void stop()
{
if(mCallback != null)
{
Log.v(TAG, "Invoking callback");
mCallback.callback();
}

stopSelf();
}

@Override
public IBinder onBind(Intent intent)
{
return binder;
}

In my client I implement ICallback and add a method to do what I need
done and create a ServiceConnection like this:

private ServiceConnection getServiceConnection()
{
return new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder
service)
{
serviceBinder =
((AutoLoginService.AutoLoginBinder)service).getService();


((AutoLoginService.AutoLoginBinder)service).setCallback(PrimeData.this);
((AutoLoginService.AutoLoginBinder)service).doWork();
}

@Override
public void onServiceDisconnected(ComponentName arg0)
{
serviceBinder = null;
}
};


I prime it in my client like this:

Intent intent = new Intent(this, AutoLoginService.class);

mConnection = getServiceConnection();

bindService(intent, mConnection, Context.BIND_AUTO_CREATE);


Thanks again. Hopefully this helps some other folks!


On Jul 20, 4:51 pm, Mark Murphy <mmur...@commonsware.com> wrote:
> On Wed, Jul 20, 2011 at 5:38 PM, darrinps <darri...@gmail.com> wrote:
> >> > I don't see any way to register for a callback which was what I was hoping
> >> > for.
>
> >> That's pretty much the point of the LocalService example ... your activity
> >> has a reference to the Service and can do what it wants with it, including
> >> registering a custom callback that the Service invokes when it's done.
>
> > I understand the reference, but I don't see any where in the example
> > where any custom callback is done, and I don't see anything clear in
> > the API on how to do that.
>
> The LocalService sample uses the binding pattern.
>
> In the binding pattern, you supply a Binder implementation, with your
> own custom API. If you want that Binder to accept some sort of
> listener as a parameter, you can implement that yourself. If you want
> your Service to call that listener when an event occurs, you can
> implement that yourself.
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> Android Training...At Your Office:http://commonsware.com/training

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment