• Android Studio的笔记--aidl实现和调用


    aidl实现

    新建aidl实现工程

    新建一个工程。工程名testaidl。包名com.lxh.testaidl。修改配置文件

    build.gradle

    a
    修改sdk配置,修改version,修改生成apk命名,修改编译混淆配置,增加系统签名
    文件位置:\testaidl\app
    文件名:build.gradle
    在这里插入图片描述

    plugins {
        id 'com.android.application'
    }
    android {
        compileSdkVersion 30
        buildToolsVersion "30.0.2"
        defaultConfig {
            applicationId "com.lxh.testaidl"
            minSdkVersion 28
            targetSdkVersion 30
            versionCode 1
            def date = new Date().format("yyyyMMddHHmm" , TimeZone.getTimeZone("GMT+08"))
            versionName "testaidl-V1.0-"+date
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
        android.applicationVariants.all {
            variant ->
                variant.outputs.all {
                    outputFileName = new File(defaultConfig.versionName + ".apk");
                }
        }
        signingConfigs {
            release {
                storeFile file("../keystore/mykey.jks")
                storePassword '123456'
                keyAlias '_mykey'
                keyPassword '123456'
            }
            debug {
                storeFile file("../keystore/mykey.jks")
                storePassword '123456'
                keyAlias '_mykey'
                keyPassword '123456'
            }
        }
        buildTypes {
            release {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                signingConfig signingConfigs.release
            }
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    dependencies {
        implementation 'androidx.appcompat:appcompat:1.2.0'
        implementation 'com.google.android.material:material:1.2.1'
        implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
        testImplementation 'junit:junit:4.+'
        androidTestImplementation 'androidx.test.ext:junit:1.1.2'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    b
    修改build variants中active build variant 从debug改成release
    在这里插入图片描述
    c
    签名文件
    文件位置:\testaidl\keystore
    文件名:mykey.jks

    proguard-rules.pro

    增加混淆
    文件位置:\testaidl\app
    文件名:proguard-rules.pro

    -dontusemixedcaseclassnames
    -dontskipnonpubliclibraryclasses
    -verbose
    -dontoptimize
    -dontpreverify
    -keepattributes *Annotation*
    -keep public class com.google.vending.licensing.ILicensingService
    -keep public class com.android.vending.licensing.ILicensingService
    -keepclasseswithmembernames class * {
    native <methods>;
    }
    -keepclassmembers public class * extends android.view.View {
    void set*(***);
    *** get*();
    }
    -keepclassmembers class * extends android.app.Activity {
    public void *(android.view.View);
    }
    -keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
    }
    -keepclassmembers class * implements android.os.Parcelable {
    public static final android.os.Parcelable$Creator CREATOR;
    }
    -keepclassmembers class **.R$* {
    public static <fields>;
    }
    -dontwarn android.support.**
    -keep class android.support.annotation.Keep
    -keep @android.support.annotation.Keep class * {*;}
    -keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
    }
    -keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
    }
    -keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
    }
    -optimizationpasses 5
    -dontusemixedcaseclassnames
    -ignorewarnings
    -keep class com.lxh.testaidl.USTservice { *; }
    -keep public class com.lxh.testaidl.aidl.UST {*;}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    增加aidl文件

    增加aidl文件
    文件位置:testaidl\app\src\main\aidl\com\lxh\testaidl\aidl
    文件名:UST.aidl

    package com.lxh.testaidl.aidl;
    
    interface UST {
    
        int installPackage(String filepath,int indicator,String version);
       
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    增加aidl实现

    编译一遍,能import aidl出来
    import com.lxh.testaidl.aidl.UST;

    aidl实现服务

    新增service,命名USTservice

    package com.lxh.testaidl;
    
    import android.app.IntentService;
    import android.content.Intent;
    import android.os.IBinder;
    import android.os.RemoteException;
    import android.util.Log;
    
    import androidx.annotation.Nullable;
    
    import com.lxh.testaidl.aidl.UST;
    
    /**
     * create by lxh on 2022/12/12 Time:14:35
     * tip:
     */
    public class USTservice extends IntentService {
        private static final String TAG = "USTservice lxh";
    
        @Override
        protected void onHandleIntent(Intent intent) {
            Log.i(TAG, "onHandleIntent");
            if (intent != null) {
                final String action = intent.getAction();
            }
        }
    
        public TatvUSTservice() {
            super("USTservice");
            Log.i(TAG, "USTservice");
        }
    
        public UST.Stub asBinder = new UST.Stub() {
            @Override
            public int installPackage(String filepath, int indicator, String version) throws RemoteException {
                Log.i(TAG, "installPackage(" + filepath + "," + indicator + "," + version);
                String callerId = getApplicationContext().getPackageManager().getNameForUid(asBinder.getCallingUid());
                Log.i(TAG, "Calling App:" + callerId);
                if (callerId.equals("com.lxh.useraidl")) {
                    //在这里放实际的代码
                }
                return 1;
            }
        };
    
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.i(TAG, "onCreate");
        }
    
        @Override
        public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
            Log.i(TAG, "onStartCommand");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            Log.i(TAG, "onBind");
            return asBinder;
        }
    
        @Override
        public boolean onUnbind(Intent intent) {
            Log.i(TAG, "onUnbind");
            return true;
        }
    
        @Override
        public void onDestroy() {
            Log.i(TAG, "onDestroy");
            super.onDestroy();
        }
    
        public int installPackage(String filepath, int indicator, String version) {
            int value = 0;
            switch (indicator) {
                case 1:
                    Log.d(TAG, "");
                    value = 1;
                    break;
                case 2:
                    value = 0;
                    break;
                default:
                    break;
            }
            return value;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

    清单中

    <queries>
            <package android:name="com.tatv.android.TMC.aidl" />
        </queries>
        
    <service
                android:name=".USTservice"
                android:enabled="true"
                android:exported="true">
                <intent-filter>
                    <action android:name="com.lxh.testaidl.aidl.UST" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </service>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    打开aidl服务

    b新增开机广播接收BootReceiver,用来打开服务

    package com.lxh.testaidl;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.text.TextUtils;
    import android.util.Log;
    
    /**
     * create by lxh on 2022/12/12 Time:14:33
     * tip:
     */
    public class BootReceiver extends BroadcastReceiver {
        private static final String TAG = "BootReceiver lxh";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i(TAG, "onReceive: " + intent.getAction());
            if (!TextUtils.isEmpty(intent.getAction()) && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                Log.i(TAG, "onReceive: ready sent ");
                Intent it1 = new Intent("com.lxh.testaidl.aidl.UST");
                it1.setPackage("com.lxh.testaidl");
                context.startService(it1);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    清单

        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
    <receiver android:name=".BootReceiver"
                android:enabled="true"
                android:exported="true">
                <intent-filter android:priority="1000">
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    aidl使用

    新建aidl使用工程

    新建一个工程。工程名useraidl。包名com.lxh.useraidl。

    增加aidl文件

    增加aidl文件
    文件位置:useraidl\app\src\main\aidl\com\lxh\testaidl\aidl
    文件名:UST.aidl

    package com.lxh.testaidl.aidl;
    
    interface UST {
    
        int installPackage(String filepath,int indicator,String version);
       
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用aidl方法

    package com.lxh.useraidl;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    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.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    import com.lxh.testaidl.aidl.UST;
    public class MainActivity extends AppCompatActivity {
        private static final String TAG = "MainActivity lxh";
        UST tUST;
        Button button;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button=findViewById(R.id.button);
    
            Intent service = new Intent("com.lxh.testaidl.aidl.UST");
            service.setPackage("com.lxh.testaidl");
            bindService(service, connection, Context.BIND_AUTO_CREATE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        tUST.installPackage("1",1,"0");
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            });
    
        }
        private ServiceConnection connection = new ServiceConnection()
        {
            public void onServiceConnected(ComponentName name, IBinder service)
            {
                Log.i(TAG, "tUST");
                tUST = UST.Stub.asInterface(service);
            }
            public void onServiceDisconnected(ComponentName name)
            {
                tUST = null;
            }
        };
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    相关回显

    开机广播:am broadcast -a android.intent.action.BOOT_COMPLETED

    2023-09-13 10:22:00.081 15635-15635/com.lxh.testaidl I/BootReceiver lxh: onReceive: android.intent.action.BOOT_COMPLETED
    2023-09-13 10:22:00.081 15635-15635/com.lxh.testaidl I/BootReceiver lxh: onReceive: ready sent 
    2023-09-13 10:22:00.087 15635-15635/com.lxh.testaidl I/USTservice lxh: USTservice
    2023-09-13 10:22:00.092 15635-15635/com.lxh.testaidl I/USTservice lxh: onCreate
    2023-09-13 10:22:00.093 15635-15635/com.lxh.testaidl I/USTservice lxh: onStartCommand
    2023-09-13 10:22:00.113 15635-15790/com.lxh.testaidl I/USTservice lxh: onHandleIntent
    2023-09-13 10:22:00.123 15635-15635/com.lxh.testaidl I/USTservice lxh: onDestroy
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    运行使用aidl工程

    2023-09-13 10:24:07.733 16174-16174/com.lxh.useraidl I/MainActivity lxh: tUST
    
    • 1

    点击按钮使用aidl方法

    2023-09-13 10:24:07.637 15635-15635/com.lxh.testaidl I/USTservice lxh: USTservice
    2023-09-13 10:24:07.642 15635-15635/com.lxh.testaidl I/USTservice lxh: onCreate
    2023-09-13 10:24:07.643 15635-15635/com.lxh.testaidl I/USTservice lxh: onBind
    2023-09-13 10:25:23.618 15635-15667/com.lxh.testaidl I/USTservice lxh: installPackage(1,1,0
    2023-09-13 10:25:23.619 15635-15667/com.lxh.testaidl I/USTservice lxh: Calling App:com.lxh.useraidl
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    机器学习实验------密度聚类方法之DB-Scan
    唯一性索引与逻辑删除冲突问题解决思路
    【Python】学习导论:Python 简介
    [附源码]计算机毕业设计JAVA乡村振兴惠农推介系统
    Unity3D 与 安卓交互
    【最详细】最新最全Java虚拟机(JVM)面试题(51道)
    运维就业现状怎么样?技能要求高吗?
    docker版jxTMS使用指南:数据总线
    Diffusers库的初识及使用
    【MySQL】MySQL数据库的初阶使用
  • 原文地址:https://blog.csdn.net/weixin_45208598/article/details/132845810