ANDROID笔记:AIDL介绍

package com.example.android_server.aidlService;



import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.os.RemoteException;



import com.example.android_server.aidlService.IStudent.Stub;



public class MyAIDLService extends Service {

    StudentBinder binder=null;

    @Override

    public void onCreate() {

        super.onCreate();

        binder=new StudentBinder();

    }



    @Override

    public IBinder onBind(Intent intent) {

        return binder;

    }



    /**

     * 自定义一个Binder,继承存根类

     * 

     * @author Administrator

     * 

     */

    public class StudentBinder extends Stub {



        @Override

        public int getCount() throws RemoteException {

            return 1;

        }

    }

}

IStudent.aidl

package com.example.android_server.aidlService;

interface IStudent

{

int getCount();

}

在manifest中注册服务:

 <service android:name="com.example.android_server.aidlService.MyAIDLService" >

            <intent-filter>

                <action android:name="com.example.aidlservice" />

            </intent-filter>

        </service>

 

另一个项目中的调用该Service

package com.example.android_serviceclient;



import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.Handler;

import android.os.IBinder;

import android.os.Message;

import android.os.RemoteException;

import android.widget.TextView;

import android.widget.Toast;



import com.example.android_server.aidlService.IStudent;

import com.example.android_server.aidlService.IStudent.Stub;



public class MainActivity extends Activity implements Handler.Callback {

    private IStudent iStudent;

    private TextView textView=null;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        textView=(TextView) findViewById(R.id.text);

        

        Intent intent = new Intent();

        intent.setAction("com.example.aidlservice");

        startService(intent);

        bindService(intent, connection, BIND_AUTO_CREATE);

        

        Handler handler = new Handler(this);

        handler.sendEmptyMessageDelayed(1, 2000);



    }

    private ServiceConnection connection = new ServiceConnection() {



        @Override

        public void onServiceDisconnected(ComponentName name) {

        }



        @Override

        public void onServiceConnected(ComponentName name, IBinder service) {

            System.out.println("onServiceConnected");

            iStudent=Stub.asInterface(service);

        }

    };



    @Override

    public boolean handleMessage(Message msg) {

        try {

            textView.setText(iStudent.getCount() +"");

            Toast.makeText(MainActivity.this, "得到的值:"+iStudent.getCount() + "", 200)

                    .show();

        } catch (RemoteException e) {

            e.printStackTrace();

        }

        return false;

    }

}

aidl是Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口.

你可能感兴趣的:(android)