android AIDL(二)

一 实现

1.工程目录

155134_j3A2_2832792.png

2.创建AIDL文件

155236_2BuC_2832792.png

在Service工程中创建了aidl后,直接原样复制一份aidl到client中。

3.工具生成

aidl创建完成后,Rebuild一下工程后,会在build目录下生成aidl对应的java文件:

155551_5NGQ_2832792.png

4.配合类RemoteService实现

关键是mBinder的实现

RemoteService.java

package com.cary.aidl.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 *  RemoteService
 */

public class RemoteService extends Service {

    private static final String TAG = RemoteService.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind");
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.i(TAG, "onDestroy");
        super.onDestroy();
    }

    private IManager.Stub mBinder = new IManager.Stub() {
        @Override
        public void eat() throws RemoteException {
            Log.i(TAG, "eat");
        }

        @Override
        public void heart() throws RemoteException {
            Log.i(TAG, "heart");
        }
    };

}

清单文件注册:

注意里面的action,这是给客户端调用时用的动作。

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true" android:theme="@style/AppTheme">

    <service android:name=".RemoteService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.cary.aidl.RemoteService"/>
        </intent-filter>
    </service>

</application>

5.客户端调用

首先要实现ServiceConnection,在连接成功之后,会获得一个IManager的实例,通过这个实例即可调用方法。

package com.cary.aidl.client;

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

import com.cary.aidl.service.IManager;

/**
 * Created by xi
 */

public class MainActivity extends Activity {

    private IManager mManager;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mManager = IManager.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mManager = null;
        }
    };

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

    /**
     * 启动服务
     * @param view
     */
    public void start(View view){
        Intent intent = new Intent();
        intent.setAction("com.cary.aidl.RemoteService");
        startService(intent);
    }

    /**
     * 停止
     * @param view
     */
    public void stop(View view){
        Intent intent = new Intent();
        intent.setAction("com.cary.aidl.RemoteService");
        stopService(intent);
    }

    /**
     * 绑定
     * @param view
     */
    public void bind(View view){
        Intent intent = new Intent();
        intent.setAction("com.cary.aidl.RemoteService");
        bindService(intent, conn, BIND_AUTO_CREATE);
    }

    /**
     * 解绑
     * @param view
     */
    public void unbind(View view){
        Intent intent = new Intent();
        intent.setAction("com.cary.aidl.RemoteService");
        unbindService(conn);
    }

    public void eat(View view){
        try {
            mManager.eat();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

 

二、写AIDL注意事项

1. 客户端和服务端的AIDL接口文件所在的包必须相同
2. 需要一个Service类的配合

转载于:https://my.oschina.net/kun123/blog/871257

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值