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

穿精又带淫゛_ 提交于 2020-02-04 02:29:51

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

// 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 +
            '}';
}

}


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!