android借助AIDL实现跨进程调用例子
1.新建一个项目android AidlServer
项目的包名为:com.example.aidlserver
2.在项目中新建一个AIDL文件 取名叫IRemoteService.aidl
package com.example.aidlserver;
//定义服务的功能
interface IRemoteService {
int getPid();
String getName();
}
3.保存文件build下项目 或是使用aidl命令生成 IRemoteService.java
如果是在eclipse下自动build会在 gen目录下生成。
4.新建一个service
package com.example.aidlserver;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import com.example.aidlserver.IRemoteService;
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyRemote();
}
class MyRemote extends IRemoteService.Stub{
@Override
public int getPid() throws RemoteException {
// TODO Auto-generated method stub
return 2;
}
@Override
public String getName() throws RemoteException {
// TODO Auto-generated method stub
return "hello name";
}
}
}
5.修改AndroidManifest.xml 添加 RemoteService声明
<service android:name=".RemoteService">
<intent-filter >
<action android:name="com.example.aidlserver.RemoteService" />
</intent-filter>
</service>
在这里我们设置一个Itent-filter 是为了可以使service被隐式调用
到这里service就完成了
6.新建客户端项目AidlClient
7.在AidlClient醒目中新建包名 com.example.aidlserver
8.将第2步中建的AIDL copy到次包名下。
9.Build AidlClient项目
10.新建布局文件
<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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/mainthread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/servicethread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bind" />
<Button
android:id="@+id/unbind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="86dp"
android:text="unbind" />
<Button
android:id="@+id/getname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="getname" />
</LinearLayout>
11.编辑Activity
package com.example.aidlclient;
import android.app.Activity;
import android.content.ComponentName;
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.TextView;
import com.example.aidlserver.IRemoteService;
public class MainActivity extends Activity implements OnClickListener {
IRemoteService services;
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
services=null;
Log.v("unbind","unbind");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
services=(IRemoteService)IRemoteService.Stub.asInterface(service);
try {
String names = services.getName();
nametxt.setText(names);
servicethread.setText(Thread.currentThread().getName()+","+Thread.currentThread().getId());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
View bind,unbind;
TextView nametxt,mainthread,servicethread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nametxt=(TextView) findViewById(R.id.name);
mainthread=(TextView) findViewById(R.id.mainthread);
servicethread=(TextView) findViewById(R.id.servicethread);
bind = findViewById(R.id.bind);
unbind=findViewById(R.id.unbind);
mainthread.setText(Thread.currentThread().getName()+","+Thread.currentThread().getId());
bind.setOnClickListener(this);
unbind.setOnClickListener(this);
Log.v("threadId","threadId="+Thread.currentThread().getId());
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bind:
Intent i = new Intent("com.example.aidlserver.RemoteService");
bindService(i, conn , BIND_AUTO_CREATE);
break;
case R.id.unbind:
unbindService(conn);
break;
default:
break;
}
}
}