现在的位置: 首页 > 综合 > 正文

android下的aidl service的简单使用

2017年12月24日 ⁄ 综合 ⁄ 共 3330字 ⁄ 字号 评论关闭

AIDL (Android Interface Definition Language) is similar to other IDLs you might haveworked with. It allows you to define the programming interface that boththe client and service agree upon in order to communicate with each other
usinginterprocess communication (IPC). On Android, one process cannot normally access thememory of another process. So to talk, they need to decompose their objects into primitives that theoperating system can understand, and marshall the objects across that
boundary for you. The code todo that marshalling is tedious to write, so Android handles it for you with AIDL.

1.在android进程之间是不能直接通信的,为此才有了aidl service进行“间接的”进程通信。

2.为了使用aidl service,首先我们得创建一个aidl serivce提供者。新建一个android工程。

package com.toigc.aidlserviceprovider;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;

public class AidlService extends Service{

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return new MyBinder();
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }
    
    private class MyBinder extends IAidlService.Stub
    {

        @Override
        public void sayHello() throws RemoteException {
            System.out.println("say hello!");
            
        }
        
    }

}

package com.toigc.aidlserviceprovider;

 interface IAidlService {
    void sayHello();
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.toigc.aidlserviceprovider"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <service android:name=".AidlService" >
            <intent-filter >
                <action android:name="com.toigc.aidlserviceprovider"/>
                
            </intent-filter>
        </service>
    </application>

</manifest>


package com.togic.aidlserviceuser;

import com.toigc.aidlserviceprovider.IAidlService;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
    IAidlService mIAidlService = null;

    private class mServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mIAidlService = IAidlService.Stub.asInterface(service);

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent();
        intent.setAction("com.toigc.aidlserviceprovider");
        bindService(intent, new mServiceConnection(), BIND_AUTO_CREATE);//綁定需要時間,不能綁定之後立即調用,否則會出現空異常。

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void aidl(View view) {
        try {
            mIAidlService.sayHello();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

}



抱歉!评论已关闭.