This example shares the specific code for Android to use AIDL to play music for your reference. The specific content is as follows
Ideas:
① Create two new APP projects or modules, one as the server and the other as the client, create a service on the server.
② Create two new aidl files (interfaces) in the main files of two modules, and define the method of processing music.
③ After defining the method of two AIDL files, give them makeproject in the taskbar and compile it into Java files, so that they can be used in service and acvitity.
Need to instantiate and implement remote methods
④.The onbind method in Service returns:.
The service needs to set an action, otherwise the client service will report a null pointer exception when running.
First handle the server:
In the Mainfest file, add an action to the implicit call of intent
<service android:name=".MusicService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name=""></action> </intent-filter>
MusicService。Java
public class MusicService extends Service { private MediaPlayer player = null; public MusicService() { } //① Implement the adil interface that has been madeprojected and rewrite the three custom definitions in it. stub = new () { @Override public void paly() throws RemoteException { if (player == null) { player = (, ); } if (player != null && !()){ (); } } @Override public void paus() throws RemoteException { if(player!=null&&()){ (); } } @Override public void stop() throws RemoteException { if(player!=null){ (); } try { ();//Prepare for next playback } catch (IOException e) { (); } } }; @Override public IBinder onBind(Intent intent) { return stub; } //② Rewrite the method when service is destroyed @Override public void onDestroy() { (); if(player!=null){ (); ();//Release resources to prevent the loss of the support and the occurrence of abnormalities } }
Handle the client
public class MainActivity extends AppCompatActivity implements { private Button btn_play, btn_pause, btn_stop, btn_stopservice, btn_stopacvitity; MusicAidlInterface service = null; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); intiView(); connect(); } private void intiView() { btn_play = (Button) findViewById(.btn_play); btn_pause = (Button) findViewById(.btn_pause); btn_stopservice = (Button) findViewById(.btn_stopservice); btn_stopacvitity = (Button) findViewById(.btn_exitacvitity); btn_stop = (Button) findViewById(.btn_stop); btn_play.setOnClickListener(this); btn_pause.setOnClickListener(this); btn_stop.setOnClickListener(this); btn_stopservice.setOnClickListener(this); btn_stopacvitity.setOnClickListener(this); } @Override public void onClick(View v) { try { switch (()) { case .btn_play: (); break; case .btn_pause: (); break; case .btn_stop: (); break; case .btn_stopservice: if (conn != null) { unbindService(conn); } break; case .btn_exitacvitity: finish(); break; } } catch (RemoteException e) { (); } } //Connect service ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder iBinder) { service = (iBinder); } @Override public void onServiceDisconnected(ComponentName name) { service = null; } }; //Get the method to connect to BindService private void connect() { //Use the implicit call method of intent Intent intent = new Intent(""); //Set the package name of the target service (""); bindService(intent, conn, BIND_AUTO_CREATE); } //Rewrite the method of unbinding when acvitity is destroyed @Override protected void onDestroy() { (); if (conn != null) {//Unbind unbindService(conn); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.