绑定到服务并执行一些方法?

时间:2011-03-10 11:14:59

标签: android ipc aidl

好的,我明白了,当你绑定到远程Service时,它将不会绑定,直到你从回调返回。我需要绑定到一个服务并立即执行一些方法。有什么办法吗?我的代码如下:

try {
            Parser parser = FrameworkBridge.getFrameworkBridge(this).getParser();
            if (parser != null) {
                for (Article a : parser.getArticlesList("http://www.bt.dk/mecommobile/latest/news_article/%s/%s?output_type=xml", 1379, 20)) {
                    listAdapter.add(a);
                }
            }
        } catch (RemoteException e) {
            Log.d(TAG, "Service communication failure");
        } catch (FrameworkNotInstalledException e) {
            Log.d(TAG, "No framework installed. Install it!");
        }

此处FrameworkBridge.getFrameworkBridge(this).getParser()执行所有服务连接例程并返回远程接口。问题是 - 当我在上面的代码中时,连接尚未执行,因此parsernull。如何在退出代码之前将其连接?

2 个答案:

答案 0 :(得分:0)

mContext.bindService( new Intent("name of the class"), this, Context.BIND_AUTO_CREATE);

上面的方法调用您存储的上下文副本应该在您需要时自动绑定服务。我不是100%肯定,但这应该会帮助你。

答案 1 :(得分:0)

onServiceConnected(..)将告诉您何时连接服务并建立远程接口。在触发此操作之前,请勿尝试调用服务中的任何方法。

相关问题