研究过framwork的都知道,有一种通信叫跨进程通信---binder通信。每个模块都离不开binder。
那么两个apk直接通信用什么方法呢?可以用跨进程的服务和AIDL来实现。
前段时间,我想实现两个一个apk调用另一个apk的方法时,在网上搜也没搜到完整的例子。APIDemos里面的例子也不完整。
所就整理出完整的例子供一起学习。
服务端:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</LinearLayout>
Activity1:
package com.example.server;
import android.app.Activity;
import android.os.Bundle;
public class ServerActiviy extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
AIDL接口:
package com.example.server;
interface IAIDLService {
int getId();
}
实现AIDL接口:
package com.example.server;
import com.example.server.IAIDLService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class AIDLService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new ImplAIDLService(); //这个返回值需要一个服务对象。
}
public class ImplAIDLService extends IAIDLService.Stub{
@Override
public int getId() throws RemoteException {
return 123;
}
}
}
客户端:
布局:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.client"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.client.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity :
package com.example.client;
import android.app.Activity;
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.view.View.OnClickListener;
import android.widget.Button;
import com.example.client.R;
import com.example.server.IAIDLService;
public class MainActivity extends Activity {
private final String TAG = "MMIAudio";
Button button =null;
private boolean start = false;
private boolean isBound;
private IAIDLService boundService = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
bindService();
button.setText(" 点击试试看 ");
}
public void init(){
button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
int count = 0;
if(start){
try {
count = boundService.getId();
} catch (RemoteException e) {
e.printStackTrace();
}
Log.e(TAG, "-->> count = " + count);
button.setText("id = " + count);
unbindService();
start = false;
}else{
bindService();
start = true;
button.setText(" 点击获取 ");
}
}
});
}
private void bindService() {
Intent i = new Intent("com.example.server.IAIDLService");
bindService(i, connection, Context.BIND_AUTO_CREATE);
isBound = true;
}
private void unbindService() {
if (boundService != null) {
try {
Log.e(TAG,"_boundService.getId() = "+ boundService.getId());
} catch (RemoteException e) {
e.printStackTrace();
}
}
if (isBound) {
unbindService(connection);
isBound = false;
}
}
private ServiceConnection connection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
boundService = IAIDLService.Stub.asInterface(service);
Log.e(TAG, "-->> onServiceConnected(), boundService = " + boundService);
}
public void onServiceDisconnected(ComponentName className) {
boundService = null;
Log.e(TAG, "-->> onServiceDisconnected()");
}
};
}
AIDL接口(和服务端的接口一模一样,包名也必须相同,不论这个服务接口定义在哪里,只要包名和类名一样,都指的是同一个):
package com.example.server;
interface IAIDLService {
int getId();
}
网上有在写客户端的时候,把服务端的gen目录下的 包和文件原封不动拷贝到客户端。其实和在客户端创建一模一样的AIDL接口是一样的道理。
我想大家看到这个完整的例子后,也不觉的难吧。例子比较简单。在客户端里获取到类服务端的 123 。这样就可以进行两个apk直接的通信。