Thursday, July 29, 2010

[android-developers] Re: bindService() does not create local service, but returns true

Please help anybody. I create a local service in the simplest possible
way, but when I bind to it, the reference remains null. Did I find a
bug anywhere? I am developing from Eclipse with lib and target
platform 2.1 update 1 (although other versions did not make a
difference).

My activity is like this:

public class ViewTrendActivity extends Activity {

private ConsumptionService consumptionService = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindService(new Intent(this, ConsumptionService.class),
serviceConnection, BIND_AUTO_CREATE);
String blah = consumptionService.doBlah();
}
}

This is the Service:

public class ConsumptionService extends Service {

public String doBlah() {
return "blah";
}

// stuff to make this an Android Service

private final Binder binder = new LocalBinder();

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

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

This is the definition in the Manifest:

<service android:name=".service.ConsumptionService" />

I want a local Service, so I do not define any process.

Is there anything missing here?

Jeroen

On 28 jul, 11:53, Jeroen Kransen <jer...@kransen.nl> wrote:
> Thanks, Mark!
>
> My code is basically the same as yours. I downloaded your project, and
> guess what, that too did not work with me! I'm sure it works for you,
> so I'm thinking my setup is broken somewhere.
>
> LogCat does not mention any service being started, neither with my,
> nor with your app. An error that I always get (with working apps as
> well, and often more than one) is this:
>
> 07-28 11:43:12.608: ERROR/AndroidRuntime(27800): ERROR: thread attach
> failed
>
> In debug mode, I see that the Service reference remains null in both
> our apps. However, I do see lots of logging related to GPS, so I think
> the Service does run. I just don't get a reference to it.
>
> I really feel lost now. It should not be this difficult to do
> something this simple...
>
> Jeroen
>
> On 28 jul, 05:18, Mark Murphy <mmur...@commonsware.com> wrote:
>
> > Examine LogCat and see if you have any warnings.
>
> > Also, in case it helps, here is a sample project demonstrating the technique:
>
> >http://github.com/commonsguy/cw-android/tree/master/Service/WeatherPlus/
>
> > On Tue, Jul 27, 2010 at 10:54 PM, Jeroen Kransen <jer...@kransen.nl> wrote:
> > > I want to use a local service to fence off some logic in a singleton
> > > class. From my Activities I try to bind to it. The method
> > > bindService() returns true, but the Service onCreate() is never
> > > called, nor is the ServiceConnector's onServiceConnected().
>
> > > Please tell me what part I am missing.
>
> > > Here is my manifest snippet (note that I do not set :remote, as I want
> > > the service to be local):
>
> > > <service android:enabled="true"
> > > android:name=".service.ConsumptionService"/>
>
> > > This is how I call the ServiceConnector and the Service:
>
> > >                Intent serviceIntent = new Intent(this, ConsumptionService.class);
> > >                ConsumptionServiceConnection serviceConnection = new
> > > ConsumptionServiceConnection();
> > >                boolean isBound = bindService(serviceIntent, serviceConnection,
> > > Context.BIND_AUTO_CREATE);
>
> > > Especially the "BIND_AUTO_CREATE" part should to the creation,
> > > according to the documentation.
>
> > > This is the Service itself:
>
> > >        @Override
> > >    public void onCreate() {
> > >                super.onCreate();
> > >                measurementDao = new MeasurementDaoImpl(this);
> > >                calculator = new TrendCalculatorImpl(measurementDao);
> > >        }
>
> > >        @Override
> > >        public IBinder onBind(Intent intent) {
> > >                return binder;
> > >        }
>
> > >        public class LocalBinder extends Binder {
> > >        ConsumptionService getService() {
> > >            return ConsumptionService.this;
> > >        }
> > >    }
>
> > >        private final IBinder binder = new LocalBinder();
>
> > --
> > Mark Murphy (a Commons Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> > _Android Programming Tutorials_ Version 2.9 Available!

--
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