服务器端:
工程结构图:
[img]
[img]http://dl.iteye.com/upload/attachment/0062/5815/5817c051-6974-31af-928f-283582ca21e4.png[/img]
[/img]
IPerson.aidl
IPersonImpl
MyRemoteService
MainActivity
res/layout/main.xml
AndroidManifest.xml
注意:gen 下的 com.zzl.test下的文件时自动生成的。
客户端:
工程结构图:
[img]
[img]http://dl.iteye.com/upload/attachment/0062/5842/55a3309a-c159-33c5-bcf3-40cb1f27202b.png[/img]
[/img]
这里的src下的com.zzl.test这个包是从服务器拷过来的。
MainActivity:
工程结构图:
[img]
[img]http://dl.iteye.com/upload/attachment/0062/5815/5817c051-6974-31af-928f-283582ca21e4.png[/img]
[/img]
IPerson.aidl
package com.zzl.test;
interface IPerson {
void setName(String name);
void setAge(int age);
String display();
}
IPersonImpl
package com.zzl.test;
import android.os.RemoteException;
public class IPersonImpl extends IPerson.Stub{
private String name;
private int age;
@Override
public String display() throws RemoteException {
return "name="+name+"age="+age;
}
@Override
public void setAge(int age) throws RemoteException {
this.age = age;
}
@Override
public void setName(String name) throws RemoteException {
this.name = name;
}
}
MyRemoteService
package com.zzl.test;
import com.zzl.test.IPerson.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyRemoteService extends Service{
private Stub iPerson = new IPersonImpl();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return iPerson;
}
}
MainActivity
package com.zzl.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn_start;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_start = (Button) findViewById(R.id.button1);
btn_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MyRemoteService.class);
startService(intent);
btn_start.setText("服务器已启动");
}
});
}
}
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:text="启动服务器"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zzl.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="MyRemoteService"
android:process=":remote">
<intent-filter>
<action android:name="com.zzl.test.MyRemoteService"></action>
</intent-filter>
</service>
</application>
</manifest>
注意:gen 下的 com.zzl.test下的文件时自动生成的。
客户端:
工程结构图:
[img]
[img]http://dl.iteye.com/upload/attachment/0062/5842/55a3309a-c159-33c5-bcf3-40cb1f27202b.png[/img]
[/img]
这里的src下的com.zzl.test这个包是从服务器拷过来的。
MainActivity:
package com.zzl.client;
import com.zzl.test.IPerson;
import android.app.Activity;
import android.app.Service;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private IPerson iPerson;
private Button btn_start;
private ServiceConnection conn = new ServiceConnection(){
@Override
synchronized public void onServiceConnected(ComponentName name, IBinder service) {
iPerson = IPerson.Stub.asInterface(service);
if(iPerson!=null){
try {
//RPC方法调用
iPerson.setName("无敌小鸡鸡");
iPerson.setAge(6);
String msg = iPerson.display();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_start = (Button) findViewById(R.id.button1);
btn_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//设置Intent 的Action属性
intent.setAction("com.zzl.test.MyRemoteService");
//绑定服务
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
}
}