AIDL安卓介面定義語言

來源:互聯網
上載者:User

標籤:auto   pre   button   lin   rem   pac   rri   執行   服務端   

Android    Interface Definition  Language簡稱AIDL翻譯為 :安卓       介面      定義        語言

AIDL:處理序間通訊。Android Interface Defination Language。(使用介面回調的思想)

舉例:

B應用程式中有一個服務,功能是做加法運算。
A應用程式需要執行B應用中的加法功能。
A中產生2個加數,將資料傳給B應用的服務。B進行運算,將運算結果返回給A。
我們要定義一個AIDL介面規範進程間的通訊,AIDL介面的代碼格式和傳統的Java有些不同

 

AIDL使用的前提:Service的綁定方式。
onCreate建立
onBind綁定
onUnbind解除綁定
onDestroy銷毀

 

 

故事:
板磚A(Activity)和板磚S(Service)
1、去五金商店買膠水
2、將膠水塗在S表面上
3、將A板磚粘在S表面上(注意:A和S中間有膠水)
4、檢測A和S是否粘牢
5、如果粘牢,拿去拍人吧

 

代碼中
1、建立膠水類Binder
2、在Service的onBind方法中初始化膠水
3、使用Activity去綁定Service
4、通過一個介面來確定Activity和Service是否串連成功
5、如果串連成功,A程式的Activity就可以使用B程式的Service了

 

思路:
先實現B程式的Service,具有加法運算功能
再實現A程式的Activity,去綁定Service

 

服務端:

 

/** * 服務端,包含一個Service,運用執行加法運算 * 1、建立處理序間通訊使用的AIDL介面 * 2、實現Service * 3、等待用戶端訪問該Service */public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}public class PlusService extends Service {    public PlusService() {    }    // 建立    @Override    public void onCreate() {        super.onCreate();        Log.d("1507", "Service onCreate");    }    // 1、建立膠水類    // stub:本意為存根。AIDL提供的類似於膠水的類,在這個膠水中實現了加法運算    private static class MyBinder extends IMyAidlInterface.Stub {        @Override        public int plus(int plus01, int plus02) throws RemoteException {            return plus01 + plus02;        }    }    // 綁定    @Override    public IBinder onBind(Intent intent) {        Log.d("1507", "Service onBind");        return new MyBinder();    }    // 解除綁定    @Override    public boolean onUnbind(Intent intent) {        Log.d("1507", "Service onUnbind");        return super.onUnbind(intent);    }    // 銷毀    @Override    public void onDestroy() {        Log.d("1507", "Service onDestroy");        super.onDestroy();    }}

 

資訊清單檔:

 

 <service            android:name=".service.PlusService"            android:enabled="true"            android:exported="true">            <intent-filter>                <action android:name="bind_server"/>            </intent-filter>        </service>

 

 

 

用戶端:

 

/** * 3、綁定服務端的Service * 4、通過ServiceConnection介面確定是否串連成功 * 5、如果串連成功,調用Service的操作 */public class MainActivity extends AppCompatActivity implements        View.OnClickListener, ServiceConnection {    // 自訂Action    public static final String ACTION_BIND = "bind_server";    protected EditText mPlus01Et;    protected EditText mPlus02Et;    protected Button mCalculateBtn;    protected TextView mResultTv;    private IMyAidlInterface mPlusInterface;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        Log.d("1507", "Activity onCreate");        super.setContentView(R.layout.activity_main);        initView();        bindService();    }    // 綁定Service    private void bindService() {        Intent intent = new Intent(ACTION_BIND);// action        intent.setPackage("net.bwie.aidlserver");// server端對應的包名        boolean isBindSuccessful = bindService(intent, this, Context.BIND_AUTO_CREATE);        Toast.makeText(this, "" + isBindSuccessful, Toast.LENGTH_SHORT).show();    }    @Override    public void onClick(View view) {        if (view.getId() == R.id.calculate_btn) {            int plus01 = Integer.parseInt(mPlus01Et.getText().toString());            int plus02 = Integer.parseInt(mPlus02Et.getText().toString());            // 調用AIDL介面中的方法            try {                int result = mPlusInterface.plus(plus01, plus02);                mResultTv.setText("結果是:" + result);            } catch (RemoteException e) {                e.printStackTrace();            }        }    }

 

AIDL安卓介面定義語言

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.