Mark,
Thanks for the quick reply! Your hunch was correct - the service
hadn't started yet. In fact, even if I put my code in the onStart for
the activity, that's still not enough time. I finally placed the code
to grab the property out of my service in a Runnable and posted it to
run later and it worked.
public class RootActivity extends Activity
{
private TestServiceConnection gsc = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gsc = new TestServiceConnection();
Intent bindIntent = new Intent(this, TestService.class);
bindService(bindIntent, gsc, Context.BIND_AUTO_CREATE);
}
private final Handler handler = new Handler();
public void onStart()
{
super.onStart();
Runnable r = new Runnable()
{
public void run()
{
TextView tv =
(TextView)findViewById(R.id.mainText);
String p = "Unable to access TestService!";
if (gsc.testService != null)
{
p = gsc.testService.getAProperty();
}
tv.setText(p);
}
};
handler.postDelayed(r, 4000L);
}
}
Thanks,
- Mike
On Sep 14, 3:14 pm, Mark Murphy <[email protected]> wrote:
> Mike wrote:
> > Hello,
>
> > As a point of example for my problem, I wrote a single Activity which
> > binds to a service and then accesses a property to display. The
> > problem is that my TestServiceConnection.onServiceConnected method is
> > never called, and I don't know why.
>
> > Here is the code:
>
> > public class TestService extends Service
> > {
> > private final IBinder binder = new TestServiceBinder();
>
> > �...@override
> > public IBinder onBind(Intent intent)
> > {
> > return binder;
> > }
>
> > public String getAProperty()
> > {
> > return "Here's my property!";
> > }
>
> > public class TestServiceBinder extends Binder
> > {
> > TestService getService()
> > {
> > return TestService.this;
> > }
> > }
> > }
>
> > public class TestServiceConnection implements ServiceConnection
> > {
> > protected TestService testService = null;
>
> > �...@override
> > public void onServiceConnected(ComponentName name, IBinder service)
> > {
> > testService =
> > ((TestService.TestServiceBinder)service).getService();
> > }
>
> > �...@override
> > public void onServiceDisconnected(ComponentName name)
> > {
> > testService = null;
> > }
> > }
>
> > public class RootActivity extends Activity
> > {
> > private TestServiceConnection gsc = null;
>
> > �...@override
> > protected void onCreate(Bundle savedInstanceState)
> > {
> > super.onCreate(savedInstanceState);
> > setContentView(R.layout.main);
>
> > TextView tv = (TextView)findViewById(R.id.mainText);
>
> > gsc = new TestServiceConnection();
>
> > Intent bindIntent = new Intent(this, TestService.class);
> > bindService(bindIntent, gsc, Context.BIND_AUTO_CREATE);
>
> > String p = "Unable to access TestService!";
>
> > if (gsc.testService != null)
> > {
> > p = gsc.testService.getAProperty();
> > }
>
> > tv.setText(p);
> > }
> > }
>
> > My manifest contains the following line for the service:
>
> > <service android:enabled="true" android:name="com.wm.TestService" />
>
> > Finally, here's my main layout:
>
> > <?xml version="1.0" encoding="utf-8"?>
> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> > android"
> > android:orientation="vertical"
> > android:layout_width="fill_parent"
> > android:layout_height="fill_parent"
>
> > <TextView
> > android:layout_width="fill_parent"
> > android:layout_height="wrap_content"
> > android:id="@+id/mainText"/>
> > </LinearLayout>
>
> If you are determining that onServiceConnected() is not being called
> because of what your TextView shows, bear in mind that bindService() is
> asynchronous. AFAIK, it won't even *start* connecting until you exit
> onCreate(). You are better served updating your TextView as a result of
> onServiceConnected(), not at the bottom of onCreate().
>
> If you are determining that onServiceConnected() is not being called
> because of a breakpoint, or logging statements not shown above, perhaps
> your service is not registered in the manifest or something. You should
> see warnings or errors in logcat (via adb logcat, DDMS, or the DDMS
> perspective in Eclipse).
>
> --
> Mark Murphy (a Commons
> Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> _Beginning Android_ from Apress Now Available!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---