IPC之AIDL -- APK之间进程通信(入门)

Server端

新建一个Activity,一个Service,一个AIDL

Activity建好就行,接着在任意目录下建立一个AIDL

我建的一个叫
IMyAIDLText.aidl


// IMyAIDLText.aidl
package com.mo.aidltext;

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

interface IMyAIDLText {
    String getString(String str);
}

接着建立服务AIDL的Service
MyService.java

package com.mo.aidltext;

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

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new IMyService();
    }

    class IMyService extends IMyAIDLText.Stub {

        @Override
        public String getString(String str) throws RemoteException {
            Log.e("mo", "getString:"+str);
            return "server";
        }
    }

    @Override
    public void onCreate() {
        Log.e("mo", "onCreate");
        super.onCreate();
    }

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

以上server端就写好了

Client端

这边需要两步

1)建一个Module,把Server的AIDL拷过来(注意,连同包整个拷过来,以此来保证包名一致。aidl的包名一样,系统会认为两个程序中的接口是同一个)

2)Activity

package com.mo.myaidlservice;

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

import com.mo.aidltext.IMyAIDLText;

public class MainActivity extends AppCompatActivity {
    private IMyAIDLText aidlText;
    private ServiceConnection serviceConnection =new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            aidlText = IMyAIDLText.Stub.asInterface(iBinder);
            Toast.makeText(MainActivity.this, "Connect success!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

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

    public void sandStr(View view) {
        try {
            String s=aidlText.getString(((EditText) findViewById(R.id.client_editText)).getText().toString());
            Toast.makeText(this,"client:"+s,Toast.LENGTH_SHORT).show();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void binder(View view){
        Intent intent = new Intent();
        intent.setPackage("com.mo.aidltext");
        //intent.setAction("com.mo.myservice");
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

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

对应的XML为

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.mo.myaidlservice.MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        android:onClick="sandStr"
        android:layout_marginTop="26dp"
        app:layout_constraintTop_toBottomOf="@+id/client_editText"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" />

    <EditText
        android:id="@+id/client_editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="131dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.503" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="binder"
        android:onClick="binder"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/client_editText"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

由此启动Server端,启动Client端,点击binder,连接成功后,就可以愉快的通信了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值