王学岗csdn 移动架构(3)————Android Binder机制

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
看代码目录
在这里插入图片描述
服务端代码

// DNAIdl.aidl
package com.dn_alan.service;

// Declare any non-default types here with import statements
import com.dn_alan.service.Person;

interface DNAIdl {
    void addPerson(in Person person);
    List<Person> getPersonList();
}

// DNAIdl.aidl
package com.dn_alan.service;

// Declare any non-default types here with import statements
parcelable Person;
package com.dn_alan.service;

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

import java.util.ArrayList;
import java.util.List;

public class DNAidlService extends Service {
    private ArrayList<Person> personArrayList;

    @Override
    public IBinder onBind(Intent intent) {
        personArrayList = new ArrayList<>();
        return iBinder;
    }

    private IBinder iBinder = new DNAIdl.Stub() {
        @Override
        public void addPerson(Person person) throws RemoteException {
            personArrayList.add(person);
        }

        @Override
        public List<Person> getPersonList() throws RemoteException {
            return personArrayList;
        }
    };

}

package com.dn_alan.service;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, DNAidlService.class));
    }
}

package com.dn_alan.service;

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

public class Person implements Parcelable {
    private String name;
    private  int grade;

    protected Person(Parcel in) {
        name = in.readString();
        grade = in.readInt();
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

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

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

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


<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".DNAidlService"
        android:exported="true"/>
</application>
```

客户端代码

// DNAIdl.aidl
package com.dn_alan.service;

// Declare any non-default types here with import statements
import com.dn_alan.service.Person;

interface DNAIdl {
void addPerson(in Person person);
List getPersonList();
}


// DNAIdl.aidl
package com.dn_alan.service;

// Declare any non-default types here with import statements
parcelable Person;


package com.dn_alan.myapplication;

import android.content.ComponentName;
import android.content.Context;
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.util.Log;
import android.view.View;
import android.widget.Toast;

import com.dn_alan.service.DNAIdl;
import com.dn_alan.service.Person;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private DNAIdl dnaIdl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindService();
    }

    private void bindService() {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(
                "com.dn_alan.service",
                "com.dn_alan.service.DNAidlService"));
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            dnaIdl = DNAIdl.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public void click(View view) {
        try {
            dnaIdl.addPerson(new Person("dn", 10));
            List<Person> people = dnaIdl.getPersonList();
            Toast.makeText(this, people.toString(), Toast.LENGTH_SHORT).show();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}


package com.dn_alan.service;

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

public class Person implements Parcelable {
private String name;
private int grade;

public Person(String name, int grade) {
    this.name = name;
    this.grade = grade;
}

public Person(Parcel in) {
    name = in.readString();
    grade = in.readInt();
}

public static final Creator<Person> CREATOR = new Creator<Person>() {
    @Override
    public Person createFromParcel(Parcel in) {
        return new Person(in);
    }

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

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

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

@Override
public String toString() {
    return "Person{" +
            "name='" + name + '\'' +
            ", grade=" + grade +
            '}';
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值