Directory structure: (the package name path and service on the client side must be the same)
1: client side: service side:
2:client端AIDLTest.aidl
package com.example.aidl; interface AIDLTest{ String hello(String name); }
MainActivity:
package com.example.aidlclient; import android.app.Activity; import android.content.ComponentName; import android.content.Context; 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.Menu; import android.widget.Toast; import com.example.aidl.AIDLTest; // If you just want to start a background service for a long-term task, then use startService. If you also want to get in touch with the running Service, there are two ways: one is to use broadcast, and the other is to use bindService. The disadvantage of the former is that if the communication is more frequent, it is easy to cause performance problems, while the latter does not have these problems. Therefore, in this case, startService and bindService need to be used together. public class MainActivity extends Activity { AIDLTest RemoteService; // Listening service private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName arg0) { Log.d("yjm", "1111111111111111111111"); // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { Log.d("yjm", "2222222222222222222"); // TODO Auto-generated method stub RemoteService = AIDLTest.Stub.asInterface(arg1); try { String string = RemoteService.hello("aaaaaaaaaaaaaa"); Toast.makeText(MainActivity.this, string, Toast.LENGTH_LONG).show(); Log.d("yjm", "QQQQQQQ"+string); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate (savedInstanceState); setContentView(R.layout.activity_main); Log.d("yjm", "DDDDDDDDDDDD"); initservice (); } // Connection service public void initservice(){ /* Intent i = new Intent(); i.setAction("android.intent.action.AIDLService");*/ //Intent i = new Intent(MainActivity.this, AIDLTest.class); //Intent i = new Intent("android.intent.action.AIDLService"); Intent i = new Intent(); i.setAction("android.intent.action.AIDLService"); i.setPackage("com.example.aidl.AIDLService"); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } boolean ret = bindService(i, mConnection, Context.BIND_AUTO_CREATE); } // Disconnect the service private void releaseService() { unbindService(mConnection); mConnection = null; } @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; } }
3:service端AIDLTest.aidl
package com.example.aidl; interface AIDLTest{ String hello(String name); }
MainActivity:
package com.example.aidlservice; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; public class MainActivity extends Activity { private Intent intent; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate (savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent(MainActivity.this, MusicPlayerService.class); startService(intent); } @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; } @Override public void onDestroy () { super .onDestroy (); stopService(intent); } }
AIDLService:
package com.example.service; import com.example.aidl.AIDLTest; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class AIDLService extends Service{ @Override public IBinder onBind(Intent arg0) { /*return new AIDLTest.Stub() { @Override public String hello(String name) throws RemoteException { // TODO Auto-generated method stub return "hello"+name; } }; * / return mBinder; } // A class that inherits Binder, then its object can be used by remote processes (provided that the remote process obtains the object of this class [object reference] private final AIDLTest.Stub mBinder = new AIDLTest.Stub() { @Override public String hello(String name) throws RemoteException { // TODO Auto-generated method stub return "hello"+name; } }; }