Android Studio 使用AIDL

最近在研究AIDL,看了好多文章都是在eclipse下面进行完成的,对于喜欢用as的我来讲决定在Android Studio下面实现。中间遇到很多麻烦,最后经过猜测和尝试还好解决了。我是这么作的。html

在eclipse里面操做时aidl文件个java文件都放在一个包下, 客户端直接将该包复制到本身的目录下,而后能够另外建另一个包放其余代码。但在android studio下面这样是不能够的,须要在src单独建一个AIDL文件夹,将aidl文件放在里面,java文件在另外的包下,这样就致使服务端项目与客户端项目的包名必须相同在as中project至关于es的workspace,moudle至关于es的project,在eclipse里面是两个project在通讯,so 我猜想在as中是两个mould在通讯,所以建了一个project(里面自带一个app module),让app moulde做为客户端而后又另外加了一个服务端moudle 叫aidlserver。在aidlserver的project视图下面的src下面右键new 选择AIDL ,AIDL Folder ,而后将本身的aidl文件放入其中。java

下面是一个简单的例子android

aidlserver的Girl.aidlapp

package com.test.huangxingli.aidlserver;
parcelable Girl;

这是AIDLServerService.aidl eclipse

// AIDLServerService.aidl
package com.test.huangxingli.aidlserver;
import com.test.huangxingli.aidlserver.Girl;
// Declare any non-default types here with import statements


interface AIDLServerService {

            String sayHello();
            Girl getGirl();
}


建好这两个文件后再编写Girl.java .注意要先写Girl.aidl而后再写Girl.java 刚开始时 我先写的Girl.java 而后再写Girl.aidl时提示不能建立重名的文件。ide

Girl.java以下:this

package com.test.huangxingli.aidlserver;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by huangxingli on 2015/3/27.
 */
public class Girl  implements Parcelable{

    String name;
    int age;

    public Girl() {

    }

    public String getName() {

        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);

    }

    public static final Creator<Girl> CREATOR=new Creator<Girl>() {
        @Override
        public Girl createFromParcel(Parcel source) {
            Girl girl=new Girl();
            girl.setName(source.readString());
            girl.setAge(source.readInt());
            return girl;
        }

        @Override
        public Girl[] newArray(int size) {
            return new Girl[size];
        }
    };
}


而后写Service类MAIDLServerService,以下:spa

package com.test.huangxingli.aidlserver;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;


public class MAIDLServerService extends Service {

    public MAIDLServerService() {
    }


    AIDLServerService.Stub binder=new AIDLServerService.Stub() {

        @Override
        public String sayHello() throws RemoteException {
            return "hello, i am from AIDLServerService";
        }

        @Override
        public Girl getGirl() throws RemoteException {
            Girl girl=new Girl();
            girl.setAge(25);
            girl.setName("lily");
            return girl;
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
       return binder;
    }
}



而后在manifest里面将该Service注册一下:以下.net

<service
            android:name="com.test.huangxingli.aidlserver.MAIDLServerService"
            android:process=":remote"
             >
            <intent-filter>
                <action android:name="com.test.huangxingli.aidlserver.MAIDLServerService"></action>
            </intent-filter>
        </service>

intent-filter的做用是便于隐式调用该Service.设计

as项目建立时的MainActivity.java类,里面没作任何处理。

下面建客户端moulde,注意必定要将服务项目端的aidl文件夹复制到相应位置,以前我不是复制的,是新建的,包名类名都相同,可依然会编译不过,后来将本身建的删掉,而后复制过来就编过了,善意提醒一下,我在这浪费了很多时间,本身建的即便名字相同也编不过,各位仍是copy吧。

而后在客户端下面将Girl.java也copy过来,再次提醒一下,客户端的有Girl.java的包名必定要与服务端有Girl.java的包名同样奥,不然提示找不到。

下面是个人客户端MainActivity.java的代码:

package com.test.huangxingli.aidlserver;


import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {


    TextView textView;
    Button button;
    AIDLServerService aidlServerService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button= (Button) findViewById(R.id.button);
        textView= (TextView) findViewById(R.id.textView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent("com.test.huangxingli.aidlserver.MAIDLServerService");
                bindService(intent,connection,BIND_AUTO_CREATE);
            }
        });


    }


    ServiceConnection connection=new ServiceConnection() {


        String content;
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            aidlServerService=AIDLServerService.Stub.asInterface(service);
            try {
                content=aidlServerService.sayHello()+"\n";
                Girl girl=aidlServerService.getGirl();
                content +="my name is "+girl.getName();


                textView.setText(content);


            } catch (RemoteException e) {
                e.printStackTrace();
            }


        }


        @Override
        public void onServiceDisconnected(ComponentName name) {
            aidlServerService=null;
        }
    };

}


好了到此就所有处理好了,运行一下吧。

注:此时删掉aidlserver mould继续运行程序,依然能正常运行。可是若是直接在一个Mould中生成后使用是不能够达到效果的。看来对于AIDL这种技术的设计意图,使用场景仍是要去了解一下。

源码下载:点击打开连接

附:aidl在项目中的位置:

相关文章
相关标签/搜索