Aidl的使用步驟


先說說Aidl傳遞參數類型

1.基本數據類型(除short類型)

2.String、charSequence

3.List,map

4.parcelable

 

第1步:定義一個*.aidl文件,eclipse會自動生成,android Studio需要編譯一下(用Make project)

簡單例子:

// IMyService.aidl
package com.example.administrator.yunstore;

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

interface IMyService {

     void setValue(String name);
     String getValue();

}

(以上是Android studio生成的java文件位置)

第2步:實現AIDL文件生成的JAVA接口Stub

package com.example.administrator.yunstore;

import android.os.RemoteException;

/**
 * Created by Administrator on 2016/7/11.
 */
public class Person extends IMyService.Stub {
    private String name;
    @Override
    public void setValue(String name) throws RemoteException {

        this.name=name;
    }

    @Override
    public String getValue() throws RemoteException {
        return name;
    }
}

  第3步:定義一個自己的service,在實現自己的service時,為了其他應用可以通過bindService來和我們的service進行交互,我們都要實現service中的onBind()方法,並且返回一個繼承了Binder的內部類

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

/**
 * Created by Administrator on 2016/7/11.
 */
public class useService extends Service {
    private IMyService.Stub iPerson=new Person();
    @Override
    public IBinder onBind(Intent intent) {
        Log.i("service", "onBind...");
        return iPerson;
    }
}

 第4步,同一個應用中的Activity為該Service中賦值,使用service

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private IMyService iMyService;
    private Button unBindButton,BindButton;

    private ServiceConnection conn=new ServiceConnection() {
        @Override//連接上服務
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

            iMyService=IMyService.Stub.asInterface(iBinder);
            if(iMyService!=null){
                try {
                    iMyService.setValue("設置值");
                    Toast.makeText(getApplicationContext(),"設定值成功",Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),"設定值失敗"+e,Toast.LENGTH_SHORT).show();
                }
            }
        }

        @Override//斷開服務
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        unBindButton= (Button) findViewById(R.id.unBind);
        BindButton= (Button) findViewById(R.id.Bind);

        unBindButton.setOnClickListener(new MyButtonListener());
        BindButton.setOnClickListener(new MyButtonListener());

    }
    private class MyButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.Bind:{
                    //綁定服務
                    bindService(new Intent("testAIDL"),conn, Service.BIND_AUTO_CREATE);
                    break;
                }
                case R.id.unBind:{
                    //解綁服務
                    unbindService(conn);
                    break;
                }
            }

        }
    }
}

  

客戶端:
       第1步:客戶端要想使用該服務,肯定要先知道我們的服務在aidl文件中到底對外提供了什么服務,對吧?所以,第一步,我們要做的就是,將aidl文件拷貝一份到客戶端的程序中(這里一定要注意,包路徑要和服務端的保持一致哦,例如服務端為cn.com.chenzheng_java.remote.a.aidl,那么在客戶端這邊也應該是這個路徑)。

      第2步:我們都知道,想要和service交互,我們要通過bindService方法,該方法中有一個ServiceConnection類型的參數。而我們的主要代碼便是在該接口的實現中。

      第3步:在ServiceConnection實現類的onServiceConnected(ComponentName name, IBinder service)方法中通過類似remoteServiceInterface = RemoteServiceInterface.Stub.asInterface(service);方式就可以獲得遠程服務端提供的服務的實例,然后我們就可以通過remoteServiceInterface 對象調用接口中提供的方法進行交互了。(這里的關鍵是通過*.Stub.asInterface(service);方法獲取一個aidl接口的實例哦)

       我們前面在服務端中說過了,必須提供一個intent-filter來匹配請求是否合法,所以我們在客戶端訪問服務的時候,還必須傳遞包含了匹配action的Intent哦。

客戶端中使用服務端中的service范例:

private ServiceConnection conn = new ServiceConnection() {    
     @Override    
     public void onServiceDisconnected(ComponentName arg0) {    
     }    
     //因為有可能有多個應用同時進行RPC操作,所以同步該方法    
     @Override    
     public synchronized void onServiceConnected(ComponentName arg0, IBinder binder) {    
         //獲得IPerson接口    
         person = IPerson.Stub.asInterface(binder);    
         if(person != null){    
             try {    
                 //RPC方法調用    
                 String name = person.getValue();    
                 Toast.makeText(DemoAIDLActivity.this, "遠程進程調用成功!值為 : "+name, Toast.LENGTH_LONG).show();    
             } catch (RemoteException e) {    
                 e.printStackTrace();      
                 Toast.makeText(DemoAIDLActivity.this, "遠程進程調用失敗! ", Toast.LENGTH_LONG).show();    
             }    
         }    
     }  
 };    
     
 @Override    
 public void onCreate(Bundle savedInstanceState) {    
     super.onCreate(savedInstanceState);    
     setContentView(R.layout.main);    
     btn = (Button)findViewById(R.id.btn);    
     btn.setOnClickListener(new OnClickListener() {    
         @Override    
         public void onClick(View arg0) {    
             //該應用中不需要在manifest中配置RemoteService    
             bindService(new Intent("forServiceAidl"), conn, Service.BIND_AUTO_CREATE);    
         }    
     });    
 }    

 

  



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM