android-在先前活动中调用startService时,bindService失败

android-在先前活动中调用startService时,bindService失败,第1张

概述我不确定如何解决此问题,但是在一个Activity中,我调用startService,然后立即调用以启动下一个Activity.这可以正常工作,服务将启动,并开始按预期方式处理数据.我转到下一个活动,在onResume中,我调用AsyncTask来绑定服务.因此,基本流程是我调用AsyncTask,bindService返回false,因

我不确定如何解决此问题,但是在一个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;    }};

解决方法:

当您调用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;    };};@OverrIDepublic 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();    }
总结

以上是内存溢出为你收集整理的android-在先前活动中调用startService时,bindService失败全部内容,希望文章能够帮你解决android-在先前活动中调用startService时,bindService失败所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1072382.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存