aild客户端java代码
package com.example.aidl_client;
import android.app.Service;
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.util.Log;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
IMyAidlInterface iMyAidlInterface;
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//绑定服务成功后,返回服务端返回的binder,将其转成IMyAidlInterface调用add方法
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
int num=iMyAidlInterface.add(4,6);//调用服务端的add方法并得到结果
// Toast.makeText(MainActivity.this, “”+num, Toast.LENGTH_SHORT).show();
Log.i(“123321”, "onServiceConnected: "+num);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
//解除绑定时调用, 清空接口,防止内容溢出
iMyAidlInterface=null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定服务
Intent intent=new Intent();
intent.setAction("com.bawei.1609A");//设置action
intent.setPackage("com.example.aidl_server");//设置服务端应用进程包名
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
//解除绑定
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}
aild清单文件
<?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>
</application>
### aidl接口代码 // IMyAidlInterface.aidl package com.example.aidl_client;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
int add( int num1,int num2);
}
aidl服务端服务代码
package com.example.aidl_server;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class MyService extends Service {
//代理人
IBinder binder=new IMyAidlInterface.Stub(){
@Override
public int add(int num1, int num2) throws RemoteException {
return num1+num2;
}
};
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
}
aidl客户端接口代码
// IMyAidlInterface.aidl
package com.example.aidl_server;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
int add( int num1,int num2);
}
messenger java代码
package com.example.message_client;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final String TAG = “123321”;
//用来接收副武器的消息
@SuppressLint(“HandlerLeak”)
Messenger messenger=new Messenger(new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle data = msg.getData();
String client = data.getString(“client”, “没有值。”);
Log.i(TAG, "handleMessage: "+client);
}
});
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Messenger messenger = new Messenger(service);
Message obtain = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString("server","你好服务器。");
obtain.setData(bundle);
//就是上面接收的信使
obtain.replyTo=messenger;
try {
messenger.send(obtain);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent it = new Intent();
it.setAction("com.shi.shi");
it.setPackage("com.example.messager_server");
bindService(it,connection, Service.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}
messenger 客户端java代码
package com.example.messager_server;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = “123321”;
@SuppressLint("HandlerLeak")
Messenger server_messenger=new Messenger(new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//取客户端发来的消息
Bundle bun = msg.getData();
String server = bun.getString("server", "客户端还没有发送。");
Log.i(TAG, "handleMessage: server"+server);
//给客户端发送一个消息,这个信使和客户端的一致。
Messenger c_messenger = msg.replyTo;
//要发送的消息。
Message obtain = Message.obtain();
Bundle bundle = new Bundle();
bundle.putString("client","客户端你好");
obtain.setData(bundle);
try {
c_messenger.send(obtain);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return server_messenger.getBinder();
}
}