Re: [android-beginners] Using Methods in a Service from an Activity

2010-04-20 Thread Tom F M White
Ok, I've found the problem, for anyone else having trouble. In the 
tutorial, onBind() returns null, I created a MediaBinder class within 
MediaService, that implemented my MediaInterface, calling the relevant 
methods in MediaService. This works nicely.


On 19/04/2010 21:43, Mark Murphy wrote:

Tom F M White wrote:
   

I'm calling start a relatively long time later, several seconds. Do I
need to call onServiceConnected() myself?
 

No, it should be called shortly after you call bindService(). This
suggests that your bindService() call is failing for some reason (e.g.,
your service is not registered in your manifest).

Here is a sample project from one of my books showing the use of
bindService():

http://github.com/commonsguy/cw-android/tree/master/Service/WeatherPlus/

   


--
You received this message because you are subscribed to the Google
Groups Android Beginners group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en


[android-beginners] Using Methods in a Service from an Activity

2010-04-19 Thread Tom F M White

Further to my previous email, I have sketched out how my Media Player
will work with the actual MediaPlayer object in a separate service,
MediaService. I can't work out how to make calls, and retrieve data from
the service. Following tutorials online has lead me to this:

MyApp.java - top level Activity, in onCreate():
...
MediaConnection conn = new MediaConnection();
StateApp state = (StateApp) getApplicationContext();
bindService( new Intent(this,MediaService.class), conn,
Context.BIND_AUTO_CREATE);
state.setMediaConnection(conn);
...
MediaView.java - View for playing media files, in play button onClick():
...
StateApp state = (StateApp) context.getApplicationContext();
MediaConnection conn = state.getMediaConnection();
conn.start(mediaLocation);
...

MediaConnection implements ServiceConnection:
public class MediaConnection implements ServiceConnection, MediaInterface{

 private Binder service;

 public void start(String loc){
 MediaInterface i = (MediaInterface)service;
 i.start(loc);
 }
 public void onServiceDisconnected(ComponentName cn){
 Log.i(INFO, Service unbound );
 }
 public void onServiceConnected(ComponentName cn, IBinder b){
 service = (Binder)b;
 Log.i(INFO, Service bound );
 }
}

Which throws a NullPointerException at i.start(loc). Can anyone tell me
what I'm doing wrong?

--
You received this message because you are subscribed to the Google
Groups Android Beginners group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en


Re: [android-beginners] Using Methods in a Service from an Activity

2010-04-19 Thread Mark Murphy
Tom F M White wrote:
 Further to my previous email, I have sketched out how my Media Player
 will work with the actual MediaPlayer object in a separate service,
 MediaService. I can't work out how to make calls, and retrieve data from
 the service. Following tutorials online has lead me to this:
 
 MyApp.java - top level Activity, in onCreate():
 ...
 MediaConnection conn = new MediaConnection();
 StateApp state = (StateApp) getApplicationContext();
 bindService( new Intent(this,MediaService.class), conn,
 Context.BIND_AUTO_CREATE);
 state.setMediaConnection(conn);
 ...
 MediaView.java - View for playing media files, in play button onClick():
 ...
 StateApp state = (StateApp) context.getApplicationContext();
 MediaConnection conn = state.getMediaConnection();
 conn.start(mediaLocation);
 ...
 
 MediaConnection implements ServiceConnection:
 public class MediaConnection implements ServiceConnection, MediaInterface{
 
  private Binder service;
 
  public void start(String loc){
  MediaInterface i = (MediaInterface)service;
  i.start(loc);
  }
  public void onServiceDisconnected(ComponentName cn){
  Log.i(INFO, Service unbound );
  }
  public void onServiceConnected(ComponentName cn, IBinder b){
  service = (Binder)b;
  Log.i(INFO, Service bound );
  }
 }
 
 Which throws a NullPointerException at i.start(loc). Can anyone tell me
 what I'm doing wrong?
 

Off the cuff, I would guess that you are calling start() before Android
calls onServiceConnected().

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android Development Wiki: http://wiki.andmob.org

-- 
You received this message because you are subscribed to the Google
Groups Android Beginners group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en


Re: [android-beginners] Using Methods in a Service from an Activity

2010-04-19 Thread Tom F M White



On 19/04/2010 21:36, Mark Murphy wrote:

Tom F M White wrote:
   

Further to my previous email, I have sketched out how my Media Player
will work with the actual MediaPlayer object in a separate service,
MediaService. I can't work out how to make calls, and retrieve data from
the service. Following tutorials online has lead me to this:

MyApp.java - top level Activity, in onCreate():
...
MediaConnection conn = new MediaConnection();
StateApp state = (StateApp) getApplicationContext();
bindService( new Intent(this,MediaService.class), conn,
Context.BIND_AUTO_CREATE);
state.setMediaConnection(conn);
...
MediaView.java - View for playing media files, in play button onClick():
...
StateApp state = (StateApp) context.getApplicationContext();
MediaConnection conn = state.getMediaConnection();
conn.start(mediaLocation);
...

MediaConnection implements ServiceConnection:
public class MediaConnection implements ServiceConnection, MediaInterface{

  private Binder service;

  public void start(String loc){
  MediaInterface i = (MediaInterface)service;
  i.start(loc);
  }
  public void onServiceDisconnected(ComponentName cn){
  Log.i(INFO, Service unbound );
  }
  public void onServiceConnected(ComponentName cn, IBinder b){
  service = (Binder)b;
  Log.i(INFO, Service bound );
  }
}

Which throws a NullPointerException at i.start(loc). Can anyone tell me
what I'm doing wrong?

 

Off the cuff, I would guess that you are calling start() before Android
calls onServiceConnected().

   
I'm calling start a relatively long time later, several seconds. Do I 
need to call onServiceConnected() myself?


--
You received this message because you are subscribed to the Google
Groups Android Beginners group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en


Re: [android-beginners] Using Methods in a Service from an Activity

2010-04-19 Thread Tom F M White



On 19/04/2010 21:43, Mark Murphy wrote:

Tom F M White wrote:
   

I'm calling start a relatively long time later, several seconds. Do I
need to call onServiceConnected() myself?
 

No, it should be called shortly after you call bindService(). This
suggests that your bindService() call is failing for some reason (e.g.,
your service is not registered in your manifest).

Here is a sample project from one of my books showing the use of
bindService():

http://github.com/commonsguy/cw-android/tree/master/Service/WeatherPlus/

   
Some strange behaviour, the service is being started ok, and 
bindService() is returning true, yet onServiceConnected() never seems to 
be called, and I can't seem to find a reason.


--
You received this message because you are subscribed to the Google
Groups Android Beginners group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en