如何通过AIDL实现跨应用绑定Service

实现AIDL实现跨应用绑定Service的大体步骤如下:

1.目标Service需要在清单文件中配置exported="true"属性,以允许外部应用访问.
2.需要在目标Service所在的工程中创建AIDL文件,它其实就是一个接口,只是没有public权限修饰符;通过Android Studio可以很轻松的使用向导来帮我们自动完成.
3.在目标Service的onBind回调方法中需要返回一个创建AIDL文件时自动创建的Stub对象,改对象是继承了android.os.Binder对象,同时实现了AIDL接口的一个抽象类.
4.绑定外部服务同样需要通过显式意图来实现,因为在android5.0之后就必须要通过显式意图来访问.

步骤1:

<service
            android:name=".AppService"
            android:enabled="true"
            android:exported="true" />

步骤2:

创建完AIDL文件后,工具会帮我自动创建相应的接口代码:
// IAppServiceInterface.aidl
package com.example.mchenys.aidlservicedemo;

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

interface IAppServiceInterface {
    /**
     * 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);
}
步骤3:在目标Service中的onBind方法中返回一个Stub对象,目标Service类的完整代码如下:
package com.example.mchenys.aidlservicedemo;

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

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

    @Override
    public IBinder onBind(Intent intent) {
        Log.i("AppService", "onBind");

        //通过向导生成了aidl文件后,需要Rebuild Project后,在onBind方法中就可以直接创建IAppServiceInterface.Stub对象了.
        return new IAppServiceInterface.Stub() {
            @Override
            public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
            }
        };
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("AppService", "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (null != intent) {
            Log.i("AppService", "onStartCommand接收到的数据是:" + intent.getStringExtra("data"));
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("AppService", "onDestroy");
    }
}
步骤4:在其他应用中创建显式意图来绑定目标Service,代码如下:
package com.example.mchenys.app2;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

/**
 * Created by mChenys on 2015/11/1.
 */
public class App2Activity2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2_app2);
        //创建绑定的目标AppService的显示意图
        final Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.example.mchenys.aidlservicedemo", "com.example.mchenys.aidlservicedemo.AppService"));
        //创建ServiceConnection接口的实现类对象,用于监听Service的链接和断开连接
        final MyServiceConnection conn = new MyServiceConnection();
        //绑定服务
        findViewById(R.id.id_btn_bind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindService(intent, conn, BIND_AUTO_CREATE);
            }
        });
        //解绑服务
        findViewById(R.id.id_btn_unbind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(conn);
            }
        });
    }

    /**
     * 创建ServiceConnection接口的实现类
     */
    private class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("App2Activity2", "目标Service已连接");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("App2Activity2", "目标Service已断开连接");

        }
    }

}

App2Activity2 中有2个Button,分别用于绑定和解绑外部服务.


分别点击上图的绑定外部服务和解绑外部服务后,观察控制台的log信息:
11-01 12:00:20.649 29883-29883/? I/AppService: onCreate
11-01 12:00:20.650 29883-29883/? I/AppService: onBind
11-01 12:00:20.659 29956-29956/? D/App2Activity2: 目标Service已连接
11-01 12:00:30.498 29883-29883/? I/AppService: onDestroy
至此,绑定和解绑外部服务的功能就实现了.






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值