android 簡單的aidl

來源:互聯網
上載者:User

 1.首先在src目錄下建立一個file,命名為IPerson.aidl

package com.example.aidldemo;interface IPerson {    void setAge(int age);    void setName(String name);    String display();}

2.接著要實現這個aidl裡面的方法

package com.example.aidldemo;import android.os.RemoteException;public class IPersonImpl extends IPerson.Stub{    //聲明兩個變數    private int age;    private String name;    @Override    public void setAge(int age) throws RemoteException {        this.age=age;    }    @Override    public void setName(String name) throws RemoteException {        this.name=name;    }    @Override    public String display() throws RemoteException {        return "name="+name+";age="+age;    }}

3.建立一個service

package com.example.aidldemo;import com.example.aidldemo.IPerson.Stub;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyRemoteService extends Service {    private Stub iPerson=new IPersonImpl();    @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        return iPerson;    }}

4.在activity中啟動服務

package com.example.aidldemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Intent intent = new Intent();        // 設定Intent Action 屬性        intent.setAction("com.example.aidldemo.action.MY_REMOTE_SERVICE");        // 綁定服務        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.activity_main, menu);        return true;    }}

5.建立一個項目,把上一個項目中的aidl拷貝到項目中
在activity中實現如下:

 

package com.example.aidlclient;import android.app.Activity;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;import com.example.aidldemo.IPerson;public class MainActivity extends Activity {    private IPerson iPerson;    private Button btn;    // 執行個體化ServiceConnection        private ServiceConnection conn = new ServiceConnection() {            @Override            synchronized public void onServiceConnected(ComponentName name, IBinder service) {                // 獲得IPerson介面                iPerson = IPerson.Stub.asInterface(service);                System.out.println("iperson----------:"+iPerson);            }            @Override            public void onServiceDisconnected(ComponentName name) {                iPerson=null;            }        };        @Override        public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            // 設定當前視圖布局            setContentView(R.layout.activity_main);            // 執行個體化Button            btn = (Button) findViewById(R.id.button1);            //為Button添加單擊事件監聽器                        // 執行個體化Intent            Intent intent = new Intent("com.example.aidldemo.action.MY_REMOTE_SERVICE");            // 設定Intent Action 屬性            bindService(intent, conn, Service.BIND_AUTO_CREATE);                        btn.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                                                            try{                        iPerson.setAge(20);                        iPerson.setName("南瓜餅");                        String msg = iPerson.display();                        // 顯示方法調用傳回值                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();                    }catch (Exception e) {                        e.printStackTrace();                    }                }            });                    }                @Override        protected void onDestroy()        {            // TODO Auto-generated method stub            unbindService(conn);            super.onDestroy();        }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }}

 項目:

csdn:http://download.csdn.net/detail/wenwei19861106/4879164

移動開發qq群:59516399

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.