Re: [android-developers] how to BIND myActivity to a Service

2011-03-02 Thread Kostya Vasilyev

For binding to work, you need something like this in the service class:

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

That "something not null" should probably be MazeAlgService.MyBinder, 
judging by the cast in the code.


Also be aware that service binding is asynchronous - the service 
connection is established only after your code calls bindService and 
returns from the click handler to the Android message loop.


-- Kostya

01.03.2011 19:21, Ivo_K пишет:

Hi!
I'm reading the book Professional Android 2 Application Development
from Reto Meier. I'm stuck on page 298.

I need having a Service in the app, and almost it works, but not
quite.

I get my Service to run, but cannot bind to it (so my reference
serviceBinder is never initialized; e.g. I get the
NullPointerException. The following code snippet is never run, and I
am confused where to put it:

Please note that I return Service.START_STICKY; from
onStartCommand( ...)  from the Service.

Following makes the service to run:
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

//  Bind to the service
Intent bindIntent = new Intent(MazeAlgorithm.this,
MazeAlgService.class);
bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);
}

but this code below is NEVER run (so serviceBinder is never
initialized):

private ServiceConnection mConnection = new ServiceConnection(){
public void onServiceConnected(ComponentName className, IBinder
service){
// Called when the connection is made.
serviceBinder = ((MazeAlgService.MyBinder) 
service).getService();
}

public void onServiceDisconnected(ComponentName className){
// Received when the service unexpectedly disconnects.
Context context = getApplicationContext();
serviceBinder = null;
}
};

MazeAlgService is the service, called from the class MazeAlgorithm.
Where to shall I put the code above in the context of the Activity?

Nicest thanks to all of you (and just for info: I'm new to this
group...  :-) )
/Ivo




--
Kostya Vasilyev -- http://kmansoft.wordpress.com

--
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] how to BIND myActivity to a Service

2011-03-02 Thread Ivo_K
Hi!
I'm reading the book Professional Android 2 Application Development
from Reto Meier. I'm stuck on page 298.

I need having a Service in the app, and almost it works, but not
quite.

I get my Service to run, but cannot bind to it (so my reference
serviceBinder is never initialized; e.g. I get the
NullPointerException. The following code snippet is never run, and I
am confused where to put it:

Please note that I return Service.START_STICKY; from
onStartCommand( ...)  from the Service.

Following makes the service to run:
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

//  Bind to the service
Intent bindIntent = new Intent(MazeAlgorithm.this,
MazeAlgService.class);
bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);
}

but this code below is NEVER run (so serviceBinder is never
initialized):

private ServiceConnection mConnection = new ServiceConnection(){
public void onServiceConnected(ComponentName className, IBinder
service){
// Called when the connection is made.
serviceBinder = ((MazeAlgService.MyBinder) 
service).getService();
}

public void onServiceDisconnected(ComponentName className){
// Received when the service unexpectedly disconnects.
Context context = getApplicationContext();
serviceBinder = null;
}
};

MazeAlgService is the service, called from the class MazeAlgorithm.
Where to shall I put the code above in the context of the Activity?

Nicest thanks to all of you (and just for info: I'm new to this
group...  :-) )
/Ivo

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en