标签:
AIDL
是 Android
Interface definition language
的缩写,它是一种Android
内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口
1.继承Service
public class PushService extends Service{ @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } }
2.在AndroidManifest.xml里注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="name.quanke.aidldemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".App"
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=".PushService"
android:enabled="true"
android:process=":push"
android:exported="true">
</service>
</application>
</manifest>
|
interface IHandler { void connect(); }aidl生成后的样子
public class PushClient { private IHandler iHandler; private static PushClient instance = new PushClient(); public static PushClient getInstance() { return instance; } public void init(Application app){ Intent binderIntent = new Intent(app,PushService.class); app.bindService(binderIntent, serviceConnection, Context.BIND_AUTO_CREATE); } private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { iHandler = IHandler.Stub.asInterface(service); //连接成功调用 } @Override public void onServiceDisconnected(ComponentName name) { //断开连接调用 } }; //通过AIDL远程调用 public void connect(){ try { iHandler.connect(); } catch (RemoteException e) { e.printStackTrace(); } } }
public class LibHandler extends IHandler.Stub{ @Override public void connect() throws RemoteException { } @Override public IBinder asBinder() { return null; } }
public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } private void init() { findViewById(R.id.aidl_test).setOnClickListener(this); } @Override public void onClick(View view) { PushClient.getInstance().connect(); } }
public class Message implements Parcelable { private long id; private String content; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "Message{" + "id=" + id + ", content='" + content + '\'' + '}'; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeLong(this.id); dest.writeString(this.content); } protected Message(Parcel in) { this.id = in.readLong(); this.content = in.readString(); } public static final Creator<Message> CREATOR = new Creator<Message>() { @Override public Message createFromParcel(Parcel source) { return new Message(source); } @Override public Message[] newArray(int size) { return new Message[size]; } }; }
interface IHandler { void connect(); void sendMessage(Message message); }
sendMessage
方法的message
参数是纯粹的输入参数—这意味着是从客户端到服务器的数据,你需要在AIDL声明:void sendMessage(in Message message);如果
sendMessage
方法的message
参数是纯粹的输出-这意味着它的数据是通过从服务器到客户端,使用:void sendMessage(out Message message);如果
sendMessage
方法的message
参数是输入也是输出-客户端的值在服务可能会修改,使用:void sendMessage(inout Message message);我们这里是客户端范围服务端的数据,所以用in
interface IHandler { void connect(); void sendMessage(in Message message); }
标签:
原文地址:http://blog.csdn.net/xiangzhihong8/article/details/52106386