原理:在需要传递数据的2个进程中都写一个AIDL文件,自定义一个Binder继承自AIDL所创建的哪个Binder通过Binder把进程和Service绑定起来,来达到数据互传的目的。
注意:
1、两个进程创建的AIDL文件的文件名必须一致
2、在写完AIDL文件后要点击Build目录下的Rebuild Project进行重新编译。在创建AIDL的时候,系统不会自动进行编译,所以必须进行手动编译,如果不进行编译就不能够
找到系统根据自己创建的AIDL文件所对应的Java代码,也就无法去使用。
3、他们的路径名也必须一样,即创建的2个AIDL文件的包名也必须是一样的。
4、要注意2个进程中的AIDL文件的内容也必须一致。
实现步骤:
1、在一个进程中创建一个AIDL文件IServer,并且进行重新编译;
2、创建一个Service,自定义一个binder继承自IServer.Stub,用这个activity来开启service;
3、在另一个进程中创建一个同样的AIDL文件,2个AIDL文件的命名,包名都要一致,然后进行重新编译;
4、然后用自定义的binder把service和activity绑定起来;
5、2个进程都绑定同一个service就达到了数据互传的目的。
实现代码:
第一个进程:
AIDL文件:
// Iserver.aidl
package com.tomato.z.androidday24server;
// Declare any non-default types here with import statements
interface IServer {
String getContent();
String changeContent(String changeContent);
}
activity:
package com.tomato.z.androidday24server.activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.tomato.z.androidday24server.R;
import com.tomato.z.androidday24server.service.ContentService;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, ContentService.class));
}
}
service:
package com.tomato.z.androidday24server.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import com.tomato.z.androidday24server.IServer;
/**
* Created by Administrator on 2016/7/2.
*/
public class ContentService extends Service {
String content = "这是ContentService里面的数据";
private static final String TAG = "Z_CONTENT_SERVICE";
@Override
public void onCreate() {
Log.i(TAG, "onCreate: " + "Thread" + Thread.currentThread().getName());
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new ContentBinder();
}
class ContentBinder extends IServer.Stub{
@Override
public String getContent() throws RemoteException {
return content;
}
@Override
public String changeContent(String changeContent) throws RemoteException {
content = changeContent;
return content;
}
}
}
第二个进程:
AIDL文件:
// IServier.aidl
package com.tomato.z.androidday24server;
// Declare any non-default types here with import statements
interface IServer {
String getContent();
String changeContent(String changeContent);
}
activity:
package com.tomato.z.androidday24;
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.Button;
import com.tomato.z.androidday24server.IServer;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button_start;
private static final String TAG = "Z_CONTENT_SERVICE";
private IServer iServer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_start = (Button) findViewById(R.id.button_start);
Intent intent = new Intent("z.androidday24.Z_CONTENT_SERVICE");
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
button_start.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
String content = iServer.getContent();
Log.i(TAG, "原服务端数据" + content);
String changeContent = iServer.changeContent("这是客户端进程修改服务端进程成功");
Log.i(TAG, "修改后服务端数据: " + changeContent);
} catch (RemoteException e) {
e.printStackTrace();
}
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iServer = IServer.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
iServer = null;
}
};
}