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

来源:互联网 发布:骁龙优化的浏览器 编辑:程序博客网 时间:2024/06/06 10:51

Server端

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

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

我建的一个叫
IMyAIDLText.aidl


// IMyAIDLText.aidlpackage com.mo.aidltext;// Declare any non-default types here with import statementsinterface 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,连接成功后,就可以愉快的通信了