Executing the function calls in onServiceConnected() method is not what is wanted as described in following case:
There is a Service Wrapper class where the binding/unbinding is actually done. The MyServiceClient is an Activity class used for invoking some functions on the service using the Service Wrapper class, instead of directly binding/unbinding to service. In this case, a service client using the wrapper class cannot execute the function calls on the service in onServiceConnected() method, which is in a different file. The NullPointerException is therefore still thrown when any function is invoked on the service after initializing the service wrapper. ------------------------------------------- CODE SNIPPET (UPDATED) ------------------------------------------- //class MyServiceClient in separate file - Used for accessing the service via a wrapper class class MyServiceClient extends Activity { public void onCreate(Bundle savedInstanceState) { //some lines ServiceWrapperClass svc = new ServiceWrapperClass(getApplicationContext()); //access the service Integer someValue = svc.getService().someFunc(1, 2); // NullPointerException here - How to make sure service should be running before any function is called on the service ? } } //class ServiceWrapperClass in separate file - Used for binding to service and providing the service instance to client class ServiceWrapperClass { private static ISomeServiceInterface myService; //constructor public ServiceWrapperClass(Context context) { //create intent to connect to service Intent serviceIntent = new Intent("some.uri"); context.bindService(serviceIntent, progressBarServiceConn, Context.BIND_AUTO_CREATE); //access the service Integer someValue = myService.someFunc(1, 2); // NullPointerException here - How to make sure service should be running before any function is called on the service ? } private ServiceConnection progressBarServiceConn = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { myService = ISomeServiceInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { myService = null; } }; public ISomeServiceInterface getService() { return myService; } } On Jul 21, 3:16 pm, Mark Murphy <mmur...@commonsware.com> wrote: > On Thu, Jul 21, 2011 at 1:26 AM, animeshsi <animeshs...@gmail.com> wrote: > > The issue is that I need to call the service methods as soon as it is > > connected. > > So, Is there any way by which I can execute the functions of service > > JUST AFTER binding is complete ? > > Execute those "functions" in onServiceConnected(). > > -- > Mark Murphy (a Commons > Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy > > Android Training in London:http://bit.ly/smand1,http://bit.ly/smand2 -- 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