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