In case it would help, here is the relevant code:
The two classes (LocalServiceBinder and LocalService) used to bind to
the service and obtain a reference to it:
-----------------------------------------------------------------
package localservice;
import android.os.Binder;
public abstract class LocalServiceBinder extends Binder{
public abstract LocalService getLocalService();
}
-----------------------------------------------------------------
package localservice;
public interface LocalService{
public String getPid();
}
-----------------------------------------------------------------
A sample activity in APK #1 that binds with the service and also
implements the ServiceConnection interface
-----------------------------------------------------------------
package kanonov.t1;
import localservice.LocalService;
import localservice.LocalServiceBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class T1 extends Activity implements ServiceConnection{
public static final String TAG = "Test";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "T1 onCreate");
bindService(new Intent("kanonov.localservice"), this,
BIND_AUTO_CREATE);
}
@Override
public void onServiceConnected(ComponentName name, IBinder service){
Log.d(TAG, "T1 onServiceConnected " +
android.os.Process.myPid());
if (service == null){
try{
LocalServiceBinder lsb =
(LocalServiceBinder)service;
LocalService ls = lsb.getLocalService();
Log.i(TAG, "My PID is: " +
android.os.Process.myPid());
Log.i(TAG, "LocalService Process' PID is: " +
ls.getPid());
}catch(Exception e){
Log.e(TAG, "Error in connection: " +
e.getMessage(), e);
}
unbindService(this);
}else{
Log.e(TAG, "Binder is null!");
}
}
@Override
public void onServiceDisconnected(ComponentName name){}
}
The actual service in APK #2
-----------------------------------------------------------------
package kanonov.t2;
import localservice.LocalService;
import localservice.LocalServiceBinder;
import android.app.Service;
import android.content.Intent;
import android.util.Log;
public class LS extends Service implements LocalService{
public static final String TAG = "Test";
@Override
public LocalServiceBinder onBind(Intent intent){
Log.d(TAG, "LocalService onBind " + android.os.Process.myPid());
return new LocalServiceBinder(){
@Override
public LocalService getLocalService(){
Log.d(TAG, "T2LS getLocalService");
return LS.this;
}
};
}
@Override
public String getPid(){
Log.d(TAG, "LocalService getPid");
return "" + android.os.Process.myPid();
}
}
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---