Android AIDL 的简单使用

Android AIDL 的简单使用

  • 本文完整项目
  • 文档结构
  • 一端
  • 另一端
  • 实现思路
  • 成功截图

本文完整项目

链接:https://download.csdn.net/download/yancr/11082932

文档结构

一端

Android AIDL 的简单使用_第1张图片

另一端

Android AIDL 的简单使用_第2张图片

一端

// Person.aidl
package com.ycr.aidlserver;

parcelable Person;
// PersonManager.aidl
package com.ycr.aidlserver;

// Declare any non-default types here with import statements
// int、long、char、boolean、double
// String、CharSequence
// List Map  must be ArrayList HashMap
// Parcelable
import com.ycr.aidlserver.Person;

interface PersonManager {
     void add(in Person p);
     List getList();
}




	
		
			
				
				
			
		
		
			
				
				
			
		
	


package com.ycr.aidlserver;

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

public class Person implements Parcelable {
	private String name;
	public Person() {}
	public Person(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	protected Person(Parcel in) {
		name = in.readString();
	}
	public static final Creator CREATOR = new Creator() {
		@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);
	}
}

package com.ycr.aidlserver;

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

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

public class PersonManageService extends Service {
	private ArrayList list;
	private final PersonManager.Stub personManager = new PersonManager.Stub() {
		@Override
		public void add(Person p) throws RemoteException {
			synchronized (this) {
				if(list == null) {
					list = new ArrayList<>();
				}
				if(p == null) {
					p = new Person("default");

				}
				list.add(p);
				Log.e("aidlserver", p.getName());
			}
		}
		@Override
		public List getList() throws RemoteException {
			return list;
		}
	};
	public PersonManageService() {
	}
	@Override
	public void onCreate() {
		super.onCreate();
		Log.e("aidlserver", "PersonManageService onCreate");
		list = new ArrayList<>();
	}
	@Override
	public IBinder onBind(Intent intent) {
		return personManager;
	}
}

另一端

package com.ycr.aidlclient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.ycr.aidlserver.Person;
import com.ycr.aidlserver.PersonManager;

import java.util.List;

public class MainActivity extends AppCompatActivity {
	private Button addBtn, listBtn;
	private PersonManager personManager;
	private boolean connect = false;
	private ServiceConnection serviceConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			personManager = PersonManager.Stub.asInterface(service);
			connect = true;
		}
		@Override
		public void onServiceDisconnected(ComponentName name) {
			connect = false;
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setTitle("Client");
		Log.e("aidlclient", "MainActivity onCreate");
		addBtn = findViewById(R.id.addBtn);
		listBtn = findViewById(R.id.listBtn);
		Listener l = new Listener();
		addBtn.setOnClickListener(l);
		listBtn.setOnClickListener(l);
		Intent intent = new Intent();
		intent.setPackage(getString(R.string.server_package)); //此处 Package 取的是 Server的gradle中的 applicationId
		intent.setAction(getString(R.string.server_action));
		boolean b = bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
		Log.e("aidlclient", String.valueOf(b));
	}
	class Listener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			if(v.equals(addBtn)) {
				if(connect) {
					Person p = new Person("Client");
					try {
						personManager.add(p);
					} catch (RemoteException e) {
						e.printStackTrace();
					}
				}
			} else if(v.equals(listBtn)) {
				try {
					List list = personManager.getList();
					StringBuilder stringBuilder = new StringBuilder();
					for(Person p : list) {
						stringBuilder.append(p.getName()).append('\t');
					}
					Toast.makeText(MainActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.e("aidlclient", "MainActivity onDestroy");
		if(connect) {
			unbindService(serviceConnection);
		}
	}
}

实现思路

使用 AIDL (Android 接口定义语言)定义两端通信的接口。此处为 PersonManager。

“另一端”通过该接口 bind “一端”的服务,并通过接口调用函数进行相关操作。

一些注意事项写在了注释中。文章中未列出的文件均无特殊修改。

成功截图

两次 add 之后 list
Android AIDL 的简单使用_第3张图片
一端
Android AIDL 的简单使用_第4张图片
Android AIDL 的简单使用_第5张图片

你可能感兴趣的:(Android)