简单AIDL使用

2023-10-31

1、AIDL准备注意

  • build.gradle
buildFeatures{
    aidl true
}
  • 权限

在调用的APP添加目标APP可见(客户端),否则会有no Font service

<queries>
    <package android:name="希望可见的包名" />
    <intent>
        <action android:name="希望可见的服务的action"/>
    </intent>
</queries>

2、创建服务

  • 客户端
1、创建AIDL接口文件: LtAidlInterface.aidl 
内容如下
package com.ltcode.library_aidl;
import com.ltcode.library_aidl.LtAidlMsgBean;
interface LTAidlInterface {
    /**
     * 注意引入完整包, in = 入, out = 返回
     */
    void callback(in LtAidlMsgBean ltBean);
}




2、创建自定义类型文件:LtAidlMsgBean.aidl
内容如下:

// LtAidlMsgBean.aidl
package com.ltcode.library_aidl;
parcelable LtAidlMsgBean;




3、在报名一样的java中实现 LtAidlMsgBean

package com.ltcode.library_aidl;
import android.os.Parcel;
import android.os.Parcelable;
public class LtAidlMsgBean implements Parcelable {
    private String name;
}


4、绑定服务bingService

        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.ltcode.helpclick","com.ltcode.helpclick.service.LtAidlServer"));

        context.bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                LTAidlInterface aidlInterface = LTAidlInterface.Stub.asInterface(iBinder);
                LtAidlMsgBean bean = new LtAidlMsgBean();
                bean.setName("haha");
                bean.setOk(true);
                try {
                    aidlInterface.callback(bean);
                }
}

3、服务器实现

public class LtAidlServer extends Service {
    private static final String TAG = "LtAidlServer";
    LtAidlBindImpl bind = new LtAidlBindImpl();
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return bind;
    }

    public static class LtAidlBindImpl extends LTAidlInterface.Stub {
        private static final String TAG = "LtAidlBindImpl";

        @Override
        public void callback(LtAidlMsgBean ltBean) throws RemoteException {
            LogUtil.d(TAG, ltBean.toString());
        }
    }
}

 XML注册组件

        <!--             android:enabled="true"
                    android:directBootAware="true"-->
        <service
            android:name="com.ltcode.helpclick.service.LtAidlServer"
            android:exported="true"
            >
            <intent-filter>
                <action android:name="com.lecode.aidl" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>

问题处理:

1、Service starting has been prevented by iaware or trustsbase sInfo ServiceInfo{6af6a6d com.example.test001.service.MyServicetest}

最后现在自定义的APK不允许关联启动,自己调试的时候可以在  手机管家----应用启动管理----APKB,关闭自动管理,把允许自启动、允许关联启动、允许后台启动打开,然后在ApkA中调用启动服务接口,就可以后台打开ApkB中的服务了

附带android:exported="true"说明

android:exported="true"  , 默认false, true 允许其他应用调用, 一帮可以配合权限


1、目标页面添加权限
    <permission android:name="com.ltcode.lttest.permission.Two" />

   <activity
            android:name=".TwoActivity"
            android:exported="true"
            android:launchMode="singleInstance"
            android:permission="com.ltcode.lttest.permission.Two"
            android:process=":two">



2、调用方申请权限

    <uses-permission android:name="com.ltcode.lttest.permission.Two" />

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

简单AIDL使用 的相关文章

随机推荐