Android進程通訊:AIDL入門執行個體

來源:互聯網
上載者:User

標籤:

AIDL即 Android Interface Definition Language。原因:On Android, one process cannot normally access thememory of another process.

也就是說AIDL用於android處理序間通訊,下面就記錄一下第一個aidl的demo。

官方文檔也給出了基本的使用方法,如:


1. 在android項目的src相關包下建立一個aidl檔案 IRemoteService.aidl:

package com.example.testndk;interface IRemoteService {    /** Request the process ID of this service, to do evil things with it. */    int getPid();    /** Demonstrates some basic types that you can use as parameters     * and return values in AIDL.     */    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,            double aDouble, String aString);}
建立成功後會在gen目錄自動產生一個同名的java檔案,開啟可以看到其實是一個介面

public interface IRemoteService extends android.os.IInterface


2. 實現這個介面

package com.example.testndk;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.Process;public class RemoteService extends Service {    @Override    public void onCreate() {        super.onCreate();    }    @Override    public IBinder onBind(Intent intent) {        // Return the interface        return mBinder;    }    private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {        public int getPid(){            return Process.myPid();        }        public void basicTypes(int anInt, long aLong, boolean aBoolean,            float aFloat, double aDouble, String aString) {            // Does nothing        }    };}

3. 把介面暴露給用戶端,即調用處

package com.example.testndk;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import android.widget.TextView;public class TestNdkActivity extends Activity {    private static final String TAG = "TestNdkActivity";    private TextView tv1 = null;    // static {    // System.loadLibrary("TestNdk");    // }    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //        // String str = JniClient.AddStr("prefix", "suffix");        //        //        // int iSum = JniClient.AddInt(5, 2);        // String strSum = "5 + 2 = " + iSum;        tv1 = new TextView(this);        bindService(new Intent(RemoteService.class.getName()), mConnection, Context.BIND_AUTO_CREATE);        setContentView(tv1);    }    IRemoteService mIRemoteService = null;    private ServiceConnection mConnection = new ServiceConnection() {        // Called when the connection with the service is established        public void onServiceConnected(ComponentName className, IBinder service) {            // Following the example above for an AIDL interface,            // this gets an instance of the IRemoteInterface, which we can use            // to call on the service            mIRemoteService = IRemoteService.Stub.asInterface(service);            try {                tv1.setText("mIRemoteService=" + mIRemoteService.getPid());            } catch (RemoteException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        // Called when the connection with the service disconnects unexpectedly        public void onServiceDisconnected(ComponentName className) {            Log.e(TAG, "Service has unexpectedly disconnected");            mIRemoteService = null;        }    };    protected void onDestroy() {        super.onDestroy();        unbindService(mConnection);    }}

用到了service,還需在AndroidManifest.xml配置一下,添加
        <service android:name="com.example.testndk.RemoteService">            <intent-filter>                <action android:name="com.example.testndk.RemoteService"/>            </intent-filter>        </service>

因為是通過action來尋找service的,所以action標籤不可少。

官方文檔的例子稍微麻煩一點,不過也是比較清楚的。


Android進程通訊: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.