Android AIDL代码实例





两个应用的aidl文件夹中的内容相同,包名为服务器的包名。

IMyAidlInterface.aidl

// IMyAidlInterface.aidl
package com.zhang.aidldemo;

import com.zhang.aidldemo.TimerCallback;

interface IMyAidlInterface {

    void setData(String data);

    void regCallback(TimerCallback callback);//注册回调接口
    void unRegCallback(TimerCallback callback);
}


TimerCallback.aidl

package com.zhang.aidldemo;

interface TimerCallback {

    void onTimer(String data);//回调函数
}


MyService

package com.zhang.aidldemo;

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

public class MyService extends Service {

    //回调函数管道   列表
    private RemoteCallbackList<TimerCallback> callbackList = new RemoteCallbackList<>();

    private String data = "原始数据";
    private int numIndex = 0;
    private boolean serviceRun = false;

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {

        return new IMyAidlInterface.Stub() {
            //可以将AIDL的IMyAidlInterface理解为特殊的Binder(),在同一个应用与Service互
            // 通只需要返回一个Binder()即可,如要传出消息,可在service中建立一个callback
            // 接口,用它来把service的数据暴露出去。
            // 在不同的应用间则要使用AIDL。
            @Override
            public void setData(String data) throws RemoteException {
                MyService.this.data = data;
            }

            @Override
            public void regCallback(TimerCallback callback) throws RemoteException {
                //service onBind 返回 IMyAidlInterface.Stub()  在其中注册回调函数
                callbackList.register(callback);
            }

            @Override
            public void unRegCallback(TimerCallback callback) throws RemoteException {
                callbackList.unregister(callback);
            }
        };
    }

    @Override
    public void onCreate() {
        super.onCreate();

        new Thread() {
            @Override
            public void run() {
                super.run();

                serviceRun = true;
                for (numIndex = 0; serviceRun; numIndex++) {
                    //回调处理
                    int count = callbackList.beginBroadcast();
                    while (count-- > 0) {
                        try {
                            callbackList.getBroadcastItem(count).onTimer(data + ":" + numIndex);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }

                    callbackList.finishBroadcast();

                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        serviceRun = false;
    }
}


client的MainActivity

package com.zhang.clientapp;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zhang.aidldemo.IMyAidlInterface;
import com.zhang.aidldemo.TimerCallback;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {

    private EditText etMsg;
    private TextView tvMsg;
    private Intent sIntent;
    private IMyAidlInterface binder = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etMsg = (EditText) findViewById(R.id.et_msg);
        tvMsg = (TextView) findViewById(R.id.tv_back);

        findViewById(R.id.btn_bind).setOnClickListener(this);
        findViewById(R.id.btn_unbind).setOnClickListener(this);
        findViewById(R.id.btn_send_msg).setOnClickListener(this);

        sIntent = new Intent();
        sIntent.setComponent(new ComponentName("com.zhang.aidldemo", "com.zhang.aidldemo.MyService"));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_bind:
                bindService(sIntent, this, Context.BIND_AUTO_CREATE);
                break;
            case R.id.btn_unbind:
                if (binder != null) {
                    try {
                        binder.unRegCallback(callback);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    unbindService(this);
                }
                break;
            case R.id.btn_send_msg:
                if (binder != null) {
                    try {
                        binder.setData(etMsg.getText().toString());
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        binder = IMyAidlInterface.Stub.asInterface(iBinder);
        try {
            binder.regCallback(callback);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        if (binder != null) {
            try {
                binder.unRegCallback(callback);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (binder != null) {
            try {
                binder.unRegCallback(callback);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    private TimerCallback.Stub callback = new TimerCallback.Stub() {
        @Override
        public void onTimer(final String data) throws RemoteException {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvMsg.setText(data);
                }
            });
        }
    };
}





 

转载于:https://www.cnblogs.com/changchou/p/6203814.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值