Android Studio AIDL 的使用 。

  1. 新建一个工程 ,包含app(server) 跟client两个module
  2. 在server端 new -》 aidl 建立Girl.aidl
// Girl.aidl
package so.com.testserver;

parcelable Girl;
  1. 建立girl类
package so.com.testserver;

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

/**
 * Created by Administrator on 2015/7/17.
 */
public class Girl implements Parcelable {
    String name;
    int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    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];
        }
    };
}
  1. build 建立halow.aidl
// halow.aidl
package so.com.testserver;
import so.com.testserver.Girl;

// Declare any non-default types here with import statements

interface halow {
String sayHello();
Girl GetGirl();
}
  1. build -> rebuild project
  2. Aidlservice 类
package so.com.testserver;

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

public class Aidlservice extends Service {
    public Aidlservice() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return binder;
    }
    halow.Stub binder=new halow.Stub() {
        @Override
        public String sayHello() throws RemoteException {
            return "hellow i am adam";
        }

        @Override
        public Girl GetGirl() throws RemoteException {
            Girl girl=new Girl();
            girl.setAge(19);
            girl.setName("小李欢");
            return girl;
        }
    };
}
  1. androidManifest.xml 配置 service (intent-filter)

        <service
            android:name=".Aidlservice"
            android:enabled="true"
            android:process=":remote"
            android:exported="true" >
            <intent-filter>
                <action android:name="so.com.testserver.Aidlservice"></action>
            </intent-filter>
        </service>
  1. 新建一个testclient客户端
    9.将server中的 Girl.aidl 跟 halow.aidl拷贝到以及Girl.java拷贝到client module中 ,注意 包名要相同 。
    10.测试client MainActivity.java
package so.com.testclient;

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.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import so.com.testserver.Girl;
import so.com.testserver.halow;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    Button button;

       halow halows;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= (TextView) findViewById(R.id.tv);
        button= (Button) findViewById(R.id.btn1);
       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent("so.com.testserver.Aidlservice");
               bindService(intent,connection,BIND_AUTO_CREATE);
           }
       });

    }

    ServiceConnection connection=new ServiceConnection() {
        String content;
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            halows=halow.Stub.asInterface(service);
            try {
                content=halows.sayHello()+"/n";
                Girl mygirl=halows.GetGirl();
                content+=content+mygirl.getName();
                textView.setText(content);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

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

        }
    };


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值