总的来说的Android跨进程通信的方式常用的有以下几种
activity通信可以是当前应用程序的中的两个Activity进行数据交换,也可以跨进程,去和其他应用程序中的Activity进行通信。也是最常用的通信方式之一。下面描述的是App间通信的大致流程。
通信流程大致如下:
代码如下:
Activity_A中启动方法Activity_B
/**
* Activity_A开始通信
*/
public void startActivity_B() {
Intent intent = new Intent();
int requestCode = 1;
//设置启动的包名+类名(全路径)
intent.setClassName("com.appb.activity","com.appb.activity.Activity_B");
//设定将要传输的数据
intent.putExtra("requestKey", "testData");
super.startActivityForResult(intent, requestCode);
}
Activity_B中Intent中数据的获取
@Override
protected void onCreate(final Bundle savedInstanceState) {
Intent intent = getIntent();
//数据取得
mRequestData = intent.getStringExtra("requestKey");
}
Activity_B中向Activity_B返回数据
public void closeActivity_B() {
Intent data= new Intent();
//返回数据定义
data.putExtra("responseKey", "responseData");
//返回数据设置
setResult(Activity.RESULT_OK, data);
//关闭当前Activity,返回启动Acivity
super.finish();
}
Activity_A中接收Intent 中返回的数据
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1 && resultCode == Activity.RESULT_OK){
//接收返回的数据
String responseData = data.getStringExtra("responseKey");
} else {
//TODO.
}
}
//TODO
//TODO
service通信其实就是安卓独有的AIDL服务,AIDL(Android Interface definition language),接口描述语言(语法和java类似),android特有的一种IPC机制。
aidi文件 TestAppService .aidl
interface TestAppService {
int checkService(int requestId, TestAppServiceCallbacks callback);
}
aidi文件 TestAppServiceCallbacks .aidl
用于通信的回调
interface TestAppServiceCallbacks {
void onCompleteCheckService(int requestId, int result);
}
需要实现aidl编译后生成的 TestAppService.Stub 接口
public class TestAppServiceBinder extends TestAppService.Stub {
@Override
public int checkService(int requestId, TestAppServiceCallbacks callback){
//TODO 其他逻辑处理
int result = 0;
callback.onCompleteCheckService( requestId, result );
return 0;
}
}
同时需要定义提供给外部App绑定的Service
public TestAppAidlService extends Service {
private TestAppServiceBinder mBinder = null;
@Override
public void onCreate() {
super.onCreate();
mBinder = new TestAppServiceBinder(this);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
客户端定义连接对象
//客户端用来请求的service
private TestAppService testAppService;
//service的连接对象
private ServiceConnection serviceConnection = new ServiceConnection () {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 我们这里拿到的对象其实就是其Stub的内部类Proxy对象
testAppService = TestAppService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
//TODO service断开连接
}
}
客户端定义绑定服务
private boolean bindAppService() {
Intent intent = new Intent();
intent.setClassName("com.app.service", "com.app.service.TestAppService");
try {
if (getApplicationContext().bindService(intent, serviceConnection ,
Context.BIND_AUTO_CREATE)) {
return true;
}
} catch (SecurityException ex) {
//TODO
}
return false;
}
客户端开始调用Service
@Override
protected void onResume() {
//绑定service成功
if (bindAppService()){
int result = testAppService.checkService();
} else {
//TODO 绑定失败
}
}
客户端Service回调处理
private TestAppServiceCallbacks.Stub testAppServiceCallbacks =
new TestAppServiceCallbacks.Stub(){
@Override
void onCompleteCheckService (int requestId, int result ){
//TODO 回调处理,接收服务端返回的数据
}
}
https://github.com/cteamx/Thief-Book