进程间通讯 AIDL实际使用步骤

1、编写aidl文件

Book.aidl

// Book.aidl
package test.test;

// Declare any non-default types here with import statements

parcelable Book;

IMyAidlInterface.aidl

注意导入import

// IMyAidlInterface.aidl
package test.test;
import test.test.Book;
// Declare any non-default types here with import statements

interface IMyAidlInterface {


   List  getBookList();


   void addBook(inout Book book);

}

2、编写Service服务

AIDLService.java

package test.test;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class AIDLService extends Service {


   private ArrayList bookList;

   @Override
   public void onCreate() {
       super.onCreate();

       bookList = new ArrayList();
       bookList.add(new Book("语文"));
       bookList.add(new Book("数学"));
       bookList.add(new Book("英语"));

   }

   @Nullable
   @Override
   public IBinder onBind(Intent intent) {
       return stub;
   }



   private final IMyAidlInterface.Stub stub =  new IMyAidlInterface.Stub(){

       @Override
       public List getBookList() throws RemoteException {
           return bookList;
       }

       @Override
       public void addBook(Book book) throws RemoteException {
           bookList.add(book);
       }
   };
}

3、绑定服务

MainActivity.java

bindService绑定服务

public class MainActivity extends AppCompatActivity {

   @BindView(R.id.button)
   Button mButton;
   @BindView(R.id.frame_layout)
   FrameLayout mFrameLayout;

   private IMyAidlInterface iMyAidlInterface;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       ButterKnife.bind(this);
       //首先要绑定服务
       bindService(new Intent(this,AIDLService.class),serviceConnection, Service.BIND_AUTO_CREATE);


       mButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               try {
                   iMyAidlInterface.addBook(new Book("体育"));
                   List bookList = iMyAidlInterface.getBookList();
                   for (Book book:bookList){
                       Log.e("book",book.getName());
                   }
               } catch (RemoteException e) {
                   e.printStackTrace();
               }
           }
       });
   }


   ServiceConnection serviceConnection = new ServiceConnection() {
       @Override
       public void onServiceConnected(ComponentName name, IBinder service) {
           iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
       }

       @Override
       public void onServiceDisconnected(ComponentName name) {
           iMyAidlInterface = null;
           Toast.makeText(MainActivity.this, name.toString(), Toast.LENGTH_SHORT).show();
       }
   };
}

4、配置不同的进程

AndroidManifest.xml

android:process配置不同的进程




   
       
           
               

               
           
       

       
       
   


你可能感兴趣的:(进程间通讯 AIDL实际使用步骤)