在上一个活动中调用startService时,bindService失败

时间:2012-02-06 04:09:52

标签: android service

我不确定如何解决这个问题,但是在一个Activity中我调用了startService,然后立即调用以启动下一个Activity。

这样可行,服务启动,并开始按预期处理数据。

我转到下一个活动,并在onResume中呼叫AsyncTask绑定服务。

因此,基本流程是我调用AsyncTask,bindService返回false,因此永远不会调用mConnection。

所以,问题是为什么bindService返回false?

我将绑定放在AsyncTask中的原因是我在绑定之前让线程休眠了10秒,看看是否需要先启动服务。

我也在这个活动中启动了服务,首先是在onCreate方法中,所以等了10秒,但是bindService仍然返回false。

private class BindServiceTask extends AsyncTask<Void, Void, Boolean> {
    protected Boolean doInBackground(Void... params) {
        return bindService(
                new Intent(IMyCallback.class.getName()),
                mConnection, Context.BIND_AUTO_CREATE);
    }
    protected void onPostExecute(Boolean b) {
        if (b) {
            Log.i(TAG, "onResume - binding succeeded");
            Toast.makeText(mContext, "Bound to service succeeded",
                    Toast.LENGTH_LONG).show();
        } else {
            Log.i(TAG, "onResume - binding failed");
            Toast.makeText(mContext, "Bound to service failed",
                    Toast.LENGTH_LONG).show();
        }
    }
}
private IMyCallback mCallback = new IMyCallback.Stub() {
    @Override
    public void dataChanged(double[] info) throws RemoteException {
        mHandler.sendMessage(mHandler.obtainMessage(LOCATION_MSG, info));
    }
};
IMyService mIRemoteService;
private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        Log.i(TAG, "onServiceConnected");
        mIRemoteService = IMyService.Stub.asInterface(service);
        try {
            Log.i(TAG, "registering callback");
            mIRemoteService.registerCallback(mCallback);
        } catch (RemoteException e) {
            Log.e(TAG, e.toString());
        }
    }

    public void onServiceDisconnected(ComponentName className) {
        Log.e(TAG, "Service has unexpectedly disconnected");
        mIRemoteService = null;
    }
};

2 个答案:

答案 0 :(得分:2)

问题似乎是清单出现问题。

我错过了intent-filter来告诉系统可以绑定哪些服务:

    <service
        android:name=".MyService"
        android:process=":remote" >
        <intent-filter>

            <!--
                 These are the interfaces supported by the service, which
                 you can bind to.
            -->
            <action android:name="my.com.services.IMyCallback" />
            <action android:name="my.com.services.IMySecondaryService" />
            <!--
                 This is an action code you can use to select the service
                 without explicitly supplying the implementation class.
            -->
            <action android:name="my.com.activity.MY_SERVICE" />
        </intent-filter>
    </service>

答案 1 :(得分:1)

当您致电bindService()时,它不一定会返回您的服务连接或API来访问该服务。它是异步发生的。你需要这样的回调:

class PictureUploadQueueServiceConnection implements ServiceConnection {
    public void onServiceConnected(ComponentName name, IBinder service){
        Log.d(TAG, "PictureUpload Service Connected!");            
        pictureUploadQueueApi = PictureUploadQueueServiceApi.Stub.asInterface(service);       
    }

    public void onServiceDisconnected(ComponentName name){
        Log.d(TAG, "PictureUpload Service Connection Closed!");
        pictureUploadQueueApi = null;
    }
};

你做绑定的电话应该是这样的:

getApplicationContext().bindService(new Intent("org.me.xxxx.PictureUploadQueueServiceApi"),
                        pictureUploadQueueServiceConnection,
                        Context.BIND_AUTO_CREATE
                );

在您的服务中确保您正在实施API Stub并在onBind()方法中返回它的实例:

private PictureUploadQueueServiceApi.Stub api = new PictureUploadQueueServiceApi.Stub() {
    @Override
    public void queuePictureUpload(String remoteURI, String localURI, String target, String description, String callback) throws RemoteException {
        appendPictureUpload(remoteURI, localURI, target, description, callback);
    }
    @Override
    public boolean isEmpty() {
        return queue.size() == 0 ? true : false;
    };
};

@Override
public IBinder onBind(Intent intent) {
    Log.d(TAG, "Bound Intent: " + intent);
    return api;
}

最后,为了完成该示例,我的示例的AIDL文件如下所示:

interface PictureUploadQueueServiceApi {

        void queuePictureUpload(String remoteURI, String localURI, String target, String description, String callback);

        boolean isEmpty();

    }