远程Service调用
实现进程之间相互通信(AIDL,android 接口定义语言),客户端和被调用实现之间是通过代理模式实现的。代理类和被代理类实现同一个接口即Ibinder接口。
(1)AIDL文件创建在src\包中,F5刷新工程,gen\包中自动生成.java文件
(2)IPersonImpl.java
(3)MyRemoteSerive.java
(4)MainActivity.java
(5)main.xml
(6)Android Menifest.xml添加service
1、工程目录
2、IPerson.aidl
package com.example.test_aidlservice;
interface IPerson{
//设置年龄方法
void setAge(int age);
//设置姓名方法
void setName(String name);
//显示信息方法
String display();
}
IPersonImpl.java
package com.example.test_aidlservice;
import android.os.RemoteException;
public class IPersonImpl extends IPerson.Stub{
//声明两个变量
private int age;
private String name;
//设置age
@Override
public void setAge(int age) throws RemoteException {
// TODO Auto-generated method stub
this.age=age;
}
//设置name
@Override
public void setName(String name) throws RemoteException {
// TODO Auto-generated method stub
this.name=name;
}
//显示name和age
@Override
public String display() throws RemoteException {
// TODO Auto-generated method stub
return "name:"+name+" age="+age;
}
}
MainActivity.java
package com.example.test_aidlservice;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
//RPC测试
public class MainActivity extends Activity {
//声明IPerson接口
private IPerson iPerson;
//声明Button
private Button btn;
//实例化ServiceConnection
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
//获得IPerson接口
iPerson = IPerson.Stub.asInterface(service);
if(iPerson != null)
try {
//RPC方法调用
iPerson.setName("jiangcuicui");
iPerson.setAge(25);
String msg = iPerson.display();
//显示方法调用返回值
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
// TODO: handle exception
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置当前视图布局
setContentView(R.layout.main);
//实例化Button
btn = (Button)findViewById(R.id.button01);
//为Button添加单击事件监听器
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//实例化Intent
Intent intent = new Intent();
//设置Intent Action属性
intent.setAction("com.example.test_aidlservice.action.MY_REMOTE_SERVICE");
//绑定服务
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MyRemoteService.java
package com.example.test_aidlservice;
import com.example.test_aidlservice.IPerson.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
//使用Service将接口暴露给客户端
public class MyRemoteService extends Service {
// 声明IPerson接口
private Stub iPerson = new IPersonImpl();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return iPerson;
}
}
3、布局文件main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RemoteService测试" />
</LinearLayout>
4、Android Menifest.xml
<service android:name=".MyRemoteService">
<intent-filter >
<action android:name="com.example.test_aidlservice.action.MY_REMOTE_SERVICE"/>
</intent-filter>
</service>