package com.example.remote;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;
public class Myservice extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("服务绑定");
return new MiddlePerson();
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("服务创建");
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("服务结束");
super.onDestroy();
}
public void methodInservice(String arr){
System.out.println("我是远程服务方法,我被调用了");
}
private class MiddlePerson extends IMiddlePerson.Stub{
@Override
public void call(String string) throws RemoteException {
// TODO Auto-generated method stub
methodInservice(string);
}
}
}
编写aidl接口
- JAVA基本数据类型不需要导入
- String,List,Map和CharSequence不需要导入
package com.example.remote;
interface IMiddlePerson {
void call(String string);
}
其次在清单文件中声明服务:
<service android:name="com.example.remote.Myservice">
<intent-filter ><action android:name="com.example.remote.Myservice"/></intent-filter>
</service>
二:编写调用远程服务的程序:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bind"
android:text="绑定远程" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mystop"
android:text="解除绑定" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="call"
android:text="调用方法" />
</LinearLayout>
调用实现:
package com.example.bindremote;
import com.example.remote.IMiddlePerson;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
private IMiddlePerson im;
private Myconn conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void mystop(View vew){
unbindService(conn);
}
public void bind(View vew){
Intent service=new Intent();
service.setAction("com.example.remote.Myservice");
conn=new Myconn();
bindService(service, conn, BIND_AUTO_CREATE);
}
public void call(View vew){
try {
im.call("leigewudi");
System.out.println(im);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class Myconn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
im=IMiddlePerson.Stub.asInterface(service);
System.out.println("服务被调用了"+im);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
unbindService(conn);
System.out.println("服务结束");
}
}
}
实验结果: