在下面例子中04Service中添加aidl包包里定义好接口 接口文件名后缀为.aidl
package com.example.aidl;
interface IRemoteService
{ void print(String msg); String getName();}
在04 client中也有这样一个包 这个包就成为一个接口 Service里实现接口 client中可以调用注意
Service中定义好service的action client中绑定服务时用这个action
<service android:name=".RemoteService">
<intent-filter > <action android:name="com.example.service04aidl.RemoteService"/> </intent-filter> </service>绑定时用到
bindService(new Intent("com.example.service04aidl.RemoteService"),conn,BIND_AUTO_CREATE);
客户端每次绑定都会创建一个与客户端activity绑定的Service activity销毁服务也会销毁
1 package com.example.service04_client; 2 3 import android.app.Activity; 4 import android.content.ComponentName; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.Bundle; 8 import android.os.IBinder; 9 import android.os.RemoteException;10 import android.view.View;11 import android.widget.Toast;12 13 import com.example.aidl.IRemoteService;14 15 public class MainActivity extends Activity {16 17 IRemoteService remoteService;18 ServiceConnection conn=new ServiceConnection() { 19 @Override20 public void onServiceDisconnected(ComponentName name) {21 22 }23 24 @Override25 public void onServiceConnected(ComponentName name, IBinder service) {26 remoteService=IRemoteService.Stub.asInterface(service); 27 }28 };29 30 @Override31 protected void onCreate(Bundle savedInstanceState) {32 super.onCreate(savedInstanceState);33 setContentView(R.layout.activity_main);34 35 bindService(new Intent("com.example.service04aidl.RemoteService"),conn,BIND_AUTO_CREATE);36 }37 public void print(View v) throws RemoteException38 {39 if(remoteService!=null)40 remoteService.print("hello,service");41 }42 public void getRemoteName(View v) throws RemoteException43 {44 if(remoteService!=null)45 Toast.makeText(getApplicationContext(), remoteService.getName(), 1).show();46 }47 48 }
1 package com.example.service04aidl; 2 3 import com.example.aidl.IRemoteService; 4 5 import android.app.Service; 6 import android.content.Intent; 7 import android.os.IBinder; 8 import android.os.RemoteException; 9 import android.util.Log;10 11 public class RemoteService extends Service {12 13 private IRemoteService.Stub stub=new IRemoteService.Stub() { 14 @Override15 public void print(String msg) throws RemoteException {16 Log.i("debug", "RemoteService---"+msg);17 }18 @Override19 public String getName() throws RemoteException {20 return "RemoteService";21 }22 };23 @Override24 public IBinder onBind(Intent intent) {25 // TODO Auto-generated method stub26 return stub;27 }28 29 }
1 26 7 10 11 16 19 20 24 2521 22 23 26 30 31 3227 2928
1 package com.example.service04_client; 2 3 import android.app.Activity; 4 import android.content.ComponentName; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.Bundle; 8 import android.os.IBinder; 9 import android.os.RemoteException;10 import android.view.View;11 import android.widget.Toast;12 13 import com.example.aidl.IRemoteService;14 15 public class MainActivity extends Activity {16 17 IRemoteService remoteService;18 ServiceConnection conn=new ServiceConnection() { 19 @Override20 public void onServiceDisconnected(ComponentName name) {21 22 }23 24 @Override25 public void onServiceConnected(ComponentName name, IBinder service) {26 remoteService=IRemoteService.Stub.asInterface(service); 27 }28 };29 30 @Override31 protected void onCreate(Bundle savedInstanceState) {32 super.onCreate(savedInstanceState);33 setContentView(R.layout.activity_main);34 35 bindService(new Intent("com.example.service04aidl.RemoteService"),conn,BIND_AUTO_CREATE);36 }37 public void print(View v) throws RemoteException38 {39 if(remoteService!=null)40 remoteService.print("hello,service");41 }42 public void getRemoteName(View v) throws RemoteException43 {44 if(remoteService!=null)45 Toast.makeText(getApplicationContext(), remoteService.getName(), 1).show();46 }47 48 }