0

I have a service component (common task for all my apps), which can be invoked by any of the apps. I am trying to access the service object from the all activities, I noticed that the one which created the service [startService(intent)] has the right informaion. But rest does not get the informaion needed. My Code is as below:

// Activity.java
public void onCreate(Bundle savedInstanceState) {
    ...

    Intent intent = new Intent (this.context, Service.class) ;
    this.context.startService(intent) ;
    this.context.bindService(intent, this, Context.BIND_AUTO_CREATE) ;
    ...
    String result = serviceObj.getData() ;
}

public void onServiceConnected(ComponentName name, IBinder service) {
    serviceObj = ((Service.LocalBinder)service).getService();
    timer.scheduleAtFixedRate(task, 5000, 60000 ) ;
}



// Service.java

private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {
    Service getService() {
        return Service.this;
    }
}

public void onCreate() {
    super.onCreate();
    context = getApplicationContext() ;
}

public void onStart( Intent intent, int startId ) {

... some processing is done here...

}

public IBinder onBind(Intent intent) {
    return mBinder;
}

If I invoke startService(intent). it creates a new service and runs in parallel to the other service.

If I don't invoke startService(intent), serviceObj.getData() retuns null value.

Can any one enlighten me where have I gone wrong.

Any kind of pointer will be very useful..

Thanks and regards, Vinay

Vinay
  • 2,395
  • 3
  • 30
  • 35

1 Answers1

7

If I invoke startService(intent). it creates a new service and runs in parallel to the other service.

No, it does not. There will be at most one instance of your service running.

If I don't invoke startService(intent), serviceObj.getData() retuns null value.

startService() has nothing to do with it, from my reading of your code. You are attempting to use serviceObj in onCreate(). That will never work. bindService() is an asynchronous call. You cannot use serviveObj until onServiceConnected() is called. onServiceConnected() will not be called until sometime after onCreate() returns.

Also:

  • While there are cases when you might need both startService() and bindService(), they are not both needed in the normal case.
  • Do not use getApplicationContext().
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the response. It helped me. Regarding the service running in parallel to the other, I found that this was due to "new Intent (this.context, Service.class)". I replaced this with "new Intent ("com.package", "com.package.service");" Thanks for the pointers.. – Vinay Aug 06 '10 at 06:48
  • You are saying "Do not use getApplicationContext". So, would that apply to the accepted answer of http://stackoverflow.com/questions/3141632/android-service-interacting-with-multiple-activities also? I the answer "wrong" / against the rules one there? – OneWorld Jul 04 '13 at 14:26
  • @OneWorld: I am definitely not a fan of the "stick an `Activity` in the `Application` to track the "current" activity" pattern. If nothing else, a static data member works just as well and with greater flexibility. Beyond that, preventing an `Activity` from being garbage collected is a prescription for trouble. – CommonsWare Jul 04 '13 at 15:00