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

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

问题描述

我用 AIDL 接口堵了一堵墙.我有一个必须通过第 3 方应用程序控制的应用程序(我对此有足够的控制权,因此我可以要求他们在他们的活动中实施我需要的任何内容)

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.

现在我想要一种从服务请求方法调用的方法(主要是启动、停止、sendData).我已经为这两个应用程序创建了 .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)

计划是使用这种方法向服务发送一个字符串,只需切换预定义的字符串即可调用正确的方法(如果能提高使用率,也可以只实现三种不同的方法).

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

{act=foo.testapp.IScript } : 未找到

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

我认为这个猜测与我的误解有关,即.包名左右)

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);

在服务类我有这个;

@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.

我的 Android 生活

一切正常,只需复制粘贴源代码并正确更改包名和函数名(假设您将其实现到自己的项目中)

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.

更新:请查看更新的 Android SDK 1.5 示例程序."

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

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

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