IDEA创建AIDL示例

在网上看了好几篇关于AIDL的入门的文章终于写出了我的第一个AIDL入门例子,特此发帖
—— 每个程序员都有一颗开源的心

服务端:

1.服务端创建aidl,build project
package com.fafa.aidlserver;

interface RemoteService {
    String getInfor(String s);
}

创建aidl
重建项目

2.创建service
package com.fafa.aidlserver;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

/**
 * Created by ZhouBing on 2017/8/14.
 */
public class MyService extends Service{

    private final IBinder binder = new RemoteService.Stub() {


        @Override
        public String getInfor(String s) throws RemoteException {
            return "我是Service返回的字符串";
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        Log.d("aidl", "aidlService--------------onBind");
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("aidl", "aidlService--------------onCreate");
    }
}
3.暴露服务
 <service android:name=".MyService">
            <intent-filter>
                <action android:name="com.fafa.aidlserver.MyService"/>
            </intent-filter>
 </service>

客户端

1.拷贝aidl自动生成的接口文件到客户端,注意一定要连包一起拷过去,不然出错

服务器 客户端

2.绑定服务
package com.fafa.aidlclient;

import android.content.ComponentName;
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.util.Log;
import com.fafa.aidlserver.RemoteService;

public class MainActivity extends AppCompatActivity {


    public final static String TAG = "aidl";
    private RemoteService myInterface;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myInterface = RemoteService.Stub.asInterface(service);
            Log.i(TAG, "连接Service 成功");
            try {
                String s = myInterface.getInfor("我是Activity传来的字符串");
                Log.i(TAG, "从Service得到的字符串:" + s);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG, "连接Service失败");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }

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

        Intent intent = new Intent();
        intent.setAction("com.fafa.aidlserver.MyService");
        intent.setPackage("com.fafa.aidlserver");
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);
    }
}
安装服务端,运行客户端

这里写图片描述

这篇博客可以看下 http://blog.csdn.net/wuzhipeng1991/article/details/40376507

代码 传送门

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值