Android 之路56---AIDL建立远程通信

本文详细介绍Android服务的创建、启动、绑定等操作,并通过示例代码演示如何使用AIDL实现跨应用的服务通信。

导读

1.案例
这里写图片描述

案例

这里写图片描述

app AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hala.srevicedemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.hala.myservice"/>
            </intent-filter>
        </service>
    </application>

</manifest>

这里写图片描述

app 布局文件 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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.hala.srevicedemo.MainActivity">

   <Button
       android:id="@+id/start"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="启动服务"
       android:onClick="onClick"/>

    <Button
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="停止服务"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/bind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="绑定服务"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/unbind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="解锁服务"
        android:onClick="onClick"/>

</LinearLayout>

app MainActivity.java

package com.hala.srevicedemo;

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 android.view.View;

public class MainActivity extends AppCompatActivity {

    private ServiceConnection conn=new ServiceConnection() {
        //当客户端正常连接着Service时,执行服务的绑定操作会被调用
        //如果没有ServiceConnection,会出现绑定后解绑,无法再次绑定的情况
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("TAG","hello,bind again");

            //以下是关于aidl操作
            IMyAidlInterface imal=IMyAidlInterface.Stub.asInterface(service);

            try {
                imal.showProgress();
            } catch (RemoteException e) {
                e.printStackTrace();
            }

            /*
            以下是关于Binder操作的代码只能在同进程间传递(MyService中也有修改)
            //强制转换
            MyService.MyBinder mb=(MyService.MyBinder)service;
            int step=mb.getProcess();
            Log.d("TAG","当前进度为:"+step);
            */
        }

        //当客户端和服务的连接丢失了
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };


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

    public void onClick(View view){

        //start 与 stop为一组
        //bind 与 unbind为一组
        switch (view.getId()){
            case R.id.start:
                //如果服务已经创建,重复启动不会重新创建,操作的是同一个服务,除非先销毁
                Intent it=new Intent(this,MyService.class);
                startService(it);
                break;

            case R.id.stop:
                //这里虽然是两个intent,但确实针对同一个对象
                Intent it1=new Intent(this,MyService.class);
                stopService(it1);
                break;

            case R.id.bind:
                //绑定服务:最大作用是对Service执行的任务进行监控,如下载到哪了,音乐播放到哪了
                Intent it2=new Intent(this,MyService.class);
                bindService(it2,conn,BIND_AUTO_CREATE);
                break;

            case R.id.unbind:
                //要指明解绑哪个连接,所以conn要设置为全局
                unbindService(conn);
                break;
        }

    }
}

app MyService.java

package com.hala.srevicedemo;

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

//实现进度监控(两个要素):
//ServicerConnection:用与绑定客户端和Service->见MainActivity.java有说明
//IBinder:在android中用于远程操作对象的一个基本接口
//因为IBinder强制要求我们实现一些方法,而Binder类是给所有强制方法实现了一个空方法
//在开发中我们会自己写一个内部类,继承Binder类,在里边写自己的方法
public class MyService extends Service {

    public static final String TAG = "TAG";
    private int i;

    public MyService() {
    }

    /**
     * 创建
     */
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG,"服务创建了");
        //开启一个线程,模拟耗时任务
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                for(i = 0; i <100; i++){
                        sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * 启动
     * @param intent
     * @param flags
     * @param startId
     * @return
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG,"服务启动了");
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 绑定
     * @param intent
     * @return
     */
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.e(TAG,"服务绑定了");
        return new IMyAidlInterface.Stub() {
            @Override
            public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

            }

            @Override
            public void showProgress() throws RemoteException {
                Log.d("TAG","当前进度是:"+i);
            }
        };

        //Binder操作返回值
        //return new MyBinder();

    }


    /*
    关于Binder操作自定义操作
    class MyBinder extends Binder{
        public int getProcess(){
            return i;
        }

    }
    */

    /**
     * 解绑
     * @param intent
     * @return
     */
    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG,"服务解绑了");
        return super.onUnbind(intent);
    }

    /**
     * 销毁
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG,"服务销毁了");
    }
}

这里写图片描述

这里写图片描述
⚠️创建aidl文件完成后,要重新build,生成java文件,因为真正起作用的是这个java文件,java文件在哪里查看呢?
转换为工程视图,如下下图所示找到那个文件

这里写图片描述

这里写图片描述

app aidl文件 IMyAidlInterface.aidl

// IMyAidlInterface.aidl
package com.hala.srevicedemo;

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

interface IMyAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);

    //定义自己所需要的方法
    void showProgress();
}

aidldemo 布局文件 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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.hala.aidldemo.MainActivity">

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="远程启动服务"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="远程停止服务"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/bind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="远程绑定服务"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/unbind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="远程解锁服务"
        android:onClick="onClick"/>

</LinearLayout>

在工程视图下如下图复制aidl到要建立远程联系的app,注意同样需要重新build

这里写图片描述

aidldemo MainActivity.java

package com.hala.aidldemo;

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 com.hala.srevicedemo.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

    ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //使用了aidl
            IMyAidlInterface imal=IMyAidlInterface.Stub.asInterface(service);
            try {
                imal.showProgress();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

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

    public void onClick(View view){
        switch(view.getId()){
            case R.id.start:
                //Android 5.0后service的intent一定要显式声明
                Intent it=new Intent();
                //参数为Service起的别名
                it.setAction("com.hala.myservice");
                //参数为Service所在的包名
                it.setPackage("com.hala.srevicedemo");
                startService(it);
                break;
            case R.id.stop:
                //Android 5.0后service的intent一定要显式声明
                Intent it1=new Intent();
                it1.setAction("com.hala.myservice");
                it1.setPackage("com.hala.srevicedemo");
                stopService(it1);
                break;
            case R.id.bind:
                //Android 5.0后service的intent一定要显式声明
                Intent it2=new Intent();
                it2.setAction("com.hala.myservice");
                it2.setPackage("com.hala.srevicedemo");
                bindService(it2,conn,BIND_AUTO_CREATE);
                break;
            case R.id.unbind:
                unbindService(conn);
                break;
        }
    }
}

显示结果

aidldemo

这里写图片描述

app

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值