Android远程服务——AIDL

来源:互联网 发布:抽风解说 知乎 编辑:程序博客网 时间:2024/05/29 14:56

        做过安卓开发的朋友对service服务肯定不陌生,它运行在后台,没有界面。但是可以在后台完成我们的很多的耗时任务。是安卓中非常强大的组件。在用它的时候,我们一般是用startService()或者bindService()来使用它。这都是调用本地的服务,就是服务和调用者在同一个应用里。现实的开发中我们经常也用到调用应用以外的的服务,这个时候,调用者和服务不再同一个应用中。我们就会用到AIDL(Android Interface definition language),今天我们来谈一下AIDL。

     每一个应用程序都是运行在自己的一个独立的进程里面,进程是操作系统分配独立空间的一个单位,进程中的数据都是独立的,默认情况下不能互相访问,什么时候可以互相访问呢,这就需要为哦们遵循一定的规则。

原理:

  

   因为进程间的通信默认情况下是不能通信的,要想通信就得通过操作系统来实现,A进程在系统中申请一块内存存贮数据,然后B进程去这个内存中存储数据

首先 我们先来创建远程的应用:


首先创建我们的aidl文件,在工程上右键直接创建aidl这样android studio 会自动给我们生成目录结构。 aidl文件中的方法都是公共的 没有修饰符

// MiddlePersonInterface.aidlpackage chs.com.myaidl;// Declare any non-default types here with import statementsinterface MiddlePersonInterface {   void CallRemoteMethod();}

创建完成之后 build一下,在下面的目录中就会自动生成一个MiddlePersonInterface的.java文件



我们需要做的就是在我们的远程服务中继承这里面的抽象方法

/** Local-side IPC implementation stub class. */public static abstract class Stub extends android.os.Binder implements chs.com.myaidl.MiddlePersonInterface{
注释也很清楚 进程间通讯(IPC)就实现这里的stub类

下面写我们的远程服务:

package chs.com.myaidl;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.os.RemoteException;import android.support.annotation.Nullable;import android.util.Log;import android.widget.Toast;import chs.com.myaidl.MiddlePersonInterface.Stub;/** * Created by Administrator on 2016/2/28. */public class RemoteService extends Service {    private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            Toast.makeText(RemoteService.this,"远程服务的方法被调用。。。",Toast.LENGTH_SHORT).show();        }    };    @Nullable    @Override    public IBinder onBind(Intent intent) {        return new MyBinder();    }    @Override    public void onCreate() {        super.onCreate();        Log.i("RemoteService", "远程服务已被创建。。。");        Toast.makeText(this,"远程服务已被创建。。。",Toast.LENGTH_SHORT).show();    }    @Override    public void onDestroy() {        super.onDestroy();        Log.i("RemoteService", "远程服务已被销毁。。。");        Toast.makeText(this,"远程服务已被销毁。。。",Toast.LENGTH_SHORT).show();    }    private void myBindRemoteService(){        Log.i("RemoteService", "远程服务的方法被调用。。。");    }//        Toast.makeText(this,"远程服务的方法被调用。。。",Toast.LENGTH_SHORT).show();    /**     * 定义一个中间人来调用远程服务     * 远程服务集成IPC的一个实现类 (IPC)系统的进程间通信     */    private class MyBinder extends MiddlePersonInterface.Stub{        @Override        public void CallRemoteMethod() throws RemoteException {            myBindRemoteService();            handler.sendEmptyMessage(0);        }    }}
在远程服务中写一个方法
在远程服务中定义一个内部类来集成上面的,stub类,在其中调用我们的方法。

在onBind方法中返回这个类。

在清单文件中注册sercvice并给它设置action

 <service android:name=".RemoteService">            <intent-filter>                <action android:name="com.chs.remoteservice"></action>            </intent-filter>        </service>


然后我们就可以写我们的调用端的应用了。
这个时候我们要将服务端应用中的aidl的完整的包名 和文件本身 原封不动的复制到这个应用中一份

在activity中写两个按钮来看效果:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical">    <Button        android:id="@+id/btn1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:text="绑定服务" />    <Button        android:id="@+id/btn3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:text="调用远程服务的方法" /></LinearLayout>

Mainactivity中:

package chs.com.mybindremoteservice;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.view.View;import chs.com.myaidl.MiddlePersonInterface;public class MainActivity extends AppCompatActivity {    private MyServiceConn conn;    private MiddlePersonInterface middlePersonInterface;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Intent intent = new Intent();                intent.setPackage("chs.com.myaidl");                intent.setAction("com.chs.remoteservice");                conn = new MyServiceConn();                bindService(intent, conn, BIND_AUTO_CREATE);            }        });        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                try {                    middlePersonInterface.CallRemoteMethod();                } catch (RemoteException e) {                    e.printStackTrace();                }            }        });    }    private class  MyServiceConn implements ServiceConnection {        @Override        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {            middlePersonInterface = MiddlePersonInterface.Stub.asInterface(iBinder);        }        @Override        public void onServiceDisconnected(ComponentName componentName) {        }    }    @Override    protected void onDestroy() {        super.onDestroy();        unbindService(conn);    }}

MainActivity中 首先我们启动服务的时候,就不能像同一个应用中那么启动了,我们得通过其包名和action来启动服务。

绑定的时候用到ServiceConnection 在其onServiceConnection方法中通过

MiddlePersonInterface.Stub.asInterface(iBinder);来找到我们的调用方法的类的对象从而调用服务中的方法。

效果:




0 0
原创粉丝点击