开发者

Android: bindService won't connect to remote (AIDL) service, and i've no idea why

I'm trying to develop开发者_JS百科 a basic AIDL service on Android 2.2.1. Everything seems to build and install ok, but bindService() just won't -- well, bind. My ServiceConnection class isn't called. I've really no idea why not, so any help would be appreciated. Here is my client activity:

public class go extends Activity {
    protected static final String TAG = "HOSPlayerClient";
    private IHOSPlayerService hosPlayerService = null;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.v(TAG, "onServiceConnection");
            hosPlayerService = IHOSPlayerService.Stub.asInterface(service);
            callService();
        }

        public void onServiceDisconnected(ComponentName name) {
            Log.v(TAG, "onServiceDisconnected");
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.v(TAG, IHOSPlayerService.class.getName());
        boolean bound = bindService(
                new Intent(IHOSPlayerService.class.getName()),
                serviceConnection, Context.BIND_AUTO_CREATE);

        Log.v(TAG, bound ? "service bound" : "service bind failed");
    }

    private void callService() {
        try {
            hosPlayerService.go();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

... and here are what I believe to be the relevant parts of my AIDL service:

public class HOSPlayerService extends Service
{
 private static final String TAG = "HOSPlayerService";

 public class HOSPlayerServiceImpl extends IHOSPlayerService.Stub
 {
  public void go() throws RemoteException
  {
   Log.v(TAG, "go called");
  }
 }

 @Override
 public IBinder onBind(Intent intent) 
 {
  // TODO Auto-generated method stub
  return new HOSPlayerServiceImpl();
 }
}

... and the AIDL file:

package com.HOS.ahos.HOSPlayerService;

interface IHOSPlayerService
{
 void go();
}


Try to put the following code in the service file -

@Override
public IBinder onBind(Intent arg0) {
    //arg0.getExtras();
    return binder;
}

Service should return binder, then only your MyServiceConnection class will be called.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜