首页 > 移动平台 > 详细

Android Studio 使用AIDL

时间:2015-03-27 17:46:55      阅读:330      评论:0      收藏:0      [点我收藏+]

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

在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文件放入其中。

下面是一个简单的例子

aidlserver的Girl.aidl

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

这是AIDLServerService.aidl 

// 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时提示不能创建重名的文件。

Girl.java如下:

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,如下:

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注册一下:如下

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

}

好了到此就全部处理好了,运行一下吧。

源码下载:点击打开链接

附:aidl在项目中的位置:

技术分享



Android Studio 使用AIDL

原文:http://my.oschina.net/u/1015229/blog/392809

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!