码迷,mamicode.com
首页 > 系统相关 > 详细

AIDL跨进程通信

时间:2017-03-23 16:08:19      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:text   service   code   version   新版本   instance   listener   idt   oid   

注:慕课网详细教程:http://www.imooc.com/learn/606

 

一、线程通信应用场景

  • AIDL   IPC  多个应用程序  多线程
  • Binder   IPC  多个应用程序  没有多线程
  • Messenger   IPC  没有多线程

什么是IPC:http://www.jianshu.com/p/c0a5bdbba3c2

 

二、案例

技术分享

 

三、工程结构

创建一个工程,即服务端

技术分享

 

工程中创建一个Module,即客户端

技术分享

 

四、服务端

一、创建AIDL文件夹

 

二、创建AIDL接口文件

技术分享
// TestAidl.aidl
package com.example.aidlservicedemo;

// Declare any non-default types here with import statements

interface TestAidl {

       //计算两个数的和
       int add(int num1,int num2);

}
TestAidl

注意:创建完成后,需要编译

技术分享

 

三、服务类

技术分享
package com.example.aidlservicedemo;

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

/**
 * Created by 袁磊 on 2017/3/23.
 */
public class IRremoteService extends Service {
    /**
     * 当客户端绑定到该服务的时候,会执行
     *
     * @param intent
     * @return
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return iBinder;
    }


    private IBinder iBinder = new TestAidl.Stub() {
        @Override
        public int add(int num1, int num2) throws RemoteException {
            Log.d("TAG", "收到了远程的请求,输入的参数是" + num1 + "和" + num2);
            return num1 + num2;
        }
    };
}
IRremoteService

 

四、清单文件中注册Service并设置可供外部程序调用

技术分享

 

五、客户端

一、创建AIDL文件夹

 

二、创建AIDL接口文件(一定要与服务端保持一致)

技术分享
// TestAidl.aidl
package com.example.aidlservicedemo;

// Declare any non-default types here with import statements

interface TestAidl {

       //计算两个数的和
       int add(int num1,int num2);

}
TestAidl

注意:创建完成后,需要编译

技术分享

 

三、主要代码

技术分享
package com.example.aidlclient;

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.Button;
import android.widget.EditText;

import com.example.aidlservicedemo.TestAidl;

public class MainActivity extends AppCompatActivity {

    private EditText etNum1;
    private EditText etNum2;
    private EditText etResult;
    private Button btnAdd;

    private TestAidl testAidl;


    private ServiceConnection conn = new ServiceConnection() {
        //当绑定上服务的时候
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            //拿到了远程的服务
            testAidl = TestAidl.Stub.asInterface(service);
        }

        //当服务断开的时候
        @Override
        public void onServiceDisconnected(ComponentName name) {
            //回收资源
            testAidl = null;
        }
    };

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

        initView();
        initListener();

        //软件一启动的时候就绑定服务
        bindService();

    }


    private void initListener() {
        btnAdd.setOnClickListener(myOnClickListener);
    }

    private void initView() {
        etNum1 = (EditText) findViewById(R.id.et_num1);
        etNum2 = (EditText) findViewById(R.id.et_num2);
        etResult = (EditText) findViewById(R.id.et_result);
        btnAdd = (Button) findViewById(R.id.btn_add);
    }

    private View.OnClickListener myOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int num1 = Integer.parseInt(etNum1.getText().toString());
            int num2 = Integer.parseInt(etNum2.getText().toString());

            try {
                //调用远程的服务
                int res = testAidl.add(num1, num2);
                etResult.setText(res + "");
            } catch (RemoteException e) {
                e.printStackTrace();
                etResult.setText("远程出错");
            }

        }
    };

    private void bindService() {
        //获取到服务端

        Intent intent = new Intent();
        //新版本必须显示Intent启动绑定服务
        intent.setComponent(new ComponentName
                ("com.example.aidlservicedemo",
                        "com.example.aidlservicedemo.IRremoteService"));

        //设置为绑定的时候自动创建服务
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

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

布局文件:

技术分享
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <EditText
        android:id="@+id/et_num1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+" />

    <EditText
        android:id="@+id/et_num2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="=" />

    <EditText
        android:id="@+id/et_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false" />

    <Button
        android:id="@+id/btn_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AIDL远程计算" />
</LinearLayout>
activity_main

 

四、1.运行服务端,即app

2.运行客户端,即aidlclient

技术分享

 

六、运行效果

技术分享

 

AIDL跨进程通信

标签:text   service   code   version   新版本   instance   listener   idt   oid   

原文地址:http://www.cnblogs.com/bky1225987336/p/6605437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!