两个应用程序之间AIDL接口 [英] AIDL interface between two applications

查看:158
本文介绍了两个应用程序之间AIDL接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持共墙的AIDL接口。我有一个应用程序,它具有通过第三方应用程序进行控制(我有足够的控制这个,所以我可以让他们实现什么都我需要为他们的活动)

I've stuck a total wall with the AIDL interfacing. I've an app which has to be controlled via 3rd party application (I've enough control over this so I can ask them to implement what ever I need into their activity)

本来我的应用程序也有接口和一切活动,但我已经改变了它是一个后台服务,并进行测试,我创建了一个虚拟应用程序,它管理启动服务的应用程序的背景。

Originally my app was also an activity with interface and everything but I've changed it to be a background service and for testing, I created a dummy app which manages to start the service app to the background.

现在我想的方式来请求服务的方法调用(主要是,启动,停止,送出数据)。我已经创建了.aidl文件,这两个应用程序。该AIDL文件实现只有一个方法(这是礼貌这里一些其他的问题。)

Now I would like a way to request method calls from the service (mainly; start, stop, sendData). I've created the .aidl files for both apps. The aidl file implements only one method (this is courtesy of some other question here.)

package foo.testapp;
interface IScript 
{
     String executeScript(String script); 
}

,而另一个AIDL是相同,除了包是foo.otherapp。的实现,我在网上找到了这两个AIDL文件有相同的包,但对我来说这将导致错误(想这是对我而言只是一个问题,因为我讨厌命名空间和包装,所以我往往只是他们的名字不好,如果是很重要的改变他们,我可以做到这一点)

while the other aidl is same except the package is "foo.otherapp". The implementations I've found online had same package for both aidl files, but for me this causes an error (guess this is just a problem on my part since I hate namespaces and packages so I often just name them badly, if it's important to change them, I can do it)

该计划是使用这个方法来将字符串发送到服务,只是有超过$ P $开关pdefined字符串来调用正确的方法(也可以只实现三种不同的方法,如果它提高了使用)。

The plan was to use this method to send a string to the service and just have a switch over predefined strings to call a correct method ( could also just implement three different methods if it improves the usage).

反正......我不能让AIDL连接,​​我得到错误无法启动服务的意图

Anyway... I can't get the aidl to connect, I get error "Unable to start service intent

{行为= foo.testapp.IScript}:未找到

{act=foo.testapp.IScript } : not found

我想这个猜测有事情做与我的误解,即。 packagenames左右)

I would this guess has something to do with my misunderstandings ie. packagenames or so)

这是在我的测试中执行的活动应用

this is the implementation in my test activity app

private final IScript.Stub mBinder = new IScript.Stub()
{
    @Override
    public String executeScript(String script) throws RemoteException
    {
        // TODO Auto-generated method stub
    }
};
IScript mService = null;
private ServiceConnection mConnection = new ServiceConnection() 
{
     public void onServiceConnected(ComponentName className, IBinder service) 
     {
         mService = IScript.Stub.asInterface(service);
     }
     public void onServiceDisconnected(ComponentName className) 
     {
         mService = null;
     }
 };

然后在OnCreate中()方法,我会做这样的:

Then in OnCreate() method I'll do this:

bindService(new Intent(IScript.class.getName()),
            mConnection, Context.BIND_AUTO_CREATE);

在服务类中我有这样;

In service class I have this;

@Override
public IBinder onBind(Intent intent) 
{
    // Select the interface to return.  If your service only implements
    // a single interface, you can just return it here without checking
    // the Intent.
    if (IScript.class.getName().equals(intent.getAction())) 
    {
        return mBinder;
    }
    return null;
}

/**
 * The IRemoteInterface is defined through IDL
 */
private final IScript.Stub mBinder = new IScript.Stub() 
{
    @Override
    public String executeScript(String script) throws RemoteException 
    {
        if (script == "test")
        {
            return "foo";
        }
        return "fail";
    }
};

和最后的清单文件;

其实很好,我不知道如果我要在与AIDL处理添加的东西到清单文件。在一个例子中,我看到了这一点;

well actually, I've no idea if I have to add something into manifest files when dealing with the aidl. In the one example I saw this;

    <intent-filter>
        <action android:name="foo.otherapp.IScript" />
    </intent-filter>

    <intent-filter>
        <action android:name="foo.testapp.IScript" />
    </intent-filter>

我猜想,这些错误可以在任何地方。我一直在试图建立这种与口香糖和创可贴。猜猜我刚刚误解了这方面的一些基本概念。

I would guess that the errors could be anywhere. I've been trying to set this up with chewing gum and band-aids. Guess I've just misunderstood some basic concept of this.

总之,任何帮助是值得欢迎的。

Anyway, any help is welcome.

在此先感谢!

推荐答案

我决定回答我的问题,因为我找到了一个精确解。

I decided to answer my own question since I found an exact solution.

我的生活的Andr​​oid

一切工作只是复制粘贴源和正确更改包名和函数名(假设你实现这到您自己的项目)

Everything worked just by copy pasting the source and changing the package names and function names correctly (assuming you're implementing this into your own project)

从客户端文件夹来源前往客户端活动,serviceimpl去服务。我并不需要的服务活动,所以我离开它(和它并没有真正似乎反正调用)。

Source from client folder goes to the client activity and serviceimpl goes to service. I didn't need the 'Service activity', so I left it out ( and it doesn't really seem to be invoked anyway).

我没有足够的信誉来发布多个链接,这样你就可以从页面顶部的来源。

I don't have enough reputation to post multiple links, so you can get the source from the top of the page.

更新:请检查出的Andr​​oid SDK 1.5的更新示例程序

"Update: please check out the updated example program for Android SDK 1.5."

这篇关于两个应用程序之间AIDL接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆